-
Notifications
You must be signed in to change notification settings - Fork 0
/
example08.lua
159 lines (134 loc) · 4.57 KB
/
example08.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
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
----------------------------------------------------------------------------------------------------
--[[
This example demonstrates how an audio stream can be recorded and replayed using
[Auproc audio receiver objects](https://github.com/osch/lua-auproc/blob/master/doc/README.md#auproc_new_audio_receiver)
and
[Auproc audio sender objects](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 mtmsg = require("mtmsg") -- https://github.com/osch/lua-mtmsg
local auproc = require("auproc") -- https://github.com/osch/lua-auproc
local ljack = require("ljack")
----------------------------------------------------------------------------------------------------
local pi = math.pi
local sin = math.sin
local format = string.format
local function printbold(...)
nocurses.setfontbold(true)
print(...)
nocurses.resetcolors()
end
----------------------------------------------------------------------------------------------------
local client = ljack.client_open("example08.lua")
client:activate()
----------------------------------------------------------------------------------------------------
local function choosePort(type, direction)
local list = client:get_ports(".*", type, direction)
printbold(format("Connect my %s %s port with: ", type, direction == "IN" and "OUT" or "IN"))
for i, p in ipairs(list) do
print(i, p)
end
if #list == 0 then
print("No ports found")
os.exit()
end
while true do
io.write(format("Choose (1-%d or none): ", #list))
local inp = io.read()
if inp == "q" then os.exit() end
if inp ~= "" then
local p = list[tonumber(inp)]
if p then
print()
return p
end
else
print()
return
end
end
end
----------------------------------------------------------------------------------------------------
local myInPort = client:port_register("audio-in", "AUDIO", "IN")
local otherOut = choosePort("AUDIO", "OUT")
print("Connecting my AUDIO IN to", otherOut)
if otherOut then
myInPort:connect(otherOut)
end
local myOutPort = client:port_register("audio-out", "AUDIO", "OUT")
local otherIn = client:get_ports(nil, "AUDIO", "IN")
print("Connecting my AUDIO OUT to", otherIn[1], otherIn[2])
if otherIn[1] then
myOutPort:connect(otherIn[1])
end
if otherIn[2] then
myOutPort:connect(otherIn[2])
end
local received = mtmsg.newbuffer()
local sendQueue = mtmsg.newbuffer()
received:notifier(nocurses, ">")
local qlength = 3
sendQueue:notifier(nocurses, "<", qlength)
local receiver = auproc.new_audio_receiver(myInPort, received)
local sender = auproc.new_audio_sender(myOutPort, sendQueue)
----------------------------------------------------------------------------------------------------
local rate = client:get_sample_rate()
local bufsize = client:get_buffer_size()
local t0 = nil
local t = nil
local allSamples = {}
local quit = false
printbold("Press <Space> to replay recording, <Q> to quit")
print("Recording...")
receiver:activate()
while not quit do
repeat
local frameTime, samples = received:nextmsg(0)
if frameTime then
if not t0 then
t0 = frameTime
t = t0
end
allSamples[#allSamples + 1] = samples
t = t + samples:len()
end
until not frameTime
local c = nocurses.getch()
if c then
c = string.char(c)
if c == "Q" or c == "q" then
printbold("Quit.")
quit = true
elseif c == " " then
printbold(format("Recording finished (%8.3f secs).", (t - t0)/rate))
break
end
end
end
receiver:deactivate()
print("Playing...")
local i = 1
local imax = #allSamples
sender:activate()
while not quit do
while sendQueue:msgcnt() < qlength and i <= imax do
local samples = allSamples[i]
sendQueue:addmsg(samples)
i = i + 1
t = t + samples:len()
end
if i > imax then
printbold("Finished.")
break
end
local c = nocurses.getch()
if c then
c = string.char(c)
if c == "Q" or c == "q" then
printbold("Quit.")
quit = true
end
end
end
----------------------------------------------------------------------------------------------------