-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample02.lua
75 lines (54 loc) · 2.62 KB
/
example02.lua
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
----------------------------------------------------------------------------------------------------
--[[
This examples plays a sinus tone for 4 seconds using
an [Auproc audio sender object](https://github.com/osch/lua-auproc/blob/master/doc/README.md#auproc_new_audio_sender).
--]]
----------------------------------------------------------------------------------------------------
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 pi = math.pi
local sin = math.sin
local format = string.format
----------------------------------------------------------------------------------------------------
local controller = lrtaudio.new()
print("RtAudio API:", controller:getCurrentApi())
controller:openStream { outputChannels = 1 }
controller:startStream()
----------------------------------------------------------------------------------------------------
local rate = controller:getStreamSampleRate()
local nframes = controller:getStreamBufferFrames()
print("Sample rate:", rate)
print("Buffer frames:", nframes)
----------------------------------------------------------------------------------------------------
local sampleQueue = mtmsg.newbuffer()
local sender = auproc.new_audio_sender(controller:getStreamOutput(1), sampleQueue)
----------------------------------------------------------------------------------------------------
local duration = 4 -- seconds
local sampleBuffer = carray.new("float", nframes)
for i = 1, math.floor((duration * rate) / nframes + 1) do
for s = 1, nframes do
local t = (i * nframes + s) / rate
sampleBuffer:set(s, 0.05*sin(t*440*2*pi))
end
sampleQueue:addmsg(sampleBuffer)
end
----------------------------------------------------------------------------------------------------
sampleQueue:notifier(nocurses, "<", 1) -- notifies nocurses if buffer empty
print("Playing sinus for "..duration.." seconds, press <Q> for Quit")
sender:activate()
while sampleQueue:msgcnt() > 0 do
local c = nocurses.getch()
if c then
c = string.char(c)
if c == "Q" or c == "q" then
print("Quit.")
break
end
end
end
print("Finished.")
----------------------------------------------------------------------------------------------------