nabaztag_hack_kit 0.1.0.beta3 → 0.1.0.beta8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -2
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +2 -2
  5. data/CHANGELOG.md +17 -2
  6. data/LICENSE +21 -0
  7. data/README.md +115 -44
  8. data/Rakefile +2 -6
  9. data/bytecode/{Commands.md → _docs/commands.md} +0 -0
  10. data/bytecode/{Docs.md → _docs/grammar.md} +0 -0
  11. data/bytecode/_docs/grammar.pdf +0 -0
  12. data/bytecode/{src → _original}/README.md +0 -0
  13. data/bytecode/{src → _original}/lib/arp.mtl +0 -0
  14. data/bytecode/{src → _original}/lib/cfg.mtl +0 -0
  15. data/bytecode/{src → _original}/lib/choreos.mtl +0 -0
  16. data/bytecode/{src → _original}/lib/dhcp.mtl +0 -0
  17. data/bytecode/{src → _original}/lib/dns.mtl +0 -0
  18. data/bytecode/{src → _original}/lib/http.mtl +0 -0
  19. data/bytecode/{src → _original}/lib/tcp.mtl +0 -0
  20. data/bytecode/{src → _original}/lib/udp.mtl +0 -0
  21. data/bytecode/{src → _original}/lib/util.mtl +0 -0
  22. data/bytecode/{src → _original}/lib/var.mtl +0 -0
  23. data/bytecode/{src → _original}/lib/wav.mtl +0 -0
  24. data/bytecode/{src → _original}/lib/wifi.mtl +0 -0
  25. data/bytecode/{src → _original}/nominal-ping.mtl +0 -0
  26. data/bytecode/main.mtl +11 -11
  27. data/bytecode/test/test.mtl +8 -8
  28. data/examples/basic/Gemfile +1 -3
  29. data/examples/basic/config.ru +14 -4
  30. data/examples/record/Gemfile +0 -5
  31. data/examples/record/{config.ru.example → config.ru} +2 -2
  32. data/examples/record/server.rb +0 -2
  33. data/ext/Makefile +10 -0
  34. data/ext/bin/mtl_comp +29 -0
  35. data/{bytecode → ext}/bin/mtl_merge +0 -0
  36. data/ext/bin/mtl_simu +29 -0
  37. data/{bytecode/src/mtl → ext}/extconf.rb +0 -0
  38. data/ext/mtl_linux/.gitignore +7 -0
  39. data/ext/mtl_linux/LICENSE +21 -0
  40. data/ext/mtl_linux/Makefile +116 -0
  41. data/ext/mtl_linux/MetalC.project +196 -0
  42. data/ext/mtl_linux/conf.bin +0 -0
  43. data/{bytecode/src/mtl → ext/mtl_linux}/conf.bin.sans_password +0 -0
  44. data/{bytecode/src/mtl → ext/mtl_linux}/config.txt +0 -0
  45. data/ext/mtl_linux/inc/log.h +21 -0
  46. data/{bytecode/src/mtl → ext/mtl_linux/inc}/properties.h +0 -0
  47. data/{bytecode/src/mtl/linux_simuaudio.h → ext/mtl_linux/inc/simu/linux/simuaudio.h} +0 -0
  48. data/{bytecode/src/mtl/linux_simunet.h → ext/mtl_linux/inc/simu/linux/simunet.h} +0 -0
  49. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simuaudio.h +18 -18
  50. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simunet.h +0 -0
  51. data/ext/mtl_linux/inc/vcomp/compiler.h +200 -0
  52. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/file.h +11 -11
  53. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/filesystem.h +0 -0
  54. data/ext/mtl_linux/inc/vcomp/interpreter.h +121 -0
  55. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/memory.h +120 -92
  56. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/param.h +0 -0
  57. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/parser.h +27 -27
  58. data/ext/mtl_linux/inc/vcomp/prodbuffer.h +42 -0
  59. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/resource.h +1 -1
  60. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/terminal.h +8 -8
  61. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/util.h +7 -7
  62. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vaudio.h +0 -0
  63. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vbc.h +1 -1
  64. data/ext/mtl_linux/inc/vm/vbc_str.h +167 -0
  65. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vinterp.h +0 -0
  66. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vloader.h +0 -0
  67. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vlog.h +0 -0
  68. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vmem.h +0 -0
  69. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vnet.h +0 -0
  70. data/ext/mtl_linux/nominal.mtl +3959 -0
  71. data/ext/mtl_linux/src/comp/main.cpp +123 -0
  72. data/ext/mtl_linux/src/simu/dumpbc.c +2566 -0
  73. data/ext/mtl_linux/src/simu/linux/main.cpp +288 -0
  74. data/ext/mtl_linux/src/simu/linux/simu.c +309 -0
  75. data/ext/mtl_linux/src/simu/linux/simuaudio.c +44 -0
  76. data/ext/mtl_linux/src/simu/linux/simunet.c +707 -0
  77. data/ext/mtl_linux/src/simu/log.c +340 -0
  78. data/ext/mtl_linux/src/simu/properties.c +368 -0
  79. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/GTKANAL.H +0 -0
  80. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/LAYER3.C +0 -0
  81. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/TABINIT.C +0 -0
  82. data/ext/mtl_linux/src/simu/win/mp3/common.c +302 -0
  83. data/ext/mtl_linux/src/simu/win/mp3/dct64_i386.c +316 -0
  84. data/ext/mtl_linux/src/simu/win/mp3/decode_i386.c +160 -0
  85. data/ext/mtl_linux/src/simu/win/mp3/huffman.h +332 -0
  86. data/ext/mtl_linux/src/simu/win/mp3/interface.c +276 -0
  87. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/mpg123.h +34 -30
  88. data/ext/mtl_linux/src/simu/win/mp3/mpglib.h +47 -0
  89. data/ext/mtl_linux/src/simu/win/simu.c +819 -0
  90. data/ext/mtl_linux/src/simu/win/simuaudio.c +787 -0
  91. data/ext/mtl_linux/src/simu/win/simunet.c +489 -0
  92. data/ext/mtl_linux/src/vcomp/bootstrap.cpp +95 -0
  93. data/ext/mtl_linux/src/vcomp/compiler.cpp +587 -0
  94. data/ext/mtl_linux/src/vcomp/compiler_file.cpp +1253 -0
  95. data/ext/mtl_linux/src/vcomp/compiler_prog.cpp +437 -0
  96. data/ext/mtl_linux/src/vcomp/compiler_term.cpp +1500 -0
  97. data/ext/mtl_linux/src/vcomp/compiler_type.cpp +1238 -0
  98. data/ext/mtl_linux/src/vcomp/compiler_var.cpp +502 -0
  99. data/ext/mtl_linux/src/vcomp/file.cpp +100 -0
  100. data/ext/mtl_linux/src/vcomp/interpreter.cpp +93 -0
  101. data/ext/mtl_linux/src/vcomp/memory.cpp +313 -0
  102. data/ext/mtl_linux/src/vcomp/parser.cpp +584 -0
  103. data/ext/mtl_linux/src/vcomp/parser_xml.cpp +137 -0
  104. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/prodbuffer.cpp +57 -36
  105. data/ext/mtl_linux/src/vcomp/stdlib_core.cpp +130 -0
  106. data/ext/mtl_linux/src/vcomp/terminal.cpp +80 -0
  107. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/util.cpp +18 -11
  108. data/ext/mtl_linux/src/vm/vaudio.c +830 -0
  109. data/ext/mtl_linux/src/vm/vinterp.c +1780 -0
  110. data/ext/mtl_linux/src/vm/vloader.c +139 -0
  111. data/ext/mtl_linux/src/vm/vlog.c +782 -0
  112. data/ext/mtl_linux/src/vm/vmem.c +461 -0
  113. data/ext/mtl_linux/src/vm/vnet.c +298 -0
  114. data/{bytecode/src/mtl → ext/mtl_linux}/utils/correct_const.sh +0 -0
  115. data/lib/nabaztag_hack_kit/server.rb +4 -7
  116. data/lib/nabaztag_hack_kit/version.rb +1 -1
  117. data/lib/nabaztag_hack_kit.rb +2 -2
  118. data/nabaztag_hack_kit.gemspec +10 -11
  119. data/spec/integration/server_spec.rb +3 -3
  120. data/spec/spec_helper.rb +2 -0
  121. data/spec/unit/message_spec.rb +12 -18
  122. data/spec/unit/server_spec.rb +13 -10
  123. metadata +145 -137
  124. data/bytecode/bin/mtl_comp +0 -50
  125. data/bytecode/bin/mtl_simu +0 -49
  126. data/bytecode/src/mtl/Makefile +0 -42
  127. data/bytecode/src/mtl/README.md +0 -13
  128. data/bytecode/src/mtl/bc.cpp +0 -1891
  129. data/bytecode/src/mtl/dumpbc.c +0 -2566
  130. data/bytecode/src/mtl/linux_simu.c +0 -271
  131. data/bytecode/src/mtl/linux_simuaudio.c +0 -16
  132. data/bytecode/src/mtl/linux_simunet.c +0 -620
  133. data/bytecode/src/mtl/log.c +0 -297
  134. data/bytecode/src/mtl/log.h +0 -20
  135. data/bytecode/src/mtl/main_compiler.cpp +0 -104
  136. data/bytecode/src/mtl/main_simu.cpp +0 -221
  137. data/bytecode/src/mtl/mp3/common.c +0 -265
  138. data/bytecode/src/mtl/mp3/dct64_i386.c +0 -316
  139. data/bytecode/src/mtl/mp3/decode_i386.c +0 -155
  140. data/bytecode/src/mtl/mp3/huffman.h +0 -332
  141. data/bytecode/src/mtl/mp3/interface.c +0 -258
  142. data/bytecode/src/mtl/mp3/mpglib.h +0 -44
  143. data/bytecode/src/mtl/properties.c +0 -293
  144. data/bytecode/src/mtl/simu.c +0 -750
  145. data/bytecode/src/mtl/simuaudio.c +0 -662
  146. data/bytecode/src/mtl/simunet.c +0 -400
  147. data/bytecode/src/mtl/vaudio.c +0 -677
  148. data/bytecode/src/mtl/vbc_str.h +0 -166
  149. data/bytecode/src/mtl/vcomp/Makefile +0 -29
  150. data/bytecode/src/mtl/vcomp/bootstrap.cpp +0 -89
  151. data/bytecode/src/mtl/vcomp/compiler.cpp +0 -470
  152. data/bytecode/src/mtl/vcomp/compiler.h +0 -200
  153. data/bytecode/src/mtl/vcomp/compiler_file.cpp +0 -929
  154. data/bytecode/src/mtl/vcomp/compiler_prog.cpp +0 -250
  155. data/bytecode/src/mtl/vcomp/compiler_term.cpp +0 -1053
  156. data/bytecode/src/mtl/vcomp/compiler_type.cpp +0 -872
  157. data/bytecode/src/mtl/vcomp/compiler_var.cpp +0 -289
  158. data/bytecode/src/mtl/vcomp/file.cpp +0 -79
  159. data/bytecode/src/mtl/vcomp/interpreter.cpp +0 -85
  160. data/bytecode/src/mtl/vcomp/interpreter.h +0 -121
  161. data/bytecode/src/mtl/vcomp/memory.cpp +0 -241
  162. data/bytecode/src/mtl/vcomp/parser.cpp +0 -427
  163. data/bytecode/src/mtl/vcomp/parser_xml.cpp +0 -124
  164. data/bytecode/src/mtl/vcomp/prodbuffer.h +0 -42
  165. data/bytecode/src/mtl/vcomp/stdlib_core.cpp +0 -122
  166. data/bytecode/src/mtl/vcomp/terminal.cpp +0 -73
  167. data/bytecode/src/mtl/vinterp.c +0 -1349
  168. data/bytecode/src/mtl/vloader.c +0 -127
  169. data/bytecode/src/mtl/vlog.c +0 -589
  170. data/bytecode/src/mtl/vmem.c +0 -424
  171. data/bytecode/src/mtl/vnet.c +0 -255
  172. data/examples/basic/Procfile +0 -2
  173. data/examples/basic/server.rb +0 -14
@@ -0,0 +1,830 @@
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"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)
62
+ {
63
+ i_end=AUDIO_FIFOPLAY+play_r-1;
64
+ }
65
+ if (play_w+len>=i_end)
66
+ {
67
+ len=i_end-play_w;
68
+ }
69
+ else
70
+ {
71
+ i_end=play_w+len;
72
+ }
73
+
74
+ if (i_end<=play_w)
75
+ {
76
+ return 0;
77
+ }
78
+ if (i_end<AUDIO_FIFOPLAY)
79
+ {
80
+ len=i_end-play_w;
81
+ memcpy(audioFifoPlay+play_w,src,len);
82
+ play_w=i_end;
83
+ return len;
84
+ }
85
+ len=AUDIO_FIFOPLAY-play_w;
86
+ memcpy(audioFifoPlay+play_w,src,len);
87
+ src+=len;
88
+ play_w=0;
89
+ i_end-=AUDIO_FIFOPLAY;
90
+ if (i_end)
91
+ {
92
+ memcpy(audioFifoPlay,src,i_end);
93
+ }
94
+ play_w=i_end;
95
+ return len+i_end;
96
+ //#endif
97
+ /*
98
+ #ifdef VREAL
99
+ set_vlsi_volume(get_adc_value()/2); //volume on 8bits, 0x00 => maximum
100
+ //Play file
101
+ play_audio((UBYTE*)src,len);
102
+ #endif
103
+ */
104
+ }
105
+
106
+ void audioPlayStop()
107
+ {
108
+ #ifdef VSIMU
109
+ PlayStop();
110
+ #endif
111
+ #ifdef VREAL
112
+ play_stop();
113
+ #endif
114
+ }
115
+
116
+ int audioPlayTryFeed(int ask)
117
+ {
118
+ int lastw;
119
+ int play_w0=play_w;
120
+ int dispo=play_w-play_r;
121
+ if (dispo<0)
122
+ {
123
+ dispo+=AUDIO_FIFOPLAY;
124
+ }
125
+ if (dispo>=ask)
126
+ {
127
+ return 0;
128
+ }
129
+ do
130
+ {
131
+ lastw=play_w;
132
+ dispo=play_w-play_r;
133
+ if (dispo<0)
134
+ {
135
+ dispo+=AUDIO_FIFOPLAY;
136
+ }
137
+
138
+ VPUSH(INTTOVAL(AUDIO_FIFOPLAY-dispo));
139
+ VPUSH(VCALLSTACKGET(sys_start,SYS_CBPLAY));
140
+ if (VSTACKGET(0)==NIL)
141
+ {
142
+ VPULL();
143
+ VPULL();
144
+ return 0;
145
+ }
146
+ interpGo();
147
+ VPULL();
148
+ }
149
+ while(play_w!=lastw);
150
+ if (play_w!=play_w0)
151
+ {
152
+ return 1;
153
+ }
154
+ return 0;
155
+ }
156
+
157
+
158
+ int audioPlayFetchByte()
159
+ {
160
+ int res;
161
+ if (play_w==play_r)
162
+ {
163
+ return -1;
164
+ }
165
+ res=audioFifoPlay[play_r++]&255;
166
+ if (play_r>=AUDIO_FIFOPLAY)
167
+ {
168
+ play_r-=AUDIO_FIFOPLAY;
169
+ }
170
+ return res;
171
+ }
172
+
173
+ int audioPlayFetch(char* dst,int ask)
174
+ {
175
+ int n=0;
176
+ int c;
177
+ audioPlayTryFeed(ask);
178
+ while(n<ask)
179
+ {
180
+ c=audioPlayFetchByte();
181
+ if (c<0)
182
+ {
183
+ return n;
184
+ }
185
+ dst[n++]=c;
186
+ }
187
+ return n;
188
+ }
189
+
190
+ void audioVol(int vol)
191
+ {
192
+ #ifdef VSIMU
193
+ // printf("xxxx audioVol %d\n",vol);
194
+
195
+ audioSetVolume(255-vol);
196
+ #endif
197
+ #ifdef VREAL
198
+ set_vlsi_volume((vol&255)/2);
199
+ #endif
200
+ }
201
+
202
+ int audioPlayTime()
203
+ {
204
+ #ifdef VSIMU
205
+ printf("xxxx audioPlayTime\n");
206
+ return 0;
207
+ #endif
208
+ #ifdef VREAL
209
+ return check_decode_time();
210
+ #endif
211
+ }
212
+
213
+ int audioRecStart(int freq,int gain)
214
+ {
215
+ #ifdef VSIMU
216
+ RecStart(freq,505,4);
217
+ #endif
218
+ #ifdef VREAL
219
+ rec_start(freq,gain);
220
+ #endif
221
+ return 0;
222
+ }
223
+
224
+ int audioRecStop()
225
+ {
226
+ #ifdef VSIMU
227
+ RecStop();
228
+ #endif
229
+ #ifdef VREAL
230
+ rec_stop();
231
+ #endif
232
+ return 0;
233
+ }
234
+
235
+ char* audioRecFeed_begin(int size)
236
+ {
237
+ VPUSH(PNTTOVAL(VMALLOCBIN(size)));
238
+ return (char*)VSTARTBIN(VALTOPNT(VSTACKGET(0)));
239
+ }
240
+
241
+ void audioRecFeed_end()
242
+ {
243
+ VPUSH(VCALLSTACKGET(sys_start,SYS_CBREC));
244
+ if (VSTACKGET(0)!=NIL)
245
+ {
246
+ interpGo();
247
+ }
248
+ else
249
+ {
250
+ VPULL();
251
+ }
252
+ VPULL();
253
+ }
254
+
255
+
256
+ #define uint16_t unsigned short
257
+ #define int16_t short
258
+ #define uint8_t unsigned char
259
+ #define int8_t char
260
+
261
+ static const uint16_t IMA_ADPCMStepTable[89] =
262
+ {
263
+ 7, 8, 9, 10, 11, 12, 13, 14,
264
+ 16, 17, 19, 21, 23, 25, 28, 31,
265
+ 34, 37, 41, 45, 50, 55, 60, 66,
266
+ 73, 80, 88, 97, 107, 118, 130, 143,
267
+ 157, 173, 190, 209, 230, 253, 279, 307,
268
+ 337, 371, 408, 449, 494, 544, 598, 658,
269
+ 724, 796, 876, 963, 1060, 1166, 1282, 1411,
270
+ 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
271
+ 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
272
+ 7132, 7845, 8630, 9493,10442,11487,12635,13899,
273
+ 15289,16818,18500,20350,22385,24623,27086,29794,
274
+ 32767
275
+ };
276
+
277
+
278
+ static const int IMA_ADPCMIndexTable[8] =
279
+ {
280
+ -1, -1, -1, -1, 2, 4, 6, 8,
281
+ };
282
+
283
+
284
+ int16_t PredictedValue;
285
+ uint8_t StepIndex;
286
+ int16_t vmin,vmax;
287
+ int vol;
288
+
289
+ void mydecodeinit(int s0,int st)
290
+ {
291
+ PredictedValue=s0;
292
+ StepIndex=st;
293
+ vmin=0;
294
+ vmax=0;
295
+ vol=0;
296
+ }
297
+
298
+ int mydecode(unsigned int adpcm)
299
+ {
300
+ int diff;
301
+ int predicedValue;
302
+ int stepIndex = StepIndex;
303
+ int step = IMA_ADPCMStepTable[stepIndex];
304
+
305
+ stepIndex += IMA_ADPCMIndexTable[adpcm&7];
306
+ if(stepIndex<0)
307
+ {
308
+ stepIndex = 0;
309
+ }
310
+ else if(stepIndex>88)
311
+ {
312
+ stepIndex = 88;
313
+ }
314
+ StepIndex = stepIndex;
315
+
316
+ diff = step>>3;
317
+ if(adpcm&4)
318
+ {
319
+ diff += step;
320
+ }
321
+ if(adpcm&2)
322
+ {
323
+ diff += step>>1;
324
+ }
325
+ if(adpcm&1)
326
+ {
327
+ diff += step>>2;
328
+ }
329
+
330
+ predicedValue = PredictedValue;
331
+ if(adpcm&8)
332
+ {
333
+ predicedValue -= diff;
334
+ }
335
+ else
336
+ {
337
+ predicedValue += diff;
338
+ }
339
+ if(predicedValue<-0x8000)
340
+ {
341
+ predicedValue = -0x8000;
342
+ }
343
+ else if(predicedValue>0x7fff)
344
+ {
345
+ predicedValue = 0x7fff;
346
+ }
347
+ PredictedValue = predicedValue;
348
+ // if (predicedValue>vmax) vmax=predicedValue;
349
+ // if (predicedValue<vmin) vmin=predicedValue;
350
+ vol+=(predicedValue*predicedValue)>>16;
351
+ return predicedValue;
352
+ }
353
+
354
+ int myvolume()
355
+ {
356
+ // if (vmax>-vmin) return vmax;
357
+ // return -vmin;
358
+ return vol>>8;
359
+ }
360
+
361
+ int audioRecVol(unsigned char* src,int len,int start)
362
+ {
363
+ int s0,st,i;
364
+
365
+ if (start+256>len)
366
+ {
367
+ return 0;
368
+ }
369
+ src+=start;
370
+ s0=(src[0]&255)+((src[1]&255)<<8);
371
+ src+=2;
372
+ st=(src[0]&255)+((src[1]&255)<<8);
373
+ src+=2;
374
+ mydecodeinit(s0,st);
375
+ for(i=0; i<252; i++)
376
+ {
377
+ int c=(*(src++))&255;
378
+ mydecode(c&15);
379
+ mydecode((c>>4)&15);
380
+ }
381
+ return myvolume();
382
+ }
383
+
384
+ void adpcmdecode(unsigned char* src,char *dstc)
385
+ {
386
+ short* dst=(short*)dstc;
387
+ int s0,st,i;
388
+ s0=(src[0]&255)+((src[1]&255)<<8);
389
+ src+=2;
390
+ st=(src[0]&255)+((src[1]&255)<<8);
391
+ src+=2;
392
+ mydecodeinit(s0,st);
393
+ *(dst++)=s0;
394
+ for(i=0; i<252; i++)
395
+ {
396
+ int c=(*(src++))&255;
397
+ *(dst++)=mydecode(c&15);
398
+ *(dst++)=mydecode((c>>4)&15);
399
+ }
400
+
401
+ }
402
+
403
+ int myencode(int16_t pcm16)
404
+ {
405
+ int step,diff;
406
+ int predicedValue = PredictedValue;
407
+ int stepIndex = StepIndex;
408
+
409
+ int delta = pcm16-predicedValue;
410
+ unsigned int value;
411
+ if(delta>=0)
412
+ {
413
+ value = 0;
414
+ }
415
+ else
416
+ {
417
+ value = 8;
418
+ delta = -delta;
419
+ }
420
+
421
+ step = IMA_ADPCMStepTable[stepIndex];
422
+ diff = step>>3;
423
+ if(delta>step)
424
+ {
425
+ value |= 4;
426
+ delta -= step;
427
+ diff += step;
428
+ }
429
+ step >>= 1;
430
+ if(delta>step)
431
+ {
432
+ value |= 2;
433
+ delta -= step;
434
+ diff += step;
435
+ }
436
+ step >>= 1;
437
+ if(delta>step)
438
+ {
439
+ value |= 1;
440
+ diff += step;
441
+ }
442
+
443
+ if(value&8)
444
+ {
445
+ predicedValue -= diff;
446
+ }
447
+ else
448
+ {
449
+ predicedValue += diff;
450
+ }
451
+ if(predicedValue<-0x8000)
452
+ {
453
+ predicedValue = -0x8000;
454
+ }
455
+ else if(predicedValue>0x7fff)
456
+ {
457
+ predicedValue = 0x7fff;
458
+ }
459
+ PredictedValue = predicedValue;
460
+
461
+ stepIndex += IMA_ADPCMIndexTable[value&7];
462
+ if(stepIndex<0)
463
+ {
464
+ stepIndex = 0;
465
+ }
466
+ else if(stepIndex>88)
467
+ {
468
+ stepIndex = 88;
469
+ }
470
+ StepIndex = stepIndex;
471
+
472
+ return value&15;
473
+ }
474
+
475
+ void adpcmencode(short* src,char *dst)
476
+ {
477
+ int delta,stepIndex,i;
478
+ short sample1,sample2;
479
+ short* p;
480
+
481
+ sample1=src[0];
482
+ sample2=src[1];
483
+
484
+ PredictedValue = sample1;
485
+ delta = sample2-sample1;
486
+ if(delta<0)
487
+ {
488
+ delta = - delta;
489
+ }
490
+ if(delta>32767)
491
+ {
492
+ delta = 32767;
493
+ }
494
+ stepIndex = 0;
495
+ while(IMA_ADPCMStepTable[stepIndex]<(unsigned)delta)
496
+ {
497
+ stepIndex++;
498
+ }
499
+ StepIndex = stepIndex;
500
+
501
+ p=(short*)dst;
502
+ p[0]=sample1;
503
+ p[1]=StepIndex;
504
+
505
+ dst+=4;
506
+ for(i=1; i<505; i+=2)
507
+ {
508
+ *(dst++)=myencode(src[i])+(myencode(src[i+1])<<4);
509
+ }
510
+ }
511
+
512
+ const short alaw[256]=
513
+ {
514
+ -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,
515
+ -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,
516
+ -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,
517
+ -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,
518
+ 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,
519
+ 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,
520
+ 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,
521
+ 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
522
+ };
523
+ const short mulaw[256]=
524
+ {
525
+ -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,
526
+ -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,
527
+ -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,
528
+ -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,
529
+ 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,
530
+ 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,
531
+ 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,
532
+ 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
533
+ };
534
+ const char exposant[256]=
535
+ {
536
+ 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,
537
+ 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,
538
+ 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,
539
+ 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,
540
+ 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,
541
+ 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,
542
+ 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,
543
+ 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
544
+ };
545
+
546
+
547
+ char alawencode(int pcm)
548
+ {
549
+ int exponent,mantissa,sign;
550
+ char alaw;
551
+
552
+ sign = (pcm & 0x8000) >> 8;
553
+ if (sign != 0)
554
+ {
555
+ pcm = -pcm;
556
+ }
557
+ //The magnitude must fit in 15 bits to avoid overflow
558
+ if (pcm > 32635)
559
+ {
560
+ pcm = 32635;
561
+ }
562
+
563
+ exponent=exposant[pcm>>8];
564
+
565
+ // exponent = 7;
566
+ // for (expMask = 0x4000; (pcm & expMask) == 0 && exponent>0; exponent--, expMask >>= 1) { }
567
+
568
+ mantissa = (pcm >> ((exponent == 0) ? 4 : (exponent + 3))) & 0x0f;
569
+
570
+ //The a-law byte bit arrangement is SEEEMMMM
571
+ //(Sign, Exponent, and Mantissa.)
572
+ alaw = (char)(sign | exponent << 4 | mantissa);
573
+
574
+ //Last is to flip every other bit, and the sign bit (0xD5 = 1101 0101)
575
+ return (char)(alaw^0xD5);
576
+ }
577
+
578
+ char mulawencode(int pcm) //16-bit
579
+ {
580
+ int exponent,mantissa,sign;
581
+ char mulaw;
582
+
583
+ //Get the sign bit. Shift it for later
584
+ //use without further modification
585
+ sign = (pcm & 0x8000) >> 8;
586
+ //If the number is negative, make it
587
+ //positive (now it's a magnitude)
588
+ if (sign != 0)
589
+ {
590
+ pcm = -pcm;
591
+ }
592
+ //The magnitude must be less than 32635 to avoid overflow
593
+ if (pcm > 32635)
594
+ {
595
+ pcm = 32635;
596
+ }
597
+ //Add 132 to guarantee a 1 in
598
+ //the eight bits after the sign bit
599
+ pcm += 0x84;
600
+
601
+ /* Finding the "exponent"
602
+ * Bits:
603
+ * 1 2 3 4 5 6 7 8 9 A B C D E F G
604
+ * S 7 6 5 4 3 2 1 0 . . . . . . .
605
+ * We want to find where the first 1 after the sign bit is.
606
+ * We take the corresponding value from
607
+ * the second row as the exponent value.
608
+ * (i.e. if first 1 at position 7 -> exponent = 2) */
609
+ // exponent = 7;
610
+ //Move to the right and decrement exponent until we hit the 1
611
+ exponent=exposant[pcm>>8];
612
+ // for (expMask = 0x4000; (pcm & expMask) == 0; exponent--, expMask >>= 1) { }
613
+
614
+ /* The last part - the "mantissa"
615
+ * We need to take the four bits after the 1 we just found.
616
+ * To get it, we shift 0x0f :
617
+ * 1 2 3 4 5 6 7 8 9 A B C D E F G
618
+ * S 0 0 0 0 0 1 . . . . . . . . . (meaning exponent is 2)
619
+ * . . . . . . . . . . . . 1 1 1 1
620
+ * We shift it 5 times for an exponent of two, meaning
621
+ * we will shift our four bits (exponent + 3) bits.
622
+ * For convenience, we will actually just shift
623
+ * the number, then and with 0x0f. */
624
+ mantissa = (pcm >> (exponent + 3)) & 0x0f;
625
+
626
+ //The mu-law byte bit arrangement
627
+ //is SEEEMMMM (Sign, Exponent, and Mantissa.)
628
+ mulaw = (char)(sign | exponent << 4 | mantissa);
629
+
630
+ //Last is to flip the bits
631
+ return (char)~mulaw;
632
+ }
633
+
634
+ short alawdecode(unsigned char v)
635
+ {
636
+ return alaw[v];
637
+ }
638
+
639
+ short mulawdecode(unsigned char v)
640
+ {
641
+ return mulaw[v];
642
+ }
643
+
644
+
645
+ void AudioAdp2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len)
646
+ {
647
+ if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc))
648
+ {
649
+ return;
650
+ }
651
+ if (len&255)
652
+ {
653
+ return; // que des blocs de 256
654
+ }
655
+ if ((len>lsrc-isrc)||(((len>>8)*505*2)>ldst-idst))
656
+ {
657
+ return;
658
+ }
659
+ // printf("a2w%d\n",len);
660
+ src+=isrc;
661
+ dst+=idst;
662
+ while(len>0)
663
+ {
664
+ adpcmdecode((unsigned char*)src,dst);
665
+ src+=256;
666
+ dst+=505*2;
667
+ len-=256;
668
+ }
669
+ }
670
+
671
+ void AudioWav2adp(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len)
672
+ {
673
+ int n=0;
674
+ int l=len;
675
+
676
+ if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc))
677
+ {
678
+ return;
679
+ }
680
+ while(l>0)
681
+ {
682
+ l-=505*2;
683
+ n++;
684
+ }
685
+ if (l)
686
+ {
687
+ return; // que des blocs de 505 �chantillons 16 bits
688
+ }
689
+
690
+ if ((len>lsrc-isrc)||((n<<8)>ldst-idst))
691
+ {
692
+ return;
693
+ }
694
+ // printf("w2a%d\n",len);
695
+ src+=isrc;
696
+ dst+=idst;
697
+ while(len>0)
698
+ {
699
+ adpcmencode((short*)src,dst);
700
+ src+=505*2;
701
+ dst+=256;
702
+ len-=505*2;
703
+ }
704
+ }
705
+
706
+ void AudioWav2alaw(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu)
707
+ {
708
+ short* p;
709
+
710
+ if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc))
711
+ {
712
+ return;
713
+ }
714
+ if (len&1)
715
+ {
716
+ return;
717
+ }
718
+
719
+ if ((len>lsrc-isrc)||((len>>1)>ldst-idst))
720
+ {
721
+ return;
722
+ }
723
+ // printf("w2l%d\n",len);
724
+ src+=isrc;
725
+ dst+=idst;
726
+ p=(short*)src;
727
+ len>>=1;
728
+ if (mu)
729
+ {
730
+ while(len--)
731
+ {
732
+ *(dst++)=mulawencode(*(p++));
733
+ }
734
+ }
735
+ else
736
+ {
737
+ while(len--)
738
+ {
739
+ *(dst++)=alawencode(*(p++));
740
+ }
741
+ }
742
+ }
743
+
744
+ void AudioAlaw2wav(char* dst,int idst,int ldst,char *src,int isrc,int lsrc,int len,int mu)
745
+ {
746
+ short* p;
747
+ unsigned char* q;
748
+
749
+ if ((idst<0)||(isrc<0)||(idst>=ldst)||(isrc>=lsrc))
750
+ {
751
+ return;
752
+ }
753
+
754
+ if ((len>lsrc-isrc)||(len+len>ldst-idst))
755
+ {
756
+ return;
757
+ }
758
+ // printf("l2w%d\n",len);
759
+ src+=isrc;
760
+ dst+=idst;
761
+ p=(short*)dst;
762
+ q=(unsigned char*)src;
763
+ if (mu)
764
+ {
765
+ while(len--)
766
+ {
767
+ *(p++)=mulaw[*(q++)];
768
+ }
769
+ }
770
+ else
771
+ {
772
+ while(len--)
773
+ {
774
+ *(p++)=alaw[*(q++)];
775
+ }
776
+ }
777
+ }
778
+
779
+ void audioWrite(int reg,int val)
780
+ {
781
+ #ifdef VSIMU
782
+ printf("xxxx audioWrite %d %d\n",reg,val);
783
+ #endif
784
+ #ifdef VREAL
785
+ vlsi_write_sci(reg,val);
786
+ #endif
787
+ }
788
+ int audioRead(int reg)
789
+ {
790
+ #ifdef VSIMU
791
+ printf("xxxx audioRead %d\n",reg);
792
+ return 0;
793
+ #endif
794
+ #ifdef VREAL
795
+ if (reg==-1)
796
+ {
797
+ return vlsi_fifo_ready();
798
+ }
799
+ return vlsi_read_sci(reg);
800
+ #endif
801
+ }
802
+ int audioFeed(char *src,int len)
803
+ {
804
+ #ifdef VSIMU
805
+ printf("xxxx audioFeed %d\n",len);
806
+ return 0;
807
+ #endif
808
+ #ifdef VREAL
809
+ return vlsi_feed_sdi((uchar*)src,len);
810
+ #endif
811
+ }
812
+ void audioRefresh()
813
+ {
814
+ #ifdef VSIMU
815
+ printf("xxxx audioRefresh\n");
816
+ #endif
817
+ #ifdef VREAL
818
+ play_check(0);
819
+ rec_check();
820
+ #endif
821
+ }
822
+ void audioAmpli(int on)
823
+ {
824
+ #ifdef VSIMU
825
+ printf("xxxx audioAmpli %d\n",on);
826
+ #endif
827
+ #ifdef VREAL
828
+ vlsi_ampli(on);
829
+ #endif
830
+ }