Skip to content

Commit 64fbcc6

Browse files
committed
FEAT: just a very basic WAV codec (decoder)
To be honest, I'm not sure how useful it is as one gets just binary, but no aditional info. It should return a sound object instead! So far it can be considered just as a minimal codec example.
1 parent fc5b8a4 commit 64fbcc6

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

make/make-settings.r

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ Defines: [
1212
USE_PNG_CODEC
1313
USE_GIF_CODEC
1414
USE_JPG_CODEC
15+
USE_WAV_CODEC
1516
;USE_NO_INFINITY ;-- use when you don't want to support IEEE infinity
1617
]

src/core/b-init.c

+3
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@ extern const REBYTE Str_Banner[];
733733
#ifdef USE_JPG_CODEC
734734
Init_JPEG_Codec();
735735
#endif
736+
#ifdef USE_WAV_CODEC
737+
Init_WAV_Codec();
738+
#endif
736739
}
737740

738741

src/core/u-wav.c

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#ifdef USE_WAV_CODEC
2+
/***********************************************************************
3+
**
4+
** REBOL [R3] Language Interpreter and Run-time Environment
5+
**
6+
** Copyright 2012 REBOL Technologies
7+
** REBOL is a trademark of REBOL Technologies
8+
**
9+
** Licensed under the Apache License, Version 2.0 (the "License");
10+
** you may not use this file except in compliance with the License.
11+
** You may obtain a copy of the License at
12+
**
13+
** http://www.apache.org/licenses/LICENSE-2.0
14+
**
15+
** Unless required by applicable law or agreed to in writing, software
16+
** distributed under the License is distributed on an "AS IS" BASIS,
17+
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
** See the License for the specific language governing permissions and
19+
** limitations under the License.
20+
**
21+
************************************************************************
22+
**
23+
** Module: u-wav.c
24+
** Summary: conversion to and from WAV audio format
25+
** Section: utility
26+
** Author: Oldes
27+
** Notes:
28+
** This is an optional part of R3. This file can be replaced by
29+
** library function calls into an updated implementation.
30+
**
31+
** To be honest, I'm not sure how useful it is as one gets just binary,
32+
** but no aditional info. It should return a sound object instead!
33+
** So far it can be considered just as a minimal codec example.
34+
**
35+
***********************************************************************
36+
** Base-code:
37+
38+
if find system/codecs 'wav [
39+
system/codecs/wav/suffixes: [%.wav %.wave]
40+
system/codecs/wav/type: 'binary!
41+
append append system/options/file-types system/codecs/wav/suffixes 'wav
42+
]
43+
44+
***********************************************************************/
45+
46+
#include "sys-core.h"
47+
48+
//**********************************************************************
49+
50+
enum WavChunks {
51+
ChunkID_RIFF = 0x46464952,
52+
ChunkID_WAVE = 0x45564157,
53+
ChunkID_FMT = 0x20746D66,
54+
ChunkID_DATA = 0x61746164,
55+
56+
// Format = 0x666D7420
57+
// LabeledText = 0x478747C6,
58+
// Instrumentation = 0x478747C6,
59+
// Sample = 0x6C706D73,
60+
// Fact = 0x47361666,
61+
// Data = 0x61746164,
62+
// Junk = 0x4b4e554a,
63+
};
64+
65+
typedef struct {
66+
u32 chunk_id;
67+
u32 chunk_size;
68+
u32 format;
69+
u32 fmtchunk_id;
70+
u32 fmtchunk_size;
71+
u16 audio_format;
72+
u16 num_channels;
73+
u32 sample_rate;
74+
u32 byte_rate;
75+
u16 block_align;
76+
u16 bps;
77+
u32 datachunk_id;
78+
u32 datachunk_size;
79+
} WavHeader;
80+
81+
/***********************************************************************
82+
**
83+
*/ static void Decode_WAV(REBCDI *codi)
84+
/*
85+
** Input: WAV encoded sound (codi->data, len)
86+
** Output: Samples bits (codi->bits, len)
87+
** Error: Code in codi->error
88+
** Return: Success as TRUE or FALSE
89+
**
90+
***********************************************************************/
91+
{
92+
REBYTE *cp, *tp;
93+
WavHeader *hdr;
94+
95+
96+
int bytes = codi->len;
97+
98+
cp = codi->data;
99+
// printf("[Decode_WAV] input bytes: %u\n", bytes);
100+
101+
hdr = (WavHeader*)cp;
102+
103+
if(bytes < sizeof(WavHeader) || hdr->chunk_id != ChunkID_RIFF || hdr->format != ChunkID_WAVE) {
104+
codi->error = CODI_ERR_SIGNATURE;
105+
return;
106+
}
107+
108+
// puts("RIFF");
109+
// printf("chunk size: %u\n", hdr->chunk_size);
110+
// printf("num_channels: %i\n", hdr->num_channels);
111+
// printf("format: %i\n", hdr->audio_format);
112+
// printf("sample_rate: %i\n", hdr->sample_rate);
113+
// printf("bps: %i\n", hdr->bps);
114+
// printf("data id: %0X\n", hdr->datachunk_id);
115+
// printf("data size: %u\n", hdr->datachunk_size);
116+
117+
if (hdr->audio_format != 1){
118+
// Only PCM encoding supported
119+
codi->error = CODI_ERR_ENCODING;
120+
return;
121+
}
122+
123+
124+
codi->len = bytes = hdr->datachunk_size;
125+
126+
tp = Make_Mem(bytes);
127+
memcpy(tp, cp + sizeof(WavHeader), bytes);
128+
codi->data = tp;
129+
}
130+
131+
132+
/***********************************************************************
133+
**
134+
*/ static void Encode_WAV(REBCDI *codi)
135+
/*
136+
** Not implemented!
137+
**
138+
***********************************************************************/
139+
{
140+
codi->error = CODI_ERR_NA;
141+
}
142+
143+
144+
/***********************************************************************
145+
**
146+
*/ REBINT Codec_WAV(REBCDI *codi)
147+
/*
148+
***********************************************************************/
149+
{
150+
codi->error = 0;
151+
152+
if (codi->action == CODI_IDENTIFY) {
153+
Decode_WAV(codi);
154+
return CODI_CHECK; // error code is inverted result
155+
}
156+
157+
if (codi->action == CODI_DECODE) {
158+
Decode_WAV(codi);
159+
return CODI_BINARY;
160+
}
161+
162+
if (codi->action == CODI_ENCODE) {
163+
Encode_WAV(codi);
164+
return CODI_BINARY;
165+
}
166+
167+
codi->error = CODI_ERR_NA;
168+
return CODI_ERROR;
169+
}
170+
171+
172+
/***********************************************************************
173+
**
174+
*/ void Init_WAV_Codec(void)
175+
/*
176+
***********************************************************************/
177+
{
178+
Register_Codec("wav", Codec_WAV);
179+
}
180+
181+
#endif

src/tools/file-base.r

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ core: [
112112
u-sha1.c
113113
u-sha256.c
114114
u-zlib.c
115+
u-wav.c
115116
]
116117

117118
made: [

0 commit comments

Comments
 (0)