@@ -47,7 +47,7 @@ register-codec [
47
47
size: UI32LE
48
48
starts: INDEX
49
49
]
50
- ends: starts + size
50
+ ends: starts + size
51
51
chunk: any [ try [to tag! id] id ]
52
52
if verbose > 0 [
53
53
printf [
@@ -152,6 +152,59 @@ register-codec [
152
152
]
153
153
]
154
154
]
155
+
156
+ encode : function [
157
+ spec [object! ]
158
+ ] [
159
+ if 'wave <> select spec 'type [ return none ]
160
+ out: binary (128 + length? spec/data )
161
+ binary/write out [
162
+ #{ 52494646 00000000 57415645 }
163
+ ]
164
+
165
+ index: index? spec/data ; stores original data position
166
+
167
+ foreach [tag value] spec/chunks [
168
+ switch tag [
169
+ <fmt > [
170
+ binary/write out reduce [
171
+ 'BYTES "fmt "
172
+ 'UI32LE 16 + length? value/7
173
+ 'UI16LE value/1 ; compression
174
+ 'UI16LE value/2 ; channels
175
+ 'UI32LE value/3 ; sampleRate
176
+ 'UI32LE value/4 ; bytesPerSec
177
+ 'UI16LE value/5 ; blockAlign
178
+ 'UI16LE value/6 ; bitsPerSample
179
+ 'BYTES value/7
180
+ ]
181
+ ]
182
+ <data > [
183
+ binary/write out reduce [
184
+ 'BYTES "data"
185
+ 'UI32LE value
186
+ 'BYTES copy/part spec/data value
187
+ ]
188
+ spec/data: skip spec/data value
189
+ ]
190
+ <fact > [
191
+ value: to binary! value
192
+ binary/write out reduce [
193
+ 'BYTES "fact"
194
+ 'UI32LE length? value
195
+ 'BYTES value
196
+ ]
197
+ ]
198
+ ]
199
+ ]
200
+
201
+ spec/data: at head spec/data index ;resets the data series to original position
202
+
203
+ bytes: (length? out/buffer ) - 8
204
+ binary/write out reduce ['at 5 'UI32LE bytes]
205
+ out/buffer
206
+ ]
207
+
155
208
identify : func [
156
209
"Returns TRUE if binary looks like WAV data"
157
210
data [binary! ]
0 commit comments