File: play.rb

package info (click to toggle)
snd 26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,028 kB
  • sloc: ansic: 291,903; lisp: 260,506; ruby: 71,134; sh: 3,293; fortran: 2,342; csh: 1,067; makefile: 294; cpp: 294; python: 87; xml: 27; javascript: 1
file content (117 lines) | stat: -rw-r--r-- 3,201 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# play.rb -- play.scm -> play.rb

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

# playing-related examples
#
# play_often(n)
# play_region_forever(reg)
# start_dac(srate, chans)
# 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,
         "play_often(n)  \
Plays the selected sound N times (interruptible via C-g).")
def play_often(n)
  plays = n - 1
  play_once = lambda do |reason|
    if plays > 0 and reason == 0
      plays -= 1
      play(selected_sound(), :start, 0, :stop, play_once)
    end
  end
  play(selected_sound(), :start, 0, :stop, play_once)
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,
         "play_region_forever(reg)  \
Plays region REG until you interrupt it via C-g.")
def play_region_forever(reg)
  if integer?(reg)
    reg = integer2region(reg)
  end
  play_region_again = lambda do |reason|
    if reason == 0
      play(reg, :wait, false, :stop, play_region_again)
    end
  end
  play(reg, :wait, false, :stop, play_region_again)
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

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)
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)  \
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.")
def play_with_amps(snd, *amps)
  chns = channels(snd)
  chns.times do |chn|
    player = make_player(snd, chn)
    set_amp_control(amps[chn], player)
    add_player(player)
  end
  start_playing(chns, srate(snd))
end

# play_sine and play_sines

add_help(:play_sine,
         "play_sine(freq, amp)  \
Plays a 1 second sinewave at freq and amp.")
def play_sine(freq, amp)
  len = 44100
  osc = make_oscil(freq)
  play(lambda do
         (len -= 1).positive? and (amp * oscil(osc))
       end)
end

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]
  end
  ob = make_oscil_bank(frqs, Vct.new(num_oscs, 0.0), amps)
  play(lambda do
         (len -= 1).positive? and oscil_bank(ob)
       end)
end
# play_sines([425, 0.05], [450, 0.01],
#            [470, 0.01], [546, 0.02],
#            [667, 0.01], [789, 0.034],
#            [910, 0.032])

# play.rb ends here