-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathybot_audio_file.xtm
166 lines (134 loc) · 6.88 KB
/
ybot_audio_file.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
;; Include guard
(if (and (defined? '*xtmlib-ybot-audio-file-loaded*) *xtmlib-ybot-audio-file-loaded*)
(sys:load-escape "ybot_audio_file library already loaded"))
(define *xtmlib-ybot-audio-file-loaded* #f)
(define *ybot-lib-dir* "/Users/s2805534/Dropbox/ybot/src/extempore_libs")
(if (not (defined? '*ybot-lib-dir*))
(sys:load-escape "Set the *ybot-lib-dir* variable before loading this library"))
(sys:load (string-append *ybot-lib-dir* "/ybot_audio_stream.xtm"))
;; Dependencies
;(sys:load "libs/aot-cache/sndfile.xtm")
(bind-val SEEK_SET i32 0)
(bind-val SEEK_CUR i32 1)
(bind-val SEEK_END i32 2)
(bind-alias audiofile_t i8*)
(bind-func YbotAudioFileSource_c:[YbotAudioSource_t,i8*]*
(lambda (fname)
(let ((info:SF_INFO* (zalloc))
(audiofile:audiofile_t (sf_open fname SFM_READ info)))
(if (null? audiofile)
(begin
(println "soundfile error:" (sf_strerror audiofile))
silent_source)
;; when sf_open has read the file without error
(let ((frames_in_file:i64 (sf_frames info))
(channels:i64 (i32toi64 (sf_channels info)))
(samplerate:SAMPLE (convert (sf_samplerate info))))
(if (<> samplerate SAMPLERATE)
(println "File samplerate" samplerate "doesn't match the current audio samplerate" SAMPLERATE ) void)
(let* ((frames_read:i64 0)
(current_read_frame:i64 0))
(sf_seek audiofile 0 SEEK_SET)
(lambda (startFrame:i64 frames:i64 channels:i64 output:SAMPLE*)
(sf_seek audiofile startFrame SEEK_SET)
(set! frames_read (sf_readf audiofile output frames))
(set! current_read_frame (+ current_read_frame frames_read))
frames_read)))))))
(bind-func YbotAudioFileWrite:[audiofile_t,SAMPLE*,i64,i64,i8*]*
(lambda (buffer frames channels path)
(let ((info:SF_INFO* (zalloc)))
(tset! info 0 frames)
(tset! info 1 SR)
(tset! info 2 channels)
(tset! info 3 (or SF_FORMAT_WAV )
(audiofile:audiofile_t (sf_open fname SFM_WRITE info))))))
;; (bind-func YbotAudioFileSource_c:[YbotAudioSource_t,i8*]*
;; (lambda (fname)
;; (let ((info:SF_INFO* (zalloc))
;; (audiofile:audiofile_t (sf_open fname SFM_READ info)))
;; (if (null? audiofile)
;; (begin
;; (println "soundfile error:" (sf_strerror audiofile))
;; silent_source)
;; ;; when sf_open has read the file without error
;; (let ((frames_in_file:i64 (sf_frames info))
;; (channels:i64 (i32toi64 (sf_channels info)))
;; (samplerate:SAMPLE (convert (sf_samplerate info))))
;; (if (<> samplerate SAMPLERATE)
;; (println "File samplerate" samplerate "doesn't match the current audio samplerate" SAMPLERATE ) void)
;; (sf_seek audiofile 0 SEEK_SET)
;; (lambda (startFrame:i64 frames:i64 channels:i64 output:SAMPLE*)
;; (sf_seek audiofile startFrame SEEK_SET)
;; (sf_readf audiofile output frames)))))))
(bind-func YbotAudioFileStream_c:[YbotAudioStream*,i8*]*
(lambda (path)
(let* ((filesource (YbotAudioFileSource_c path))
(file_channels:i64 (filesource.channels:i64))
(file_samplerate:SAMPLE (filesource.samplerate))
(file_length:i64 (filesource.frames_in_file))
(bufferedStream:YbotAudioStream* (YbotAudioBufferedStream_c filesource 1000000 file_channels file_samplerate)))
(printf "%p " bufferedStream)
(print bufferedStream)
(if (<> file_samplerate SAMPLERATE)
(let ((convertedStream:YbotAudioStream* (YbotAudioConvertedStream_c bufferedStream SAMPLERATE)))
(println "\nConverted Stream: " convertedStream)
convertedStream)
bufferedStream))))
;; (bind-func YbotAudioFileStream_c:[YbotAudioStream*,i8*]*
;; (lambda (path)
;; (let* ((filesource (YbotAudioFileSource_c path))
;; (file_channels:i64 (filesource.channels:i64))
;; (file_samplerate:SAMPLE (filesource.samplerate))
;; (converter_block_size:i64 32)
;; (converter_err:i32* (zalloc))
;; (conversion_buffer:SAMPLE* (zalloc (* converter_block_size file_channels)))
;; (fileStream:YbotAudioStream* (YbotAudioStream filesource file_channels file_samplerate 0 converter_block_size converter_err conversion_buffer)))
;; (if (<> file_samplerate SAMPLERATE)
;; (let ((convertedStream:YbotAudioStream* (YbotAudioConvertedStream_c fileStream SAMPLERATE)))
;; (println "\nConverted Stream: " convertedStream)
;; convertedStream)
;; fileStream))))
;; (bind-func YbotAudioFileStream_d:[void,YbotAudioStream*]*
;; (lambda (filestream)
;; (let* ((filesource (filestream.filesource:YbotAudioSource_t))
;; (audiofile (filesource.audiofile:audiofile_t))
;; (converter_data (filestream.converter_data:YbotSRC_data*))
;; (converter_err (filestream.converter_err:i32*)))
;; (sf_close audiofile)
;; void)))
;(bind-val hernandez YbotAudioStream* (YbotAudioFileStream_c "/Users/s2805534/Music/iTunes/iTunes Media/Music/Absolute Music/Absolute Disco Classics [Disc 1]/1-07 Born To Be Alive.aiff"))
;(bind-val chitchat YbotAudioStream* (YbotAudioFileStream_c "/Users/s2805534/Dropbox/ybot/projects/CascadePortals/leweton/audio/March_16_2016/DinnerPreparation/DinnerPreparation_LR.WAV"))
;(bind-val mono YbotAudioStream* (YbotAudioFileStream_c "/Users/s2805534/Dropbox/ybot/grants/SwishGoTheFish/EcoAcoustics2016/mono.wav"))
(bind-val birds2 YbotAudioStream* (YbotAudioFileStream_c "/Users/s2805534/Dropbox/ybot/projects/A3D/audio/stereo192000.wav"))
(sys:load "libs/external/audio_dsp_ext.xtm")
(bind-func convolution_verb1
(creverb_st_c
"/Library/Audio/Impulse Responses/IRCAM/IRC_1002_C/IRC_1002_C_R0195_T090_P000_L.wav"
"/Library/Audio/Impulse Responses/IRCAM/IRC_1002_C/IRC_1002_C_R0195_T090_P000_R.wav"))
($ (print chitchat))
(bind-func dsp:DSPMC
(let ((start_time:i64 (now))
(player:YbotAudioSource_t (tref birds2 0)))
(println FRAMES)
(lambda (in:float* out:float* time:i64 userData:i8*)
(player (- time start_time) FRAMES 2 out)
;(println time)
;(let ((i:i64 0))
;(dotimes (i FRAMES)
;(pset! out i (dtof (* 0.2 (- (random) 0.5))))))
void)))
(bind-func dsp:DSP
(let ((offset:i64 (now)) (success:i64 0)
(player:YbotAudioSource_t (tref birds2 0))
(out:SAMPLE* (zalloc CHANNELS)))
(lambda (in:float time:i64 channel:i64 data:float*)
(cond
((= channel 0)
(set! success (player (- time offset) 1 (i32toi64 CHANNELS) out))
(if (< success 0) (begin (println "looping") (set! offset (now))))
void)
(else void))
(pref out channel)
)))
($ (print (dsp.player:YbotAudioSource_t)))
(dsp:set! dsp)