updates for splitting up assets and code etc
[swamp-bikeopera:code.git] / src / swamp_playd
1 #!/usr/bin/lua
2
3 -- Swamp Bike Opera embedded system for Kaffe Matthews 
4 -- Copyright (C) 2012 Wolfgang Hauptfleisch, Dave Griffiths
5 --
6 -- This program is free software: you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as published by
8 -- the Free Software Foundation, either version 3 of the License, or
9 -- (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package.path = package.path..";./?.lua;../lib/?.lua"
18 package.cpath = package.cpath..";./?.so;../lib/?.so"
19
20
21 -- create an audio device using default parameters and exit in case of errors
22 require("proAudioRt")
23 require("posix")
24 require("socket")
25
26 print("starting player")
27 posix.mkfifo("/tmp/dodo")
28
29 dofile '../config/swamp.conf'
30
31 if not proAudio.create( ) then -- 8, 44000, 1024 ) then 
32    os.exit() 
33 end
34
35 --- initialize the sample and sound table
36     sample = {}
37     sound = {}
38
39 -- PITCHES
40
41 print("pre-loading")
42 start_load = os.time()
43 -- PRELOADING
44 sample[1] = proAudio.sampleFromFile(CONFIG.sound_path.."1.ogg")
45 print("1")
46 --sample[2] = proAudio.sampleFromFile(CONFIG.sound_path.."2.ogg")
47 print("2")
48 --sample[3] = proAudio.sampleFromFile(CONFIG.sound_path.."3.ogg")
49 print("3")
50 --sample[4] = proAudio.sampleFromFile(CONFIG.sound_path.."4.ogg")
51 print("4")
52 --sample[5] = proAudio.sampleFromFile(CONFIG.sound_path.."5.ogg")
53 print("5")
54 --sample[6] = proAudio.sampleFromFile(CONFIG.sound_path.."6.ogg")
55 print("6")
56 --sample[7] = proAudio.sampleFromFile(CONFIG.sound_path.."7.ogg")
57 print("7")
58 --sample[8] = proAudio.sampleFromFile(CONFIG.sound_path.."8.ogg")
59 print("8")
60 --sample[9] = proAudio.sampleFromFile(CONFIG.sound_path.."9.ogg")
61 print("9")
62 --sample[10] = proAudio.sampleFromFile(CONFIG.sound_path.."10.ogg")
63 print("10")
64 --sample[31] = proAudio.sampleFromFile(CONFIG.sound_path.."31.ogg")
65 --sample[32] = proAudio.sampleFromFile(CONFIG.sound_path.."32.ogg")
66 --sample[33] = proAudio.sampleFromFile(CONFIG.sound_path.."33.ogg")
67 --sample[34] = proAudio.sampleFromFile(CONFIG.sound_path.."34.ogg")
68 --sample[35] = proAudio.sampleFromFile(CONFIG.sound_path.."35.ogg")
69 --sample[36] = proAudio.sampleFromFile(CONFIG.sound_path.."36.ogg")
70 --sample[37] = proAudio.sampleFromFile(CONFIG.sound_path.."37.ogg")
71 --sample[38] = proAudio.sampleFromFile(CONFIG.sound_path.."38.ogg")
72 --sample[39] = proAudio.sampleFromFile(CONFIG.sound_path.."39.ogg")
73 print("pre-loading finished")
74 print("took "..os.time() - start_load.." seconds.")
75
76
77 PITCH = { slow = 0.5 ,
78
79 normal = 1.0 , 
80
81 fast = 2.0 ,
82
83 }
84
85 local file = io.open("/tmp/dodo","r")
86
87
88 while true do
89   socket.sleep(0.5)
90   local line = file:read()
91   if line then
92
93     local message , n = string.match(line, "^(.-)%s(.-)$")
94     print(message, n)
95     local n = tonumber(n)
96
97   if n and message then
98
99     if message == "play" then
100          if not sound[n] then
101             --sample[n] = proAudio.sampleFromFile(CONFIG.sound_path.."audio/"..n..".ogg")
102             if sample[n] then sound[n] = proAudio.soundPlay(sample[n])
103             else print("sound "..n.." not loaded")
104             end
105          else
106             print("sound "..n.." allready playing")
107          end
108
109     elseif message == "loop" then
110          if not sound[n] then
111             --sample[n] = proAudio.sampleFromFile(CONFIG.sound_path.."audio/"..n..".ogg")
112             if sample[n] then sound[n] = proAudio.soundLoop(sample[n])
113             else print("sound "..n.." not loaded")
114             end
115          else
116             print("sound "..n.." allready playing")
117          end
118
119    elseif message == "play_right" then
120          if not sound[n] then
121             --sample[n] = proAudio.sampleFromFile(CONFIG.sound_path.."audio/"..n..".ogg")
122             if sample[n] then sound[n] = proAudio.soundLoop(sample[n])
123             else print("sound "..n.." not loaded")
124             end
125             proAudio.soundUpdate( sound[n], 0.0, 2.0, 0.0, 1.0 )
126          else
127             print("sound "..n.." allready playing")
128          end
129
130    elseif message == "play_left" then
131          if not sound[n] then
132             --sample[n] = proAudio.sampleFromFile(CONFIG.sound_path.."audio/"..n..".ogg")
133             if sample[n] then sound[n] = proAudio.soundLoop(sample[n])
134             else print("sound "..n.." not loaded")
135             end
136             proAudio.soundUpdate( sound[n], 2.0, 0.0, 0.0, 1.0 )
137          else
138             print("sound "..n.." allready playing")
139          end
140
141
142     elseif message == "stop" then
143          if n == 100 then
144             for z = 1, 22 do
145                if sound[n] then
146                 proAudio.soundStop(sound[n])
147                 sound[n] = false
148                end
149             end
150          else
151
152          if sound[n] then
153              proAudio.soundStop(sound[n])
154              sound[n] = false
155          end
156
157          end
158
159     elseif message == "right" then
160           if sound[n] then
161              proAudio.soundUpdate( sound[n], 0.0, 2.0, 0.0, 1.0 )
162              --posix.sleep(1)
163           end
164
165     elseif message == "left" then
166          if sound[n] then
167              proAudio.soundUpdate( sound[n], 2.0, 0.0, 0.0, 1.0 )  
168              --posix.sleep(2)
169          end
170
171     elseif message == "slow" then
172          if sound[n] then
173              proAudio.soundUpdate( sound[n], 1.0, 1.0, 0.0, 0.5 )
174              --posix.sleep(1)
175          end
176
177     elseif message == "normal" then
178          if sound[n] then
179              proAudio.soundUpdate( sound[n], 1.0, 1.0, 0.0, 1.0 )
180              --posix.sleep(1)
181          end
182
183     elseif message == "fast" then
184          if sound[n] then
185              proAudio.soundUpdate( sound[n], 1.0, 1.0, 0.0, 2.0 )
186              --posix.sleep(1)
187          end
188
189
190     elseif message == "fadeout" then
191              for i= 1 , 0 , -0.1 do
192                  proAudio.soundUpdate( sound[n], i, i, 0.0, 1.0  )
193                  socket.sleep(0.5)
194              end
195           proAudio.soundStop(sound[n])
196           sound[n] = false
197     else
198          print("invalid message")
199     end    
200
201    else 
202     print("invalid or missing parameter")
203    end
204
205   end
206
207
208
209 end
210
211 os.remove("/tmp/dodo")
212 print("exit")