Class: CWG::ToneGenerator

Inherits:
Object
  • Object
show all
Includes:
ToneHelpers
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb

Constant Summary

MUTE =
false

Constants included from ToneHelpers

CWG::ToneHelpers::DASH_FILENAME, CWG::ToneHelpers::DOT_FILENAME, CWG::ToneHelpers::E_SPACE_FILENAME, CWG::ToneHelpers::HERE, CWG::ToneHelpers::SPACE_FILENAME, CWG::ToneHelpers::TWO_PI

Instance Method Summary (collapse)

Methods included from ToneHelpers

#convert_words, #generate_space, #last_element?, #space_sample?

Constructor Details

- (ToneGenerator) initialize

Returns a new instance of ToneGenerator



13
14
15
16
17
18
19
20
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 13

def initialize
  @max_amplitude = (Params.volume > 1.0 ? 1.0 : Params.volume)
  @wpm = Params.wpm.to_f
  @frequency = Params.frequency
  @effective_wpm = Params.effective_wpm ? Params.effective_wpm.to_f : @wpm
  @sample_rate = 2400
  @print = Print.new
end

Instance Method Details

- (Object) char_space



184
185
186
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 184

def char_space
  @effective_wpm == @wpm ? [space,space] : [e_space,e_space]
end

- (Object) compile_fundamentals



116
117
118
119
120
121
122
123
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 116

def compile_fundamentals
  elements.each do |ele|
    #      progress.increment
    audio_samples = generate_samples ele
    buffer = generate_buffer(audio_samples, ele)
    write_element_audio_file ele, buffer
  end
end

- (Object) create_element_method(ele)

create dot, dash, space or e_space method



98
99
100
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 98

def create_element_method ele
  define_singleton_method(ele) {data[ele]}
end

- (Object) create_element_methods

create dot, dash, space and e_space methods



104
105
106
107
108
109
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 104

def create_element_methods
  elements.each do |ele|
    #      progress.increment
    create_element_method ele
  end
end

- (Object) cw_encoding



40
41
42
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 40

def cw_encoding
  @encoding ||= CwEncoding.new
end

- (Object) data



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 48

def data
  { :dot => {:name => :dot,
             :filename => DOT_FILENAME,
             :spb => (@sample_rate * 1.2 / @wpm).to_i },
    :dash  => {:name => :dash,
               :filename => DASH_FILENAME,
               :spb => (@sample_rate * 3.6 / @wpm).to_i },
    :space => {:name => :space,
               :filename   => SPACE_FILENAME   ,
               :spb => (@sample_rate * 1.2 / @wpm).to_i },
    :e_space => {:name => :e_space,
                 :filename => E_SPACE_FILENAME ,
                 :spb => (@sample_rate * 1.2 / @effective_wpm).to_i }}
end

- (Object) elements



92
93
94
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 92

def elements
  [:dot, :dash, :space, :e_space]
end

- (Object) filter_maybe(size, count)



63
64
65
66
67
68
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 63

def filter_maybe(size, count)
  ramp = 0.05
  ramp_point =  @max_amplitude / ramp
  ampl = (count < ramp_point) ? (ramp * count) : @max_amplitude
  (count > (size - ramp_point)) ? (ramp * (size - count)) : ampl
end

- (Object) generate(wrds)



22
23
24
25
26
27
28
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 22

def generate wrds
  word_parts(wrds)
  #    progress.init elements.size * 3 + (wrds.size)
  create_element_methods
  compile_fundamentals
  write_word_parts
end

- (Object) generate_buffer(audio_samples, ele)



82
83
84
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 82

def generate_buffer audio_samples, ele
  WaveFile::Buffer.new(audio_samples, WaveFile::Format.new(:mono, :float, data[ele][:spb]))
end

- (Object) generate_samples(ele)



111
112
113
114
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 111

def generate_samples ele
  return generate_space(data[ele][:spb]) if space_sample? ele
  generate_tone(data[ele][:spb])  unless    space_sample? ele
end

- (Object) generate_tone(number_of_samples)



70
71
72
73
74
75
76
77
78
79
80
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 70

def generate_tone(number_of_samples)
  audio_samples = [].fill(0.0, 0, number_of_samples)
  number_of_samples.times do |sample_number|
    amplitude = filter_maybe(number_of_samples, sample_number)
    #      amplitude = 1.0 # @max_amplitude
    #      amplitude = 0.01 if MUTE
    sine_radians = ((@frequency * TWO_PI) / @sample_rate) * sample_number
    audio_samples[sample_number] = amplitude * Math.sin(sine_radians)
  end
  audio_samples
end

- (Object) make_word_parts



157
158
159
160
161
162
163
164
165
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 157

def make_word_parts
  puts "make_word_parts"
  parts = []
  @word_parts.each do |part|
    #      progress.increment
    parts += send_char part.downcase
  end
  parts
end

- (Object) play



34
35
36
37
38
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 34

def play
  cmd = play_command + ' ' + play_filename
  puts "cmd = #{cmd}"
  @pid = ! @dry_run ? Process.spawn(cmd) : cmd
end

- (Object) play_filename



30
31
32
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 30

def play_filename
  HERE + "audio/#{Params.audio_filename}"
end

- (Object) prepare_buffers



167
168
169
170
171
172
173
174
175
176
177
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 167

def prepare_buffers
  @buffers = {}
  elements.each do |ele|
    #      progress.increment
    @buffers[ele] = []
    WaveFile::Reader.new(data[ele][:filename]).
      each_buffer(data[ele][:spb]) do |buffer|
      @buffers[ele] = buffer
    end
  end
end

- (Object) progress



44
45
46
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 44

def progress
  @progress ||= Progress.new('Compiling')
end

- (Object) push_enc(chr)



129
130
131
132
133
134
135
136
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 129

def push_enc chr
  arry = []
  chr.each_with_index do |c,idx|
    arry << c
    arry << ((last_element?(idx, chr)) ? (space_or_espace) : space)
  end
  arry += char_space
end

- (Object) reset



208
209
210
211
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 208

def reset
  @word_parts = @progress = nil
  #    puts "\r"
end

- (Object) send_char(c)



138
139
140
141
142
143
144
145
146
147
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 138

def send_char c
  puts "c = #{c}"
  enc = nil
  if c == ' '
    enc = word_space
  else
    enc = cw_encoding.fetch(c).map { |e| send(e)}
  end
  push_enc enc
end

- (Object) space_or_espace



125
126
127
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 125

def space_or_espace
  (@effective_wpm == @wpm) ? space : e_space
end

- (Object) word_composite(word)



192
193
194
195
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 192

def word_composite word
  puts "word_composite"
  send_char word.downcase
end

- (Object) word_parts(str = nil)



149
150
151
152
153
154
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 149

def word_parts str = nil
  return @word_parts if @word_parts
  @word_parts = []
  str.split('').each { |part| @word_parts << part}
  @word_parts
end

- (Object) word_space



188
189
190
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 188

def word_space
  @effective_wpm == @wpm ? [space] : [e_space]
end

- (Object) write_audio



197
198
199
200
201
202
203
204
205
206
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 197

def write_audio
  WaveFile::Writer.new(play_filename, WaveFile::Format.new(:mono, :pcm_16, @sample_rate)) do |writer|
    yield.each do |char|
      #        progress.increment
      char.each do |fta|
        writer.write(@buffers[fta[:name]])
      end
    end
  end
end

- (Object) write_audio_file



213
214
215
216
217
218
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 213

def write_audio_file
  print "@word_parts = "
  p @word_parts
  write_audio { @word_parts.collect {|part| word_composite(part) } }
  reset
end

- (Object) write_element_audio_file(ele, buffer)



86
87
88
89
90
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 86

def write_element_audio_file ele, buffer
  WaveFile::Writer.new(data[ele][:filename], WaveFile::Format.new(:mono, :pcm_16, @sample_rate)) do |writer|
    writer.write(buffer)
  end
end

- (Object) write_word_parts



179
180
181
182
# File '/Users/martyn/cw/cw_clone/lib/cw/tone_generator.rb', line 179

def write_word_parts
  prepare_buffers
  write_audio_file
end