-
Notifications
You must be signed in to change notification settings - Fork 0
/
ybot_audio_file_reader.xtm
311 lines (274 loc) · 11.3 KB
/
ybot_audio_file_reader.xtm
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(sys:load "libs/aot-cache/ybot_audio_file_reader.xtm" 'quiet)
(sys:load-preload-check 'ybot_audio_file_reader)
(define *xtmlib-ybot_audio_file_reader-loaded* #f)
(impc:aot:suppress-aot-do
(sys:load "libs/core/audio_dsp.xtm")
(sys:load "libs/external/sndfile.xtm")
(sys:load "libs/core/xthread.xtm")
(sys:load "libs/contrib/ybot/ybot_base.xtm"))
(impc:aot:insert-forms
(sys:load "libs/core/audio_dsp.xtm")
(sys:load "libs/external/sndfile.xtm")
(sys:load "libs/core/xthread.xtm")
(sys:load "libs/contrib/ybot/ybot_base.xtm"))
(impc:aot:insert-header "xtmybot_audio_file_reader")
(bind-val SEEK_SET i32 0)
(bind-val SEEK_CUR i32 1)
(bind-val SEEK_END i32 2)
(bind-alias audiofile_t i8*)
(bind-alias mutex_t i8*)
;;;;;;;;;;;;;;;;;;;;;;
;; YbotAudioFileBuffer type ;;
;;;;;;;;;;;;;;;;;;;;;;
;; 0 SAMPLE* (data)
;; 1 number of frames in buffer
;; 2 channels
;; 3 samplerate
;; 4 which frame of the parent audiofile corresponds to the start of the buffer
;; 5 current playhead frame (relative to start of buffer)
;; 6 current writehead frame (relative to the start of buffer)
;; 7 the AudioFile from which it is drawn
;; 8 an integer ID
;; 9 A Mutex for thread synchronisation
;; 10 status flag
(bind-type YbotAudioFileBuffer <SAMPLE*,i64,i64,SAMPLE,i64,i64,i64,audiofile_t,i64,mutex_t*,bool>)
;(bind-alias YAFB YbotAudioFileBuffer)
(bind-val YAFB_before i64 -1)
(bind-val YAFB_well_inside i64 0)
(bind-val YAFB_near_end i64 1)
(bind-val YAFB_past i64 2)
(bind-val YAFB_busy i64 -2)
(bind-func YbotAudioFileBuffer_c:[YbotAudioFileBuffer*,i64,i64,SAMPLE,audiofile_t,i64]*
(lambda (frames channels samplerate audiofile id)
(let ((output:YbotAudioFileBuffer* (zalloc))
(mutex_ptr:mutex_t* (zalloc))
(data:SAMPLE* (zalloc (* frames channels))))
(pset! mutex_ptr 0 (mutex_create))
(tset! output 0 data)
(tset! output 1 frames)
(tset! output 2 channels)
(tset! output 3 samplerate)
(tset! output 4 0)
(tset! output 5 0)
(tset! output 6 0)
(tset! output 7 audiofile)
(tset! output 8 id)
(tset! output 9 mutex_ptr)
(tset! output 10 #t)
output)))
(bind-func YbotAudioFileBuffer_d:[void,YbotAudioFileBuffer*]*
(lambda (buffer)
(con buffer
(mutex_destroy (pref (tref buffer 9) 0)))
void))
(bind-func YAFB_lock:[void,YbotAudioFileBuffer*]*
(lambda (buffer)
(con buffer
(mutex_lock (pref (tref buffer 9) 0)))
void))
(bind-func YAFB_unlock:[void,YbotAudioFileBuffer*]*
(lambda (buffer)
(con buffer
(mutex_unlock (pref (tref buffer 9) 0)))
void))
(bind-func YAFB_trylock:[bool,YbotAudioFileBuffer*]*
(lambda (buffer)
(= 0 (i32toi64 (mutex_trylock (pref (tref buffer 9) 0))))))
(bind-func YAFB_load_from_file_offset:[i64,YbotAudioFileBuffer*,i64]*
(lambda (buffer start_frame)
(println "\n\nStarting to load from offset: " start_frame)
(YAFB_lock buffer)
;(tset! buffer 10 #f)
(let*
((data (tref buffer 0))
(frames (tref buffer 1))
(audiofile (tref buffer 7)))
(sf_seek audiofile start_frame SEEK_SET)
(let ((frames_read (sf_readf audiofile data frames)))
(tset! buffer 4 start_frame)
(tset! buffer 5 start_frame)
;(tset! buffer 10 #t)
(YAFB_unlock buffer)
(println "Finished loading in " frames_read " frames")
frames_read))))
(bind-func YAFB_ready:[bool,YbotAudioFileBuffer*]*
(lambda (buffer)
(if (YAFB_trylock buffer) (begin (YAFB_unlock buffer) #t) (begin (println "Buffer is not ready\n") #f))))
(bind-func YAFB_fetch_sample:[void,YbotAudioFileBuffer*,i64,i64,bool*,SAMPLE*]*
(lambda (buffer frame channel success output)
(if (YAFB_trylock buffer)
(let*
((data (tref buffer 0))
(frames (tref buffer 1))
(channels (tref buffer 2))
(start_frame (tref buffer 4))
(desired_frame (- frame start_frame)))
(if (and (<= 0 desired_frame) (< desired_frame frames))
(begin
(pset! output 0 (pref data (+ channel (* desired_frame channels))))
(pset! success 0 #t)
(YAFB_unlock buffer)
void)
(begin
(pset! output 0 (convert 0.0))
(pset! success 0 #f)
(YAFB_unlock buffer)
void)))
(begin
(pset! output 0 (convert 0.0))
(pset! success 0 #f)
void))))
(bind-func YAFB_contains:[i64,YbotAudioFileBuffer*,i64]*
(lambda (buffer frame) ;; -1 = before buffer, 0 = well inside buffer, 1 = near end of buffer, 2 = past buffer
(if (YAFB_trylock buffer)
(let*
((start_frame (tref buffer 4))
(lngth (tref buffer 1))
(end_frame (+ start_frame lngth))
(output:i64 0))
(cond
((< frame start_frame) (set! output YAFB_before))
((and (>= frame start_frame) (< frame (+ start_frame (/ lngth 2)))) (set! output YAFB_well_inside))
((< frame end_frame) (set! output YAFB_near_end))
(else (set! output YAFB_past)))
(YAFB_unlock buffer)
output)
YAFB_busy)))
(bind-alias YbotAudioFileReader_t [SAMPLE,i64,i64]*)
(bind-func silent_reader:YbotAudioFileReader_t
(lambda (frame channel)
(convert 0.0)))
(bind-func YbotAudioFileReader_c:[YbotAudioFileReader_t,i8*,i64]*
(lambda (fname read_buffer_width)
(let*
((playing_buffer:i64 1)
(buffering:bool #f)
(info:SF_INFO* (salloc))
(audiofile:audiofile_t (sf_open fname SFM_READ info)))
(if (null? audiofile)
(begin
(println "soundfile error:" (sf_strerror audiofile))
silent_reader)
;; when sf_open has read the file without error
(let ((frames (sf_frames info))
(channels (i32toi64 (sf_channels info)))
(samplerate (i32tof (sf_samplerate info))))
(if (= read_buffer_width 0)
(set! read_buffer_width frames) 0)
(if (<> samplerate (convert SAMPLERATE))
(println "File samplerate" samplerate "doesn't match the current audio samplerate" SR ) void)
(if (> read_buffer_width frames)
(set! read_buffer_width frames) 0)
;; initialise and allocate memory for the AudioBuffer
(let*
((frames_read:i64 0) (current_file_read_frame:i64 0)
(buffer1:YbotAudioFileBuffer* (YbotAudioFileBuffer_c read_buffer_width channels (convert samplerate) audiofile 1))
(buffer2:YbotAudioFileBuffer* (YbotAudioFileBuffer_c read_buffer_width channels (convert samplerate) audiofile 2)))
;; read the audio data from the file into buffer1
(println "About to read into buffer1")
(set! frames_read (YAFB_load_from_file_offset buffer1 0))
(println "Have read in " frames_read " frames")
(lambda (frame channel)
(let*
((f (modulo frame frames))
(c (modulo channel channels))
(playbuf (if (= playing_buffer 1) buffer1 buffer2))
(idlebuf (if (= playing_buffer 1) buffer2 buffer1))
(p (YAFB_contains playbuf f))
(success:bool* (salloc))
(value:SAMPLE* (salloc)))
(if (= channel 0)
(cond
((= p YAFB_near_end)
(cond
((and (not buffering) (YAFB_ready playbuf) (YAFB_ready idlebuf)) ;; time to load some more data into the idle buffer
(set! buffering #t)
(spawn (let ((new_start_frame (+ read_buffer_width (tref playbuf 1))))
(lambda () (YAFB_load_from_file_offset idlebuf new_start_frame) void)))
void)
(else void)))
((or (= p YAFB_past) (= p YAFB_before))
(if (YAFB_ready idlebuf) (set! buffering #f) #f)
(let ((q (YAFB_contains idlebuf f)))
(cond
((= q 0)
(if (= playing_buffer 1)
(begin
(set! playing_buffer 2)
(set! playbuf buffer2)
(set! idlebuf buffer1)
void)
(begin
(set! playing_buffer 1)
(set! playbuf buffer1)
(set! idlebuf buffer2)
void)))
(else (println "Something is wrong") void))))
((= p YAFB_well_inside) ;; All is well :)
void)
(else ;; Something weird is happening - generate noise so we can notice it
(pset! value 0 (dtof (* 0.1 (random))))
void
)))
(YAFB_fetch_sample playbuf f c success value)
(pref value 0)))))))))
(bind-func YbotAudioFileReader_d:[i32,YbotAudioFileReader_t]*
(lambda (yafr)
(cond
((non-null yafr)
(let*
((af:i8* (yafr.audiofile))
(buffer1:YbotAudioFileBuffer* (yafr.buffer1))
(buffer2:YbotAudioFileBuffer* (yafr.buffer2)))
(YbotAudioFileBuffer_d buffer2)
(YbotAudioFileBuffer_d buffer1)
(sf_close af)))
(else -1:i32))))
;;;;; Example usage ;;;;;;
;; (bind-func dsp:DSP
;; (let* ((playhead:i64 100000)
;; (playing:i64 1)
;; (master_gain:float 0.2)
;; (reader (YbotAudioFileReader_c "/Users/s2805534/Dropbox/ybot/grants/SwishGoTheFish/audio/Crossbows/Toby_CrossbowsMixStereo.wav" (* 44100 3)))
;; (val:float 0.0))
;; (lambda (in:SAMPLE time:i64 channel:i64 data:SAMPLE*)
;; (if (< 0 playing)
;; (set! val (reader playhead channel))
;; (set! val (dtof 0.0)))
;; (cond ((= channel 0)
;; (set! playhead (+ playhead 1))
;; (cond ((> playhead (reader.frames))
;; (println "Looping")
;; (set! playhead 0)))))
;; (* master_gain val))))
;; (bind-func dsp:DSP
;; (lambda (in:SAMPLE time:i64 channel:i64 data:SAMPLE*)
;; (if (= 0 (modulo time 44100))
;; (spawn (lambda () (println "Spawning ...") void)))
;; (convert 0.0)))
(bind-func dsp:DSPMC
(let* ((playhead:i64 0)
(playing:i64 1)
(master_gain:float 0.2)
(reader (YbotAudioFileReader_c "/Users/ybot/Desktop/pink_noise.wav" 44100)))
(lambda (in:float* out:float* time:i64 userData:i8*)
(let ((c:i64 0) (f:i64 0))
(cond
((< 0 playing)
(dotimes (f FRAMES)
(dotimes (c (i32toi64 CHANNELS))
(pset! out (+ c (* f (i32toi64 CHANNELS))) (reader playhead c))))
(set! playhead (+ playhead FRAMES))
(cond
((> playhead (reader.frames))
(println "Looping")
(set! playhead 0)
void)
(else void)))
(else void))))))
(dsp:set! dsp)
;;(dsp.master_gain 0.5)
;; (dsp.playhead 0)
;;(dsp.playing 1)
(define *xtmlib-ybot_audio_file_reader-loaded* #t)
(impc:aot:insert-footer "xtmybot_audio_file_reader")