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
@@ -1,677 +0,0 @@
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
- }