nabaztag_hack_kit 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +7 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +4 -0
- data/README.md +119 -0
- data/Rakefile +21 -0
- data/bin/mtl_comp +41 -0
- data/bin/mtl_merge +22 -0
- data/bin/mtl_simu +40 -0
- data/bytecode/lib/buffer.mtl +109 -0
- data/bytecode/lib/button.mtl +27 -0
- data/bytecode/lib/data_helper.mtl +88 -0
- data/bytecode/lib/ear.mtl +90 -0
- data/bytecode/lib/led.mtl +33 -0
- data/bytecode/lib/rfid.mtl +50 -0
- data/bytecode/main.mtl +129 -0
- data/config.ru +6 -0
- data/ext/bytecode/Commands.md +172 -0
- data/ext/bytecode/Docs.md +488 -0
- data/ext/bytecode/README.md +5 -0
- data/ext/bytecode/lib/arp.mtl +159 -0
- data/ext/bytecode/lib/cfg.mtl +74 -0
- data/ext/bytecode/lib/choreos.mtl +1487 -0
- data/ext/bytecode/lib/dhcp.mtl +152 -0
- data/ext/bytecode/lib/dns.mtl +89 -0
- data/ext/bytecode/lib/http.mtl +84 -0
- data/ext/bytecode/lib/tcp.mtl +340 -0
- data/ext/bytecode/lib/udp.mtl +49 -0
- data/ext/bytecode/lib/util.mtl +74 -0
- data/ext/bytecode/lib/var.mtl +15 -0
- data/ext/bytecode/lib/wifi.mtl +243 -0
- data/ext/bytecode/nominal-ping.mtl +5828 -0
- data/ext/mtl/Makefile +42 -0
- data/ext/mtl/README.md +13 -0
- data/ext/mtl/bc.cpp +1891 -0
- data/ext/mtl/conf.bin.sans_password +0 -0
- data/ext/mtl/config.txt +5 -0
- data/ext/mtl/dumpbc.c +2566 -0
- data/ext/mtl/extconf.rb +1 -0
- data/ext/mtl/linux_simu.c +271 -0
- data/ext/mtl/linux_simuaudio.c +16 -0
- data/ext/mtl/linux_simuaudio.h +18 -0
- data/ext/mtl/linux_simunet.c +620 -0
- data/ext/mtl/linux_simunet.h +7 -0
- data/ext/mtl/log.c +297 -0
- data/ext/mtl/log.h +20 -0
- data/ext/mtl/main_compiler.cpp +104 -0
- data/ext/mtl/main_simu.cpp +221 -0
- data/ext/mtl/mp3/GTKANAL.H +97 -0
- data/ext/mtl/mp3/LAYER3.C +2090 -0
- data/ext/mtl/mp3/TABINIT.C +82 -0
- data/ext/mtl/mp3/common.c +265 -0
- data/ext/mtl/mp3/dct64_i386.c +316 -0
- data/ext/mtl/mp3/decode_i386.c +155 -0
- data/ext/mtl/mp3/huffman.h +332 -0
- data/ext/mtl/mp3/interface.c +258 -0
- data/ext/mtl/mp3/mpg123.h +182 -0
- data/ext/mtl/mp3/mpglib.h +44 -0
- data/ext/mtl/properties.c +293 -0
- data/ext/mtl/properties.h +10 -0
- data/ext/mtl/simu.c +750 -0
- data/ext/mtl/simuaudio.c +662 -0
- data/ext/mtl/simuaudio.h +74 -0
- data/ext/mtl/simunet.c +400 -0
- data/ext/mtl/simunet.h +30 -0
- data/ext/mtl/utils/correct_const.sh +34 -0
- data/ext/mtl/vaudio.c +677 -0
- data/ext/mtl/vaudio.h +46 -0
- data/ext/mtl/vbc.h +160 -0
- data/ext/mtl/vbc_str.h +166 -0
- data/ext/mtl/vcomp/Makefile +29 -0
- data/ext/mtl/vcomp/bootstrap.cpp +89 -0
- data/ext/mtl/vcomp/compiler.cpp +470 -0
- data/ext/mtl/vcomp/compiler.h +200 -0
- data/ext/mtl/vcomp/compiler_file.cpp +929 -0
- data/ext/mtl/vcomp/compiler_prog.cpp +250 -0
- data/ext/mtl/vcomp/compiler_term.cpp +1053 -0
- data/ext/mtl/vcomp/compiler_type.cpp +872 -0
- data/ext/mtl/vcomp/compiler_var.cpp +289 -0
- data/ext/mtl/vcomp/file.cpp +79 -0
- data/ext/mtl/vcomp/file.h +39 -0
- data/ext/mtl/vcomp/filesystem.h +14 -0
- data/ext/mtl/vcomp/interpreter.cpp +85 -0
- data/ext/mtl/vcomp/interpreter.h +121 -0
- data/ext/mtl/vcomp/memory.cpp +241 -0
- data/ext/mtl/vcomp/memory.h +326 -0
- data/ext/mtl/vcomp/param.h +95 -0
- data/ext/mtl/vcomp/parser.cpp +427 -0
- data/ext/mtl/vcomp/parser.h +97 -0
- data/ext/mtl/vcomp/parser_xml.cpp +124 -0
- data/ext/mtl/vcomp/prodbuffer.cpp +125 -0
- data/ext/mtl/vcomp/prodbuffer.h +42 -0
- data/ext/mtl/vcomp/resource.h +17 -0
- data/ext/mtl/vcomp/stdlib_core.cpp +122 -0
- data/ext/mtl/vcomp/terminal.cpp +73 -0
- data/ext/mtl/vcomp/terminal.h +30 -0
- data/ext/mtl/vcomp/util.cpp +48 -0
- data/ext/mtl/vcomp/util.h +31 -0
- data/ext/mtl/vinterp.c +1349 -0
- data/ext/mtl/vinterp.h +11 -0
- data/ext/mtl/vloader.c +127 -0
- data/ext/mtl/vloader.h +31 -0
- data/ext/mtl/vlog.c +589 -0
- data/ext/mtl/vlog.h +69 -0
- data/ext/mtl/vmem.c +424 -0
- data/ext/mtl/vmem.h +107 -0
- data/ext/mtl/vnet.c +255 -0
- data/ext/mtl/vnet.h +19 -0
- data/lib/nabaztag_hack_kit/message/api.rb +39 -0
- data/lib/nabaztag_hack_kit/message/helper.rb +39 -0
- data/lib/nabaztag_hack_kit/message.rb +36 -0
- data/lib/nabaztag_hack_kit/server.rb +50 -0
- data/lib/nabaztag_hack_kit/version.rb +3 -0
- data/lib/nabaztag_hack_kit.rb +4 -0
- data/nabaztag_hack_kit.gemspec +29 -0
- data/public/bytecode.bin +0 -0
- data/test/bytecode/helper.mtl +60 -0
- data/test/bytecode/native.mtl +28 -0
- data/test/bytecode/test.mtl +221 -0
- data/test/spec_helper.rb +5 -0
- data/test/unit/message_spec.rb +56 -0
- metadata +209 -0
data/ext/mtl/vaudio.c
ADDED
@@ -0,0 +1,677 @@
|
|
1
|
+
// VLISP Virtual Machine - 2006 - by Sylvain Huet
|
2
|
+
// Lowcost IS Powerfull
|
3
|
+
|
4
|
+
#include"vmem.h"
|
5
|
+
|
6
|
+
#include <string.h>
|
7
|
+
|
8
|
+
#ifdef VSIMU
|
9
|
+
#include<stdio.h>
|
10
|
+
#include<time.h>
|
11
|
+
#endif
|
12
|
+
|
13
|
+
#include"vloader.h"
|
14
|
+
#include"vinterp.h"
|
15
|
+
#include"vaudio.h"
|
16
|
+
|
17
|
+
#ifdef VSIMU
|
18
|
+
#include"linux_simuaudio.h"
|
19
|
+
#endif
|
20
|
+
#ifdef VREAL
|
21
|
+
#include"audio.h"
|
22
|
+
#endif
|
23
|
+
|
24
|
+
char audioFifoPlay[AUDIO_FIFOPLAY];
|
25
|
+
|
26
|
+
int play_w;
|
27
|
+
int play_r;
|
28
|
+
|
29
|
+
void audioInit()
|
30
|
+
{
|
31
|
+
play_w=play_r=0;
|
32
|
+
}
|
33
|
+
|
34
|
+
void audioPlayStart(int freq,int bps,int stereo,int trytofeed)
|
35
|
+
{
|
36
|
+
play_w=play_r=0;
|
37
|
+
#ifdef VSIMU
|
38
|
+
PlayStart(freq,stereo,44100,bps,3);
|
39
|
+
#endif
|
40
|
+
#ifdef VREAL
|
41
|
+
// set_vlsi_volume(get_adc_value()/2); //volume on 8bits, 0x00 => maximum
|
42
|
+
play_start(trytofeed);
|
43
|
+
#endif
|
44
|
+
}
|
45
|
+
|
46
|
+
int audioPlayFeed(char *src,int len)
|
47
|
+
{
|
48
|
+
//#ifdef VSIMU
|
49
|
+
int i_end;
|
50
|
+
if (!src)
|
51
|
+
{
|
52
|
+
#ifdef VSIMU
|
53
|
+
PlayEof();
|
54
|
+
#endif
|
55
|
+
#ifdef VREAL
|
56
|
+
play_eof();
|
57
|
+
#endif
|
58
|
+
return 0;
|
59
|
+
}
|
60
|
+
i_end=play_r-1;
|
61
|
+
if (play_w>=play_r) i_end=AUDIO_FIFOPLAY+play_r-1;
|
62
|
+
if (play_w+len>=i_end) len=i_end-play_w;
|
63
|
+
else i_end=play_w+len;
|
64
|
+
|
65
|
+
if (i_end<=play_w) return 0;
|
66
|
+
if (i_end<AUDIO_FIFOPLAY)
|
67
|
+
{
|
68
|
+
len=i_end-play_w;
|
69
|
+
memcpy(audioFifoPlay+play_w,src,len);
|
70
|
+
play_w=i_end;
|
71
|
+
return len;
|
72
|
+
}
|
73
|
+
len=AUDIO_FIFOPLAY-play_w;
|
74
|
+
memcpy(audioFifoPlay+play_w,src,len);
|
75
|
+
src+=len;
|
76
|
+
play_w=0;
|
77
|
+
i_end-=AUDIO_FIFOPLAY;
|
78
|
+
if (i_end) memcpy(audioFifoPlay,src,i_end);
|
79
|
+
play_w=i_end;
|
80
|
+
return len+i_end;
|
81
|
+
//#endif
|
82
|
+
/*
|
83
|
+
#ifdef VREAL
|
84
|
+
set_vlsi_volume(get_adc_value()/2); //volume on 8bits, 0x00 => maximum
|
85
|
+
//Play file
|
86
|
+
play_audio((UBYTE*)src,len);
|
87
|
+
#endif
|
88
|
+
*/
|
89
|
+
}
|
90
|
+
|
91
|
+
void audioPlayStop()
|
92
|
+
{
|
93
|
+
#ifdef VSIMU
|
94
|
+
PlayStop();
|
95
|
+
#endif
|
96
|
+
#ifdef VREAL
|
97
|
+
play_stop();
|
98
|
+
#endif
|
99
|
+
}
|
100
|
+
|
101
|
+
int audioPlayTryFeed(int ask)
|
102
|
+
{
|
103
|
+
int lastw;
|
104
|
+
int play_w0=play_w;
|
105
|
+
int dispo=play_w-play_r;
|
106
|
+
if (dispo<0) dispo+=AUDIO_FIFOPLAY;
|
107
|
+
if (dispo>=ask) return 0;
|
108
|
+
do
|
109
|
+
{
|
110
|
+
lastw=play_w;
|
111
|
+
dispo=play_w-play_r;
|
112
|
+
if (dispo<0) dispo+=AUDIO_FIFOPLAY;
|
113
|
+
|
114
|
+
VPUSH(INTTOVAL(AUDIO_FIFOPLAY-dispo));
|
115
|
+
VPUSH(VCALLSTACKGET(sys_start,SYS_CBPLAY));
|
116
|
+
if (VSTACKGET(0)==NIL)
|
117
|
+
{
|
118
|
+
VPULL();
|
119
|
+
VPULL();
|
120
|
+
return 0;
|
121
|
+
}
|
122
|
+
interpGo();
|
123
|
+
VPULL();
|
124
|
+
}
|
125
|
+
while(play_w!=lastw);
|
126
|
+
if (play_w!=play_w0) return 1;
|
127
|
+
return 0;
|
128
|
+
}
|
129
|
+
|
130
|
+
|
131
|
+
int audioPlayFetchByte()
|
132
|
+
{
|
133
|
+
int res;
|
134
|
+
if (play_w==play_r) return -1;
|
135
|
+
res=audioFifoPlay[play_r++]&255;
|
136
|
+
if (play_r>=AUDIO_FIFOPLAY) play_r-=AUDIO_FIFOPLAY;
|
137
|
+
return res;
|
138
|
+
}
|
139
|
+
|
140
|
+
int audioPlayFetch(char* dst,int ask)
|
141
|
+
{
|
142
|
+
int n=0;
|
143
|
+
int c;
|
144
|
+
audioPlayTryFeed(ask);
|
145
|
+
while(n<ask)
|
146
|
+
{
|
147
|
+
c=audioPlayFetchByte();
|
148
|
+
if (c<0) return n;
|
149
|
+
dst[n++]=c;
|
150
|
+
}
|
151
|
+
return n;
|
152
|
+
}
|
153
|
+
|
154
|
+
void audioVol(int vol)
|
155
|
+
{
|
156
|
+
#ifdef VSIMU
|
157
|
+
// printf("xxxx audioVol %d\n",vol);
|
158
|
+
|
159
|
+
audioSetVolume(255-vol);
|
160
|
+
#endif
|
161
|
+
#ifdef VREAL
|
162
|
+
set_vlsi_volume((vol&255)/2);
|
163
|
+
#endif
|
164
|
+
}
|
165
|
+
|
166
|
+
int audioPlayTime()
|
167
|
+
{
|
168
|
+
#ifdef VSIMU
|
169
|
+
printf("xxxx audioPlayTime\n");
|
170
|
+
return 0;
|
171
|
+
#endif
|
172
|
+
#ifdef VREAL
|
173
|
+
return check_decode_time();
|
174
|
+
#endif
|
175
|
+
}
|
176
|
+
|
177
|
+
int audioRecStart(int freq,int gain)
|
178
|
+
{
|
179
|
+
#ifdef VSIMU
|
180
|
+
RecStart(freq,505,4);
|
181
|
+
#endif
|
182
|
+
#ifdef VREAL
|
183
|
+
rec_start(freq,gain);
|
184
|
+
#endif
|
185
|
+
return 0;
|
186
|
+
}
|
187
|
+
|
188
|
+
int audioRecStop()
|
189
|
+
{
|
190
|
+
#ifdef VSIMU
|
191
|
+
RecStop();
|
192
|
+
#endif
|
193
|
+
#ifdef VREAL
|
194
|
+
rec_stop();
|
195
|
+
#endif
|
196
|
+
return 0;
|
197
|
+
}
|
198
|
+
|
199
|
+
char* audioRecFeed_begin(int size)
|
200
|
+
{
|
201
|
+
VPUSH(PNTTOVAL(VMALLOCBIN(size)));
|
202
|
+
return (char*)VSTARTBIN(VALTOPNT(VSTACKGET(0)));
|
203
|
+
}
|
204
|
+
|
205
|
+
void audioRecFeed_end()
|
206
|
+
{
|
207
|
+
VPUSH(VCALLSTACKGET(sys_start,SYS_CBREC));
|
208
|
+
if (VSTACKGET(0)!=NIL) interpGo();
|
209
|
+
else { VPULL();}
|
210
|
+
VPULL();
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
#define uint16_t unsigned short
|
215
|
+
#define int16_t short
|
216
|
+
#define uint8_t unsigned char
|
217
|
+
#define int8_t char
|
218
|
+
|
219
|
+
static const uint16_t IMA_ADPCMStepTable[89] =
|
220
|
+
{
|
221
|
+
7, 8, 9, 10, 11, 12, 13, 14,
|
222
|
+
16, 17, 19, 21, 23, 25, 28, 31,
|
223
|
+
34, 37, 41, 45, 50, 55, 60, 66,
|
224
|
+
73, 80, 88, 97, 107, 118, 130, 143,
|
225
|
+
157, 173, 190, 209, 230, 253, 279, 307,
|
226
|
+
337, 371, 408, 449, 494, 544, 598, 658,
|
227
|
+
724, 796, 876, 963, 1060, 1166, 1282, 1411,
|
228
|
+
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
|
229
|
+
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
|
230
|
+
7132, 7845, 8630, 9493,10442,11487,12635,13899,
|
231
|
+
15289,16818,18500,20350,22385,24623,27086,29794,
|
232
|
+
32767
|
233
|
+
};
|
234
|
+
|
235
|
+
|
236
|
+
static const int IMA_ADPCMIndexTable[8] =
|
237
|
+
{
|
238
|
+
-1, -1, -1, -1, 2, 4, 6, 8,
|
239
|
+
};
|
240
|
+
|
241
|
+
|
242
|
+
int16_t PredictedValue;
|
243
|
+
uint8_t StepIndex;
|
244
|
+
int16_t vmin,vmax;
|
245
|
+
int vol;
|
246
|
+
|
247
|
+
void mydecodeinit(int s0,int st)
|
248
|
+
{
|
249
|
+
PredictedValue=s0;
|
250
|
+
StepIndex=st;
|
251
|
+
vmin=0;
|
252
|
+
vmax=0;
|
253
|
+
vol=0;
|
254
|
+
}
|
255
|
+
|
256
|
+
int mydecode(unsigned int adpcm)
|
257
|
+
{
|
258
|
+
int diff;
|
259
|
+
int predicedValue;
|
260
|
+
int stepIndex = StepIndex;
|
261
|
+
int step = IMA_ADPCMStepTable[stepIndex];
|
262
|
+
|
263
|
+
stepIndex += IMA_ADPCMIndexTable[adpcm&7];
|
264
|
+
if(stepIndex<0)
|
265
|
+
stepIndex = 0;
|
266
|
+
else if(stepIndex>88)
|
267
|
+
stepIndex = 88;
|
268
|
+
StepIndex = stepIndex;
|
269
|
+
|
270
|
+
diff = step>>3;
|
271
|
+
if(adpcm&4)
|
272
|
+
diff += step;
|
273
|
+
if(adpcm&2)
|
274
|
+
diff += step>>1;
|
275
|
+
if(adpcm&1)
|
276
|
+
diff += step>>2;
|
277
|
+
|
278
|
+
predicedValue = PredictedValue;
|
279
|
+
if(adpcm&8)
|
280
|
+
predicedValue -= diff;
|
281
|
+
else
|
282
|
+
predicedValue += diff;
|
283
|
+
if(predicedValue<-0x8000)
|
284
|
+
predicedValue = -0x8000;
|
285
|
+
else if(predicedValue>0x7fff)
|
286
|
+
predicedValue = 0x7fff;
|
287
|
+
PredictedValue = predicedValue;
|
288
|
+
// if (predicedValue>vmax) vmax=predicedValue;
|
289
|
+
// if (predicedValue<vmin) vmin=predicedValue;
|
290
|
+
vol+=(predicedValue*predicedValue)>>16;
|
291
|
+
return predicedValue;
|
292
|
+
}
|
293
|
+
|
294
|
+
int myvolume()
|
295
|
+
{
|
296
|
+
// if (vmax>-vmin) return vmax;
|
297
|
+
// return -vmin;
|
298
|
+
return vol>>8;
|
299
|
+
}
|
300
|
+
|
301
|
+
int audioRecVol(unsigned char* src,int len,int start)
|
302
|
+
{
|
303
|
+
int s0,st,i;
|
304
|
+
|
305
|
+
if (start+256>len) return 0;
|
306
|
+
src+=start;
|
307
|
+
s0=(src[0]&255)+((src[1]&255)<<8); src+=2;
|
308
|
+
st=(src[0]&255)+((src[1]&255)<<8); src+=2;
|
309
|
+
mydecodeinit(s0,st);
|
310
|
+
for(i=0;i<252;i++)
|
311
|
+
{
|
312
|
+
int c=(*(src++))&255;
|
313
|
+
mydecode(c&15);
|
314
|
+
mydecode((c>>4)&15);
|
315
|
+
}
|
316
|
+
return myvolume();
|
317
|
+
}
|
318
|
+
|
319
|
+
void adpcmdecode(unsigned char* src,char *dstc)
|
320
|
+
{
|
321
|
+
short* dst=(short*)dstc;
|
322
|
+
int s0,st,i;
|
323
|
+
s0=(src[0]&255)+((src[1]&255)<<8); src+=2;
|
324
|
+
st=(src[0]&255)+((src[1]&255)<<8); src+=2;
|
325
|
+
mydecodeinit(s0,st);
|
326
|
+
*(dst++)=s0;
|
327
|
+
for(i=0;i<252;i++)
|
328
|
+
{
|
329
|
+
int c=(*(src++))&255;
|
330
|
+
*(dst++)=mydecode(c&15);
|
331
|
+
*(dst++)=mydecode((c>>4)&15);
|
332
|
+
}
|
333
|
+
|
334
|
+
}
|
335
|
+
|
336
|
+
int myencode(int16_t pcm16)
|
337
|
+
{
|
338
|
+
int step,diff;
|
339
|
+
int predicedValue = PredictedValue;
|
340
|
+
int stepIndex = StepIndex;
|
341
|
+
|
342
|
+
int delta = pcm16-predicedValue;
|
343
|
+
unsigned int value;
|
344
|
+
if(delta>=0)
|
345
|
+
value = 0;
|
346
|
+
else
|
347
|
+
{
|
348
|
+
value = 8;
|
349
|
+
delta = -delta;
|
350
|
+
}
|
351
|
+
|
352
|
+
step = IMA_ADPCMStepTable[stepIndex];
|
353
|
+
diff = step>>3;
|
354
|
+
if(delta>step)
|
355
|
+
{
|
356
|
+
value |= 4;
|
357
|
+
delta -= step;
|
358
|
+
diff += step;
|
359
|
+
}
|
360
|
+
step >>= 1;
|
361
|
+
if(delta>step)
|
362
|
+
{
|
363
|
+
value |= 2;
|
364
|
+
delta -= step;
|
365
|
+
diff += step;
|
366
|
+
}
|
367
|
+
step >>= 1;
|
368
|
+
if(delta>step)
|
369
|
+
{
|
370
|
+
value |= 1;
|
371
|
+
diff += step;
|
372
|
+
}
|
373
|
+
|
374
|
+
if(value&8)
|
375
|
+
predicedValue -= diff;
|
376
|
+
else
|
377
|
+
predicedValue += diff;
|
378
|
+
if(predicedValue<-0x8000)
|
379
|
+
predicedValue = -0x8000;
|
380
|
+
else if(predicedValue>0x7fff)
|
381
|
+
predicedValue = 0x7fff;
|
382
|
+
PredictedValue = predicedValue;
|
383
|
+
|
384
|
+
stepIndex += IMA_ADPCMIndexTable[value&7];
|
385
|
+
if(stepIndex<0)
|
386
|
+
stepIndex = 0;
|
387
|
+
else if(stepIndex>88)
|
388
|
+
stepIndex = 88;
|
389
|
+
StepIndex = stepIndex;
|
390
|
+
|
391
|
+
return value&15;
|
392
|
+
}
|
393
|
+
|
394
|
+
void adpcmencode(short* src,char *dst)
|
395
|
+
{
|
396
|
+
int delta,stepIndex,i;
|
397
|
+
short sample1,sample2;
|
398
|
+
short* p;
|
399
|
+
|
400
|
+
sample1=src[0];
|
401
|
+
sample2=src[1];
|
402
|
+
|
403
|
+
PredictedValue = sample1;
|
404
|
+
delta = sample2-sample1;
|
405
|
+
if(delta<0)
|
406
|
+
delta = - delta;
|
407
|
+
if(delta>32767)
|
408
|
+
delta = 32767;
|
409
|
+
stepIndex = 0;
|
410
|
+
while(IMA_ADPCMStepTable[stepIndex]<(unsigned)delta)
|
411
|
+
stepIndex++;
|
412
|
+
StepIndex = stepIndex;
|
413
|
+
|
414
|
+
p=(short*)dst;
|
415
|
+
p[0]=sample1;
|
416
|
+
p[1]=StepIndex;
|
417
|
+
|
418
|
+
dst+=4;
|
419
|
+
for(i=1;i<505;i+=2)
|
420
|
+
{
|
421
|
+
*(dst++)=myencode(src[i])+(myencode(src[i+1])<<4);
|
422
|
+
}
|
423
|
+
}
|
424
|
+
|
425
|
+
const short alaw[256]={
|
426
|
+
-5504,-5248,-6016,-5760,-4480,-4224,-4992,-4736,-7552,-7296,-8064,-7808,-6528,-6272,-7040,-6784,-2752,-2624,-3008,-2880,-2240,-2112,-2496,-2368,-3776,-3648,-4032,-3904,-3264,-3136,-3520,-3392,
|
427
|
+
-22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,-30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,-11008,-10496,-12032,-11520,-8960,-8448,-9984,-9472,-15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
|
428
|
+
-344,-328,-376,-360,-280,-264,-312,-296,-472,-456,-504,-488,-408,-392,-440,-424,-88,-72,-120,-104,-24,-8,-56,-40,-216,-200,-248,-232,-152,-136,-184,-168,
|
429
|
+
-1376,-1312,-1504,-1440,-1120,-1056,-1248,-1184,-1888,-1824,-2016,-1952,-1632,-1568,-1760,-1696,-688,-656,-752,-720,-560,-528,-624,-592,-944,-912,-1008,-976,-816,-784,-880,-848,
|
430
|
+
5504,5248,6016,5760,4480,4224,4992,4736,7552,7296,8064,7808,6528,6272,7040,6784,2752,2624,3008,2880,2240,2112,2496,2368,3776,3648,4032,3904,3264,3136,3520,3392,
|
431
|
+
22016,20992,24064,23040,17920,16896,19968,18944,30208,29184,32256,31232,26112,25088,28160,27136,11008,10496,12032,11520,8960,8448,9984,9472,15104,14592,16128,15616,13056,12544,14080,13568,
|
432
|
+
344,328,376,360,280,264,312,296,472,456,504,488,408,392,440,424,88,72,120,104,24,8,56,40,216,200,248,232,152,136,184,168,
|
433
|
+
1376,1312,1504,1440,1120,1056,1248,1184,1888,1824,2016,1952,1632,1568,1760,1696,688,656,752,720,560,528,624,592,944,912,1008,976,816,784,880,848};
|
434
|
+
const short mulaw[256]={
|
435
|
+
-32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,-11900,-11388,-10876,-10364,-9852,-9340,-8828,-8316,
|
436
|
+
-7932,-7676,-7420,-7164,-6908,-6652,-6396,-6140,-5884,-5628,-5372,-5116,-4860,-4604,-4348,-4092,-3900,-3772,-3644,-3516,-3388,-3260,-3132,-3004,-2876,-2748,-2620,-2492,-2364,-2236,-2108,-1980,
|
437
|
+
-1884,-1820,-1756,-1692,-1628,-1564,-1500,-1436,-1372,-1308,-1244,-1180,-1116,-1052,-988,-924,-876,-844,-812,-780,-748,-716,-684,-652,-620,-588,-556,-524,-492,-460,-428,-396,
|
438
|
+
-372,-356,-340,-324,-308,-292,-276,-260,-244,-228,-212,-196,-180,-164,-148,-132,-120,-112,-104,-96,-88,-80,-72,-64,-56,-48,-40,-32,-24,-16,-8,0,
|
439
|
+
32124,31100,30076,29052,28028,27004,25980,24956,23932,22908,21884,20860,19836,18812,17788,16764,15996,15484,14972,14460,13948,13436,12924,12412,11900,11388,10876,10364,9852,9340,8828,8316,
|
440
|
+
7932,7676,7420,7164,6908,6652,6396,6140,5884,5628,5372,5116,4860,4604,4348,4092,3900,3772,3644,3516,3388,3260,3132,3004,2876,2748,2620,2492,2364,2236,2108,1980,
|
441
|
+
1884,1820,1756,1692,1628,1564,1500,1436,1372,1308,1244,1180,1116,1052,988,924,876,844,812,780,748,716,684,652,620,588,556,524,492,460,428,396,
|
442
|
+
372,356,340,324,308,292,276,260,244,228,212,196,180,164,148,132,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0};
|
443
|
+
const char exposant[256]={
|
444
|
+
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
445
|
+
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
446
|
+
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
447
|
+
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
448
|
+
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
449
|
+
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
450
|
+
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
451
|
+
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8};
|
452
|
+
|
453
|
+
|
454
|
+
char alawencode(int pcm)
|
455
|
+
{
|
456
|
+
int exponent,mantissa,sign;
|
457
|
+
char alaw;
|
458
|
+
|
459
|
+
sign = (pcm & 0x8000) >> 8;
|
460
|
+
if (sign != 0) pcm = -pcm;
|
461
|
+
//The magnitude must fit in 15 bits to avoid overflow
|
462
|
+
if (pcm > 32635) pcm = 32635;
|
463
|
+
|
464
|
+
exponent=exposant[pcm>>8];
|
465
|
+
|
466
|
+
// exponent = 7;
|
467
|
+
// for (expMask = 0x4000; (pcm & expMask) == 0 && exponent>0; exponent--, expMask >>= 1) { }
|
468
|
+
|
469
|
+
mantissa = (pcm >> ((exponent == 0) ? 4 : (exponent + 3))) & 0x0f;
|
470
|
+
|
471
|
+
//The a-law byte bit arrangement is SEEEMMMM
|
472
|
+
//(Sign, Exponent, and Mantissa.)
|
473
|
+
alaw = (char)(sign | exponent << 4 | mantissa);
|
474
|
+
|
475
|
+
//Last is to flip every other bit, and the sign bit (0xD5 = 1101 0101)
|
476
|
+
return (char)(alaw^0xD5);
|
477
|
+
}
|
478
|
+
|
479
|
+
char mulawencode(int pcm) //16-bit
|
480
|
+
{
|
481
|
+
int exponent,mantissa,sign;
|
482
|
+
char mulaw;
|
483
|
+
|
484
|
+
//Get the sign bit. Shift it for later
|
485
|
+
//use without further modification
|
486
|
+
sign = (pcm & 0x8000) >> 8;
|
487
|
+
//If the number is negative, make it
|
488
|
+
//positive (now it's a magnitude)
|
489
|
+
if (sign != 0) pcm = -pcm;
|
490
|
+
//The magnitude must be less than 32635 to avoid overflow
|
491
|
+
if (pcm > 32635) pcm = 32635;
|
492
|
+
//Add 132 to guarantee a 1 in
|
493
|
+
//the eight bits after the sign bit
|
494
|
+
pcm += 0x84;
|
495
|
+
|
496
|
+
/* Finding the "exponent"
|
497
|
+
* Bits:
|
498
|
+
* 1 2 3 4 5 6 7 8 9 A B C D E F G
|
499
|
+
* S 7 6 5 4 3 2 1 0 . . . . . . .
|
500
|
+
* We want to find where the first 1 after the sign bit is.
|
501
|
+
* We take the corresponding value from
|
502
|
+
* the second row as the exponent value.
|
503
|
+
* (i.e. if first 1 at position 7 -> exponent = 2) */
|
504
|
+
// exponent = 7;
|
505
|
+
//Move to the right and decrement exponent until we hit the 1
|
506
|
+
exponent=exposant[pcm>>8];
|
507
|
+
// for (expMask = 0x4000; (pcm & expMask) == 0; exponent--, expMask >>= 1) { }
|
508
|
+
|
509
|
+
/* The last part - the "mantissa"
|
510
|
+
* We need to take the four bits after the 1 we just found.
|
511
|
+
* To get it, we shift 0x0f :
|
512
|
+
* 1 2 3 4 5 6 7 8 9 A B C D E F G
|
513
|
+
* S 0 0 0 0 0 1 . . . . . . . . . (meaning exponent is 2)
|
514
|
+
* . . . . . . . . . . . . 1 1 1 1
|
515
|
+
* We shift it 5 times for an exponent of two, meaning
|
516
|
+
* we will shift our four bits (exponent + 3) bits.
|
517
|
+
* For convenience, we will actually just shift
|
518
|
+
* the number, then and with 0x0f. */
|
519
|
+
mantissa = (pcm >> (exponent + 3)) & 0x0f;
|
520
|
+
|
521
|
+
//The mu-law byte bit arrangement
|
522
|
+
//is SEEEMMMM (Sign, Exponent, and Mantissa.)
|
523
|
+
mulaw = (char)(sign | exponent << 4 | mantissa);
|
524
|
+
|
525
|
+
//Last is to flip the bits
|
526
|
+
return (char)~mulaw;
|
527
|
+
}
|
528
|
+
|
529
|
+
short alawdecode(unsigned char v)
|
530
|
+
{
|
531
|
+
return alaw[v];
|
532
|
+
}
|
533
|
+
|
534
|
+
short mulawdecode(unsigned char v)
|
535
|
+
{
|
536
|
+
return mulaw[v];
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
void AudioAdp2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len)
|
541
|
+
{
|
542
|
+
if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return;
|
543
|
+
if (len&255) return; // que des blocs de 256
|
544
|
+
if ((len>lsrc-isrc)||(((len>>8)*505*2)>ldst-idst)) return;
|
545
|
+
// printf("a2w%d\n",len);
|
546
|
+
src+=isrc;
|
547
|
+
dst+=idst;
|
548
|
+
while(len>0)
|
549
|
+
{
|
550
|
+
adpcmdecode((unsigned char*)src,dst);
|
551
|
+
src+=256;
|
552
|
+
dst+=505*2;
|
553
|
+
len-=256;
|
554
|
+
}
|
555
|
+
}
|
556
|
+
|
557
|
+
void AudioWav2adp(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len)
|
558
|
+
{
|
559
|
+
int n=0;
|
560
|
+
int l=len;
|
561
|
+
|
562
|
+
if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return;
|
563
|
+
while(l>0)
|
564
|
+
{
|
565
|
+
l-=505*2;
|
566
|
+
n++;
|
567
|
+
}
|
568
|
+
if (l) return;// que des blocs de 505 �chantillons 16 bits
|
569
|
+
|
570
|
+
if ((len>lsrc-isrc)||((n<<8)>ldst-idst)) return;
|
571
|
+
// printf("w2a%d\n",len);
|
572
|
+
src+=isrc;
|
573
|
+
dst+=idst;
|
574
|
+
while(len>0)
|
575
|
+
{
|
576
|
+
adpcmencode((short*)src,dst);
|
577
|
+
src+=505*2;
|
578
|
+
dst+=256;
|
579
|
+
len-=505*2;
|
580
|
+
}
|
581
|
+
}
|
582
|
+
|
583
|
+
void AudioWav2alaw(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu)
|
584
|
+
{
|
585
|
+
short* p;
|
586
|
+
|
587
|
+
if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return;
|
588
|
+
if (len&1) return;
|
589
|
+
|
590
|
+
if ((len>lsrc-isrc)||((len>>1)>ldst-idst)) return;
|
591
|
+
// printf("w2l%d\n",len);
|
592
|
+
src+=isrc;
|
593
|
+
dst+=idst;
|
594
|
+
p=(short*)src;
|
595
|
+
len>>=1;
|
596
|
+
if (mu)
|
597
|
+
{
|
598
|
+
while(len--) *(dst++)=mulawencode(*(p++));
|
599
|
+
}
|
600
|
+
else
|
601
|
+
{
|
602
|
+
while(len--) *(dst++)=alawencode(*(p++));
|
603
|
+
}
|
604
|
+
}
|
605
|
+
|
606
|
+
void AudioAlaw2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu)
|
607
|
+
{
|
608
|
+
short* p;
|
609
|
+
unsigned char* q;
|
610
|
+
|
611
|
+
if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc)) return;
|
612
|
+
|
613
|
+
if ((len>lsrc-isrc)||(len+len>ldst-idst)) return;
|
614
|
+
// printf("l2w%d\n",len);
|
615
|
+
src+=isrc;
|
616
|
+
dst+=idst;
|
617
|
+
p=(short*)dst;
|
618
|
+
q=(unsigned char*)src;
|
619
|
+
if (mu)
|
620
|
+
{
|
621
|
+
while(len--) *(p++)=mulaw[*(q++)];
|
622
|
+
}
|
623
|
+
else
|
624
|
+
{
|
625
|
+
while(len--) *(p++)=alaw[*(q++)];
|
626
|
+
}
|
627
|
+
}
|
628
|
+
|
629
|
+
void audioWrite(int reg,int val)
|
630
|
+
{
|
631
|
+
#ifdef VSIMU
|
632
|
+
printf("xxxx audioWrite %d %d\n",reg,val);
|
633
|
+
#endif
|
634
|
+
#ifdef VREAL
|
635
|
+
vlsi_write_sci(reg,val);
|
636
|
+
#endif
|
637
|
+
}
|
638
|
+
int audioRead(int reg)
|
639
|
+
{
|
640
|
+
#ifdef VSIMU
|
641
|
+
printf("xxxx audioRead %d\n",reg);
|
642
|
+
return 0;
|
643
|
+
#endif
|
644
|
+
#ifdef VREAL
|
645
|
+
if (reg==-1) return vlsi_fifo_ready();
|
646
|
+
return vlsi_read_sci(reg);
|
647
|
+
#endif
|
648
|
+
}
|
649
|
+
int audioFeed(char *src,int len)
|
650
|
+
{
|
651
|
+
#ifdef VSIMU
|
652
|
+
printf("xxxx audioFeed %d\n",len);
|
653
|
+
return 0;
|
654
|
+
#endif
|
655
|
+
#ifdef VREAL
|
656
|
+
return vlsi_feed_sdi((uchar*)src,len);
|
657
|
+
#endif
|
658
|
+
}
|
659
|
+
void audioRefresh()
|
660
|
+
{
|
661
|
+
#ifdef VSIMU
|
662
|
+
printf("xxxx audioRefresh\n");
|
663
|
+
#endif
|
664
|
+
#ifdef VREAL
|
665
|
+
play_check(0);
|
666
|
+
rec_check();
|
667
|
+
#endif
|
668
|
+
}
|
669
|
+
void audioAmpli(int on)
|
670
|
+
{
|
671
|
+
#ifdef VSIMU
|
672
|
+
printf("xxxx audioAmpli %d\n",on);
|
673
|
+
#endif
|
674
|
+
#ifdef VREAL
|
675
|
+
vlsi_ampli(on);
|
676
|
+
#endif
|
677
|
+
}
|
data/ext/mtl/vaudio.h
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
// VLISP Virtual Machine - 2006 - by Sylvain Huet
|
2
|
+
// Lowcost IS Powerfull
|
3
|
+
|
4
|
+
#ifndef _VAUDIO_
|
5
|
+
#define _VAUDIO_
|
6
|
+
|
7
|
+
#define AUDIO_FIFOPLAY 4096
|
8
|
+
extern char audioFifoPlay[AUDIO_FIFOPLAY];
|
9
|
+
|
10
|
+
void audioInit();
|
11
|
+
|
12
|
+
void audioPlayStart(int freq,int bps,int stereo,int trytofeed);
|
13
|
+
int audioPlayFeed(char *src,int len);
|
14
|
+
void audioPlayStop();
|
15
|
+
int audioPlayTryFeed(int ask);
|
16
|
+
int audioPlayFetchByte();
|
17
|
+
int audioPlayFetch(char* dst,int ask);
|
18
|
+
|
19
|
+
void audioVol(int vol);
|
20
|
+
int audioPlayTime();
|
21
|
+
|
22
|
+
|
23
|
+
int audioRecStart(int freq,int gain);
|
24
|
+
int audioRecStop();
|
25
|
+
int audioRecVol(unsigned char* src,int len,int start);
|
26
|
+
char* audioRecFeed_begin(int size);
|
27
|
+
void audioRecFeed_end();
|
28
|
+
|
29
|
+
void adpcmdecode(unsigned char* src,char *dstc);
|
30
|
+
void adpcmencode(short* src,char *dst);
|
31
|
+
|
32
|
+
void AudioAdp2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len);
|
33
|
+
void AudioWav2adp(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len);
|
34
|
+
void AudioWav2alaw(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu);
|
35
|
+
void AudioAlaw2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu);
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
void audioWrite(int reg,int val);
|
40
|
+
int audioRead(int reg);
|
41
|
+
int audioFeed(char *src,int len);
|
42
|
+
void audioRefresh();
|
43
|
+
void audioAmpli(int on);
|
44
|
+
|
45
|
+
#endif
|
46
|
+
|