play.rb 3.13 KB
Newer Older
1
# play.rb -- play.scm -> play.rb
2 3

# Translator: Michael Scholz <mi-scholz@users.sourceforge.net>
4 5
# Created: 05/04/22 23:36:39
# Changed: 14/11/14 08:21:06
6 7 8 9 10

# playing-related examples
#
# play_often(n)
# play_region_forever(reg)
11
# start_dac(srate, chans)
12 13 14 15 16 17 18 19
# stop_dac
# play_with_amps(snd, *rest)
# play_sine(freq, amp)
# play_sines(freq_and_amps)

# play sound n times -- play_often(3) for example

add_help(:play_often,
20 21
         "play_often(n)  \
Plays the selected sound N times (interruptible via C-g).")
22 23 24
def play_often(n)
  plays = n - 1
  play_once = lambda do |reason|
25
    if plays > 0 and reason == 0
26
      plays -= 1
27
      play(selected_sound(), :start, 0, :stop, play_once)
28 29
    end
  end
30
  play(selected_sound(), :start, 0, :stop, play_once)
31 32 33 34 35 36
end
# bind_key(?p, 0, lambda do |n| play_often([1, n].max) end, false, "play often")

# play region over and over until C-g typed

add_help(:play_region_forever,
37 38
         "play_region_forever(reg)  \
Plays region REG until you interrupt it via C-g.")
39
def play_region_forever(reg)
40 41 42
  if integer?(reg)
    reg = integer2region(reg)
  end
43
  play_region_again = lambda do |reason|
44
    if reason == 0
45
      play(reg, :wait, false, :stop, play_region_again)
46 47
    end
  end
48
  play(reg, :wait, false, :stop, play_region_again)
49 50 51 52 53 54 55 56 57
end
# bind_key(?p, 0,
#          lambda do |n| play_region_forever(Snd.regions[[0, n].max]) end,
#          false, "play region forever")

# play while looping continuously between two movable marks

# hold DAC open and play sounds via keyboard

58 59 60 61 62
add_help(:start_dac,
         "start_dac(srate=44100, chans=1)  \
Starts the DAC running continuously in the background.")
def start_dac(sr = 44100, chans = 1)
  play(false, :srate, sr, :channels, chans)
63 64 65 66 67 68 69
end
alias stop_dac stop_playing

# play_with_amps -- play channels with individually settable amps

add_help(:play_with_amps,
         "play_with_amps(snd, *amps)  \
70 71
Plays snd with each channel scaled by the corresponding amp: \
play_with_amps(0, 1.0, 0.5) plays channel 2 of stereo sound at half amplitude.")
72
def play_with_amps(snd, *amps)
73 74
  chns = channels(snd)
  chns.times do |chn|
75 76 77 78
    player = make_player(snd, chn)
    set_amp_control(amps[chn], player)
    add_player(player)
  end
79
  start_playing(chns, srate(snd))
80 81 82 83
end

# play_sine and play_sines

84 85 86
add_help(:play_sine,
         "play_sine(freq, amp)  \
Plays a 1 second sinewave at freq and amp.")
87
def play_sine(freq, amp)
88 89 90 91 92
  len = 44100
  osc = make_oscil(freq)
  play(lambda do
         (len -= 1).positive? and (amp * oscil(osc))
       end)
93 94
end

95 96 97 98 99 100 101 102 103 104 105
add_help(:play_sines,
         "play_sines(*freq_and_amps)  \
Produces a tone given its spectrum: play_sines([[440, 0.4], [660, 0.3]])")
def play_sines(*freq_and_amps)
  len = 44100
  num_oscs = freq_and_amps.length
  frqs = Vct.new(num_oscs)
  amps = Vct.new(num_oscs)
  freq_and_amps.each_with_index do |fa, i|
    frqs[i] = hz2radians(fa[0])
    amps[i] = fa[1]
106
  end
107 108 109 110
  ob = make_oscil_bank(frqs, Vct.new(num_oscs, 0.0), amps)
  play(lambda do
         (len -= 1).positive? and oscil_bank(ob)
       end)
111
end
112 113 114 115
# play_sines([425, 0.05], [450, 0.01],
#            [470, 0.01], [546, 0.02],
#            [667, 0.01], [789, 0.034],
#            [910, 0.032])
116 117

# play.rb ends here