-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample03.lua
More file actions
227 lines (184 loc) · 7.1 KB
/
Copy pathexample03.lua
File metadata and controls
227 lines (184 loc) · 7.1 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
----------------------------------------------------------------------------------------------------
--[[
This example generates audio samples from Lua script code on the main thread and lets the
user interactively modify oscillator parameters.
This example demonstrates the usage of
[Auproc audio sender object](https://github.com/osch/lua-auproc/blob/master/doc/README.md#auproc_new_audio_sender)
and
[Auproc audio mixer object](https://github.com/osch/lua-auproc/blob/master/doc/README.md#auproc_new_audio_mixer).
--]]
----------------------------------------------------------------------------------------------------
local nocurses = require("nocurses") -- https://github.com/osch/lua-nocurses
local carray = require("carray") -- https://github.com/osch/lua-carray
local mtmsg = require("mtmsg") -- https://github.com/osch/lua-mtmsg
local auproc = require("auproc") -- https://github.com/osch/lua-auproc
local lrtaudio = require("lrtaudio")
----------------------------------------------------------------------------------------------------
local BUFFER_FRAMES = 512
local BUFFER_QUEUE_LENGTH = 3
----------------------------------------------------------------------------------------------------
local pi = math.pi
local sin = math.sin
local format = string.format
local function printbold(...)
nocurses.setfontbold(true) print(...) nocurses.resetcolors()
end
----------------------------------------------------------------------------------------------------
local function choose(title, list, printFilter, noneAllowed)
printbold(format("List %s:", title))
for i, p in ipairs(list) do
if printFilter then
local txt = printFilter(p)
if txt then print(i, txt) end
else
print(i, p)
end
end
if #list == 0 then
print(format("No %s found", title))
os.exit()
end
while true do
io.write(format("Choose %s (1-%d%s): ", title, #list, noneAllowed and " or none" or ""))
local inp = io.read()
if inp == "q" then os.exit() end
if inp ~= "" then
local p = list[tonumber(inp)]
if p then
print()
return tonumber(inp), p
end
elseif noneAllowed then
return
end
end
end
----------------------------------------------------------------------------------------------------
local _, audioApi = choose("RtAudio API", lrtaudio.getCompiledApi())
local controller = lrtaudio.new(audioApi)
----------------------------------------------------------------------------------------------------
local deviceId = choose("Output device",
controller:getDeviceInfo(),
function(d)
return format("%s (out=%d)", d.name, d.outputChannels)
end)
----------------------------------------------------------------------------------------------------
print("RtAudio API:", controller:getCurrentApi())
controller:openStream { outputDevice = deviceId,
outputChannels = 2,
bufferFrames = BUFFER_FRAMES,
numberOfBuffers = BUFFER_QUEUE_LENGTH }
----------------------------------------------------------------------------------------------------
local rate = controller:getStreamSampleRate()
local nframes = controller:getStreamBufferFrames()
print("Output device:", controller:getOutputDeviceInfo().name)
print("Sample rate:", rate)
print("Buffer frames:", nframes)
print("Buffer queue:", BUFFER_QUEUE_LENGTH)
----------------------------------------------------------------------------------------------------
local sampleBuffer = carray.new("float", nframes)
local sampleQueue = mtmsg.newbuffer()
sampleQueue:notifier(nocurses, "<", BUFFER_QUEUE_LENGTH)
local out1Ctrl = mtmsg.newbuffer()
local out2Ctrl = mtmsg.newbuffer()
local soundBuffer = controller:newStreamBuffer()
local soundSender = auproc.new_audio_sender(soundBuffer, sampleQueue)
local out1 = auproc.new_audio_mixer(soundBuffer, controller:getStreamOutput(1), out1Ctrl)
local out2 = auproc.new_audio_mixer(soundBuffer, controller:getStreamOutput(2), out2Ctrl)
out1:activate()
out2:activate()
function setBalance(bal)
out1Ctrl:addmsg(1, 0.5 - 0.5*bal)
out2Ctrl:addmsg(1, 0.5 + 0.5*bal)
end
----------------------------------------------------------------------------------------------------
local f1, f2, v1, v2, bal
local frameTime0, frameTime
local function reset()
f1 = 440
f2 = 0.2
v1 = 0.05
v2 = 0.1
bal = 0
frameTime0 = 0
frameTime = 0
setBalance(bal)
end
reset()
----------------------------------------------------------------------------------------------------
local function printValues()
print(format("v1: %8.2f, f1: %8.2f, v2: %8.2f, f2: %8.2f, bal: %3.2f", v1, f1, v2, f2, bal))
end
local function printHelp()
printbold("Press keys <,>,v,V,b,B,f,F,g,G,h,H,r,p for modifying parameters, q for Quit")
end
print()
printHelp()
printValues()
----------------------------------------------------------------------------------------------------
soundSender:activate()
controller:startStream()
while true do
while sampleQueue:msgcnt() < BUFFER_QUEUE_LENGTH do
for i = 1, nframes do
local t = (frameTime - frameTime0 + i)/rate
sampleBuffer:set(i, v1*sin(t*(f1+v2*sin(t*f2*2*pi))*2*pi))
end
sampleQueue:addmsg(sampleBuffer)
frameTime = frameTime + nframes
end
local c = nocurses.getch()
if c then
c = string.char(c)
if c == "Q" or c == "q" then
soundSender:deactivate()
printbold("Quit.")
break
elseif c == "F" then
f1 = f1 + 20
elseif c == "f" then
f1 = f1 - 20
elseif c == "G" then
f2 = f2 + 0.01
elseif c == "g" then
f2 = f2 - 0.01
elseif c == "H" then
f2 = f2 + 0.1
elseif c == "h" then
f2 = f2 - 0.1
elseif c == "V" then
v1 = v1 + 0.01
elseif c == "v" then
v1 = v1 - 0.01
elseif c == "B" then
v2 = v2 + 0.01
elseif c == "b" then
v2 = v2 - 0.01
elseif c == "<" then
bal = bal - 0.05
if bal < -1 then bal = -1 end
setBalance(bal)
elseif c == ">" then
bal = bal + 0.05
if bal > 1 then bal = 1 end
setBalance(bal)
elseif c == 'p' then
if soundSender:active() then
printbold("Paused.")
soundSender:deactivate()
else
printbold("Continue.")
sampleQueue:clear()
soundSender:activate()
end
elseif c == 'r' then
printbold("Reset.")
reset()
soundSender:activate()
else
printHelp()
end
printValues()
end
end
----------------------------------------------------------------------------------------------------