Skip to content

Commit 68b197a

Browse files
committed
FEAT: very basic WAV encoder
1 parent d076b3d commit 68b197a

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/mezz/codec-wav.r

+54-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ register-codec [
4747
size: UI32LE
4848
starts: INDEX
4949
]
50-
ends: starts + size
50+
ends: starts + size
5151
chunk: any [ try [to tag! id] id ]
5252
if verbose > 0 [
5353
printf [
@@ -152,6 +152,59 @@ register-codec [
152152
]
153153
]
154154
]
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+
155208
identify: func [
156209
"Returns TRUE if binary looks like WAV data"
157210
data [binary!]

0 commit comments

Comments
 (0)