Class: Feep::SoundPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/feep/sound_player.rb

Instance Method Summary collapse

Instance Method Details

#display_text_beep(duration) ⇒ Object

displays a fun ‘feep’ message after playing wav file



43
44
45
46
47
48
49
50
51
# File 'lib/feep/sound_player.rb', line 43

def display_text_beep(duration)
  print 'Fe'
  1.upto(duration) {|ms|
    if ms % 100 == 0
      print 'e'
    end
  }
  puts 'ep!'
end

#play_note(frequency, output_filename, samples_to_write, options) ⇒ Object

main function that creates, plays, and removes note



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/feep/sound_player.rb', line 9

def play_note(frequency, output_filename, samples_to_write, options)
  if options[:verbose]
    puts 'Playing note'
    puts "  frequency:    #{frequency.to_f.abs}"
    puts "  midi:         #{Utils.freq_to_midi(frequency)}"
    puts "  duration:     #{options[:duration]}"
  end
  SoundFile.new.create_sound(frequency, samples_to_write, output_filename, options)
  play_wav_file(output_filename, options[:duration], options[:visual_cue])
  remove_sound(output_filename, options)
end

#play_wav_file(file, duration, visual_cue) ⇒ Object

use command line app to play wav file



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/feep/sound_player.rb', line 22

def play_wav_file(file, duration, visual_cue)
  if OS.windows?
    if command_exists?(SNDPLAYER_WIN)
      display_text_beep(duration) if visual_cue
      system("#{SNDPLAYER_WIN} #{file}")
    else
      puts "couldn't find #{SNDPLAYER_WIN}"
    end
  end

  if OS.mac? || OS.linux?
    if command_exists?(SNDPLAYER_UNIX)
      display_text_beep(duration) if visual_cue
      system("#{SNDPLAYER_UNIX} #{file}")
    else
      puts "couldn't find #{SNDPLAYER_UNIX}"
    end
  end
end

#remove_sound(file, options) ⇒ Object

removes the sound, unless marked to save, and optionally display info about file



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/feep/sound_player.rb', line 55

def remove_sound(file, options)
  unless options[:save]
    if OS.windows?
      system("del #{file}")
    else
      system("rm #{file}")
    end
  else
    if options[:verbose]
      info = WaveFile::Reader.info(file)
      duration = info.duration
      formatted_duration = duration.minutes.to_s.rjust(2, '0') << ':' <<
                         duration.seconds.to_s.rjust(2, '0') << ':' <<
                         duration.milliseconds.to_s.rjust(3, '0')
      puts ''
      puts "Created #{file}"
      puts '---'
      puts "Length:      #{formatted_duration}"
      puts "Format:      #{info.audio_format}"
      puts "Channels:    #{info.channels}"
      puts "Frames:      #{info.sample_frame_count}"
      puts "Sample Rate: #{info.sample_rate}"
    end
  end
end