-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample05.lua
141 lines (115 loc) · 4.2 KB
/
example05.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
----------------------------------------------------------------------------------------------------
--[[
This example generates audio samples from Lua script code on the main thread.
This example demonstrates the usage of a
[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 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("example05.lua")
client:activate()
local myPort = client:port_register("audio-out", "AUDIO", "OUT")
local otherPorts = client:get_ports(nil, "AUDIO", "IN")
print("Connecting to", otherPorts[1], otherPorts[2])
myPort:connect(otherPorts[1])
myPort:connect(otherPorts[2])
local queue = mtmsg.newbuffer()
local qlength = 3
queue:notifier(nocurses, "<", qlength)
local sender = auproc.new_audio_sender(myPort, queue)
local buflen = client:get_buffer_size()
local rate = client:get_sample_rate()
local samples = carray.new("float", buflen)
----------------------------------------------------------------------------------------------------
local f1, f2, v1, v2
local frameTime0, frameTime
local function reset()
f1 = 440
f2 = 0.2
v1 = 0.05
v2 = 0.1
frameTime0 = client:frame_time()
frameTime = frameTime0
sender:activate()
end
reset()
----------------------------------------------------------------------------------------------------
local function printValues()
print(format("v1: %8.2f, f1: %8.2f, v2: %8.2f, f2: %8.2f", v1, f1, v2, f2))
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
printHelp()
printValues()
----------------------------------------------------------------------------------------------------
sender:activate()
----------------------------------------------------------------------------------------------------
while true do
while queue:msgcnt() < qlength do
for i = 1, buflen do
local t = (frameTime - frameTime0 + i)/rate
samples:set(i, v1*sin(t*(f1+v2*sin(t*f2*2*pi))*2*pi))
end
queue:addmsg(frameTime, samples)
frameTime = frameTime + buflen
end
local c = nocurses.getch()
if c then
c = string.char(c)
if c == "Q" or c == "q" then
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 == 'p' then
if sender:active() then
sender:deactivate()
else
queue:clear()
sender:activate()
local dt = frameTime - frameTime0
frameTime = client:frame_time()
frameTime0 = frameTime - dt
end
elseif c == 'r' then
reset()
else
printHelp()
end
printValues()
end
end
----------------------------------------------------------------------------------------------------