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,787 @@
1
+ // Librairie Sound
2
+
3
+ #include<stdio.h>
4
+ #include<stdlib.h>
5
+ #include<string.h>
6
+ #include<time.h>
7
+
8
+ #include <commctrl.h>
9
+ #include <commdlg.h>
10
+ #include <mmsystem.h>
11
+
12
+ #include "mp3/mpg123.h"
13
+ #include "mp3/mpglib.h"
14
+
15
+
16
+ #include "simuaudio.h"
17
+ #include "vaudio.h"
18
+
19
+ HWAVEOUT hWaveOut;
20
+ HWAVEIN hWaveIn;
21
+
22
+ int nPlayBuffers;
23
+ WaveOutBuffer* WaveOutBuf;
24
+ int PlayState;
25
+ int PlayStateEof;
26
+ int PlayType;
27
+
28
+ int nRecBuffers;
29
+ WaveInBuffer* WaveInBuf;
30
+ int SendInBuffers;
31
+ BOOL StopRecord;
32
+ BOOL RecordInitialised;
33
+
34
+ #define PLAYST_IDLE 0
35
+ #define PLAYST_START 1
36
+ #define PLAYST_PLAY 2
37
+
38
+ #define PLAYTYPE_MP3 0
39
+ #define PLAYTYPE_WAV 1
40
+ #define PLAYTYPE_ADPCM 2
41
+
42
+ struct mpstr mp;
43
+
44
+
45
+
46
+ #define BUFMP3OUT_LENGTH 65536
47
+ #define BUFMP3IN_LENGTH 65536
48
+
49
+ char bufplayout[BUFMP3OUT_LENGTH];
50
+ int bufplayout_i;
51
+ char bufplayin[BUFMP3IN_LENGTH];
52
+ int bufplaystart;
53
+
54
+ void mp3simuinit()
55
+ {
56
+ InitMP3(&mp);
57
+ bufplayout_i=0;
58
+ bufplaystart=0;
59
+ }
60
+
61
+ char bufmp3[4608];
62
+
63
+ // mono : a b -> aaaa bbbb
64
+ // stereo : a b -> abababab
65
+
66
+ void mp3scale(short *src,int len,short *dst,int freq,int lsf,int stereo,int dstlen)
67
+ {
68
+ if (stereo==2)
69
+ {
70
+ if (len==dstlen)
71
+ {
72
+ memcpy((char*)dst,(char*)src,len);
73
+ }
74
+ }
75
+ if (stereo==1)
76
+ {
77
+ int i;
78
+ // int k=dstlen/len;
79
+ dstlen/=2;
80
+ len/=2;
81
+ for(i=0; i<len; i++)
82
+ {
83
+ int j;
84
+ int k=(i+1)*dstlen/len;
85
+ for(j=i*dstlen/len; j<k; j++)
86
+ {
87
+ *(dst++)=*src;
88
+ }
89
+ src++;
90
+ }
91
+ }
92
+ }
93
+
94
+ void wavscale(short *src,int len,short *dst)
95
+ {
96
+ int i;
97
+ len/=2;
98
+ for(i=0; i<len; i++)
99
+ {
100
+ int j;
101
+ for(j=0; j<10; j++)
102
+ {
103
+ *(dst++)=*src;
104
+ }
105
+ src++;
106
+ }
107
+ }
108
+
109
+ int getmp3len(int freq)
110
+ {
111
+ if (freq==16000)
112
+ {
113
+ return 4608*44100/32000;
114
+ }
115
+ return 4608;
116
+ }
117
+
118
+ void simuFetchMP3(int ask)
119
+ {
120
+ int dstlen;
121
+ int len;
122
+ int ret;
123
+ int loop=1;
124
+
125
+ while((loop)&&(bufplayout_i<ask))
126
+ {
127
+ ret = decodeMP3(&mp,NULL,0,bufmp3,4608,&len);
128
+ if (ret==MP3_OK)
129
+ {
130
+
131
+ // printf("freq=%d lsf=%d stereo=%d -> %d\n",freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,len);
132
+ dstlen=getmp3len(freqs[mp.fr.sampling_frequency]);
133
+ mp3scale((short*)bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen);
134
+ bufplayout_i+=dstlen;
135
+ }
136
+ else
137
+ {
138
+ len=audioPlayFetch(bufplayin+bufplaystart,BUFMP3IN_LENGTH-bufplaystart);
139
+ len+=bufplaystart;
140
+ // printf("simuFetchWav get=%d\n",len);
141
+ if (len)
142
+ {
143
+ bufplaystart=0;
144
+ ret = decodeMP3(&mp,bufplayin,len,bufmp3,4608,&len);
145
+ if (ret==MP3_OK)
146
+ {
147
+ // printf("freq=%d lsf=%d stereo=%d -> %d\n",freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,len);
148
+ dstlen=getmp3len(freqs[mp.fr.sampling_frequency]);
149
+ mp3scale(bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen);
150
+ bufplayout_i+=dstlen;
151
+ }
152
+ }
153
+ else
154
+ {
155
+ loop=0;
156
+ }
157
+ }
158
+ };
159
+ }
160
+
161
+ void simuFetchWav(int ask)
162
+ {
163
+ int len;
164
+ int loop=1;
165
+ int askwav=2+(((ask-bufplayout_i)/10)&(-2));
166
+
167
+ while((loop)&&(bufplaystart<askwav))
168
+ {
169
+ len=audioPlayFetch(bufplayin+bufplaystart,askwav-bufplaystart);
170
+ printf("simuFetchWav get=%d\n",len);
171
+ if (len)
172
+ {
173
+ bufplaystart+=len;
174
+ }
175
+ else
176
+ {
177
+ loop=0;
178
+ }
179
+ }
180
+
181
+ len=bufplaystart&(-2);
182
+ if (len>=askwav)
183
+ {
184
+ len=askwav;
185
+ }
186
+
187
+ wavscale(bufplayin,len,bufplayout+bufplayout_i);
188
+ bufplayout_i+=len*10;
189
+ if (bufplaystart!=len)
190
+ {
191
+ memcpy(bufplayin,bufplayin+len,bufplaystart-len);
192
+ bufplaystart-=len;
193
+ }
194
+ else
195
+ {
196
+ bufplaystart=0;
197
+ }
198
+ }
199
+
200
+ void simuFetchAdpcm(int ask)
201
+ {
202
+ printf("simuFetchAdpcm %d\n",ask);
203
+ while(bufplayout_i<ask)
204
+ {
205
+ if (bufplaystart<256)
206
+ {
207
+ bufplaystart+=audioPlayFetch(bufplayin+bufplaystart,256-bufplaystart);
208
+ }
209
+ if (bufplaystart<256)
210
+ {
211
+ return;
212
+ }
213
+
214
+ adpcmdecode(bufplayin,bufmp3);
215
+ wavscale(bufmp3,1010,bufplayout+bufplayout_i);
216
+ bufplayout_i+=1010*10;
217
+ bufplaystart-=256;
218
+ memcpy(bufplayin,bufplayin+256,bufplaystart);
219
+ }
220
+ }
221
+
222
+ int simuFetchPlay(char* dst,int ask)
223
+ {
224
+ // printf("simuFetchPlay %d\n",PlayType);
225
+
226
+ if (PlayType==PLAYTYPE_MP3)
227
+ {
228
+ simuFetchMP3(ask);
229
+ }
230
+ if (PlayType==PLAYTYPE_WAV)
231
+ {
232
+ simuFetchWav(ask);
233
+ }
234
+ if (PlayType==PLAYTYPE_ADPCM)
235
+ {
236
+ simuFetchAdpcm(ask);
237
+ }
238
+ // printf("bufplayout_i=%d\n",bufplayout_i);
239
+ if (bufplayout_i<ask)
240
+ {
241
+ ask=bufplayout_i;
242
+ }
243
+ if ((PlayStateEof)&&(!bufplayout_i))
244
+ {
245
+ PlayState=PLAYST_IDLE;
246
+ }
247
+ if (!ask)
248
+ {
249
+ return 0;
250
+ }
251
+
252
+ memcpy(dst,bufplayout,ask);
253
+ memcpy(bufplayout,bufplayout+ask,bufplayout_i-ask);
254
+ bufplayout_i-=ask;
255
+ return ask;
256
+ }
257
+
258
+ void simuFetchStart()
259
+ {
260
+ while(1)
261
+ {
262
+ int len=BUFMP3IN_LENGTH-bufplaystart;
263
+ if (len)
264
+ {
265
+ len=audioPlayFetch(bufplayin+bufplaystart,len);
266
+ }
267
+ printf("simuFetchStart get=%d\n",len);
268
+
269
+ if (!len)
270
+ {
271
+ if (PlayStateEof)
272
+ {
273
+ PlayState=PLAYST_IDLE;
274
+ }
275
+ return;
276
+ }
277
+ bufplaystart+=len;
278
+
279
+ if (1)//(((bufplaystart>=2)&&(bufplayin[0]=='I')&&(bufplayin[1]=='D')&&(bufplayin[2]=='3')) ||((bufplayin[0]&255)==255))
280
+ {
281
+ int i=0;
282
+ int head=0;
283
+ PlayType=PLAYTYPE_MP3;
284
+ printf("detect MP3\n");
285
+ PlayState=PLAYST_PLAY;
286
+ return;
287
+
288
+ /* while((i<bufplaystart)&&(!checkmp3header(head)))
289
+ {
290
+ head=(head<<8)+(bufplayin[i++]&255);
291
+ }
292
+ if (i<bufplaystart)
293
+ {
294
+ int j;
295
+ printf("detect start at %d\n",i);
296
+ i-=4;
297
+ len=bufplaystart-i;
298
+ for(j=0;j<len;j++) bufplayin[j]=bufplayin[j+i];
299
+ bufplaystart=len;
300
+ PlayState=PLAYST_PLAY;
301
+ return;
302
+ }
303
+ */
304
+ }
305
+ else if ((bufplaystart>20)&&(bufplayin[0]=='R')&&(bufplayin[1]=='I')&&(bufplayin[2]=='F'))
306
+ {
307
+ int i;
308
+ printf("detect wav bufplaystart=%d\n",bufplaystart);
309
+ PlayType=(bufplayin[20]==0x11)?PLAYTYPE_ADPCM:PLAYTYPE_WAV;
310
+ for(i=0; i<bufplaystart-8; i++) if (!strncmp(bufplayin+i,"data",4))
311
+ {
312
+ int j;
313
+ i+=8;
314
+ len=bufplaystart-i;
315
+ for(j=0; j<len; j++)
316
+ {
317
+ bufplayin[j]=bufplayin[j+i];
318
+ }
319
+ bufplaystart=len;
320
+ PlayState=PLAYST_PLAY;
321
+ return;
322
+ }
323
+ }
324
+ }
325
+ }
326
+
327
+ int simuFetch(char* dst,int ask)
328
+ {
329
+ // printf("simuFetch %d\n",PlayState);
330
+ if (PlayState==PLAYST_IDLE)
331
+ {
332
+ return 0;
333
+ }
334
+ if (PlayState==PLAYST_START)
335
+ {
336
+ simuFetchStart();
337
+ }
338
+ if (PlayState==PLAYST_PLAY)
339
+ {
340
+ return simuFetchPlay(dst,ask);
341
+ }
342
+ return 0;
343
+ }
344
+
345
+
346
+ extern HWND mainwin;
347
+
348
+ int OutFillBuffer(int i)
349
+ {
350
+ int k,len;
351
+
352
+ if (WaveOutBuf[i].lpData==NULL)
353
+ {
354
+ return 0;
355
+ }
356
+ k=simuFetch(WaveOutBuf[i].lpData,WaveOutBuf[i].dwDataSize);
357
+ // printf("#outfill\n");
358
+ len=WaveOutBuf[i].dwDataSize-k;
359
+ if (len>0)
360
+ {
361
+ memset(WaveOutBuf[i].lpData+k,0,len);
362
+ }
363
+
364
+ return 0;
365
+ }
366
+ int InFillBuffer(int i)
367
+ {
368
+ char *p;
369
+
370
+ if (WaveInBuf[i].lpData==NULL)
371
+ {
372
+ return 0;
373
+ }
374
+
375
+ p=audioRecFeed_begin(256);
376
+ adpcmencode(WaveInBuf[i].lpData,p);
377
+ /* p=audioRecFeed_begin(WaveInBuf[i].dwDataSize);
378
+ memcpy(p,WaveInBuf[i].lpData,WaveInBuf[i].dwDataSize);
379
+ */ audioRecFeed_end();
380
+
381
+ return 0;
382
+ }
383
+
384
+ void WaveOutFreeAll()
385
+ {
386
+ int i;
387
+ for (i=0; i<nPlayBuffers; i++)
388
+ {
389
+ waveOutUnprepareHeader(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
390
+
391
+ if (WaveOutBuf[i].lpData != NULL)
392
+ {
393
+ GlobalUnlock(WaveOutBuf[i].hData);
394
+ }
395
+ if (WaveOutBuf[i].hData)
396
+ {
397
+ GlobalFree(WaveOutBuf[i].hData);
398
+ }
399
+
400
+ if (WaveOutBuf[i].lpWaveHdr != NULL)
401
+ {
402
+ GlobalUnlock(WaveOutBuf[i].hWaveHdr);
403
+ }
404
+ if (WaveOutBuf[i].hWaveHdr)
405
+ {
406
+ GlobalFree(WaveOutBuf[i].hWaveHdr);
407
+ }
408
+ }
409
+ memset(WaveOutBuf,0,nPlayBuffers*sizeof(WaveOutBuffer));
410
+ }
411
+ void WaveInFreeAll()
412
+ {
413
+ int i;
414
+ for (i=0; i<nRecBuffers; i++)
415
+ {
416
+ waveInUnprepareHeader(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
417
+
418
+ if (WaveInBuf[i].lpData != NULL)
419
+ {
420
+ GlobalUnlock(WaveInBuf[i].hData);
421
+ }
422
+ if (WaveInBuf[i].hData)
423
+ {
424
+ GlobalFree(WaveInBuf[i].hData);
425
+ }
426
+
427
+ if (WaveInBuf[i].lpWaveHdr != NULL)
428
+ {
429
+ GlobalUnlock(WaveInBuf[i].hWaveHdr);
430
+ }
431
+ if (WaveInBuf[i].hWaveHdr)
432
+ {
433
+ GlobalFree(WaveInBuf[i].hWaveHdr);
434
+ }
435
+ }
436
+ memset(WaveInBuf,0,nRecBuffers*sizeof(WaveInBuffer));
437
+ RecordInitialised=FALSE;
438
+ SendInBuffers=0;
439
+ }
440
+
441
+
442
+ int PlayInit(int rate,int ChannelSize,int nbBuffers)
443
+ {
444
+ UINT wResult;
445
+ PCMWAVEFORMAT pcmWaveFormat;
446
+ int i;
447
+
448
+ // Open a waveform device for output using window callback.
449
+ int nChannels=2;
450
+ int BitsPerSample=16;
451
+
452
+ pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM;
453
+ pcmWaveFormat.wf.nChannels=nChannels;
454
+ pcmWaveFormat.wf.nSamplesPerSec=rate;
455
+ pcmWaveFormat.wf.nAvgBytesPerSec=rate*nChannels*(BitsPerSample/8);
456
+ pcmWaveFormat.wf.nBlockAlign=(nChannels*BitsPerSample)/8;
457
+ pcmWaveFormat.wBitsPerSample=BitsPerSample;
458
+
459
+ wResult=waveOutOpen((LPHWAVEOUT)&hWaveOut, WAVE_MAPPER,
460
+ (LPWAVEFORMATEX)&pcmWaveFormat,
461
+ (LONG)mainwin, 0L, CALLBACK_WINDOW);
462
+ if (wResult != 0)
463
+ {
464
+ return 0;
465
+ }
466
+
467
+ nPlayBuffers=nbBuffers;
468
+ if (WaveOutBuf)
469
+ {
470
+ free(WaveOutBuf);
471
+ }
472
+ WaveOutBuf=(WaveOutBuffer*)malloc(sizeof(WaveOutBuffer)*nPlayBuffers);
473
+
474
+
475
+ for (i=0; i<nPlayBuffers; i++)
476
+ {
477
+ WaveOutBuf[i].nChannels=nChannels;
478
+ WaveOutBuf[i].Rate=rate;
479
+ WaveOutBuf[i].Resolution=BitsPerSample;
480
+ WaveOutBuf[i].hwnd=mainwin;
481
+ WaveOutBuf[i].dwDataSize=ChannelSize;
482
+
483
+ // Allocate and lock memory for the waveform data.
484
+ WaveOutBuf[i].hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, WaveOutBuf[i].dwDataSize );
485
+ if (!WaveOutBuf[i].hData)
486
+ {
487
+ return 0;
488
+ }
489
+ if ((WaveOutBuf[i].lpData = (char*)GlobalLock(WaveOutBuf[i].hData)) == NULL)
490
+ {
491
+ WaveOutFreeAll();
492
+ return 0;
493
+ }
494
+
495
+ // Allocate and lock memory for the header.
496
+ WaveOutBuf[i].hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,(DWORD) sizeof(WAVEHDR));
497
+ if (WaveOutBuf[i].hWaveHdr == NULL)
498
+ {
499
+ WaveOutFreeAll();
500
+ return 0;
501
+ }
502
+
503
+ WaveOutBuf[i].lpWaveHdr = (LPWAVEHDR) GlobalLock(WaveOutBuf[i].hWaveHdr);
504
+ if (WaveOutBuf[i].lpWaveHdr == NULL)
505
+ {
506
+ WaveOutFreeAll();
507
+ return 0;
508
+ }
509
+
510
+ // After allocation, set up and prepare header.
511
+
512
+ WaveOutBuf[i].lpWaveHdr->lpData = WaveOutBuf[i].lpData;
513
+ WaveOutBuf[i].lpWaveHdr->dwBufferLength = WaveOutBuf[i].dwDataSize;
514
+ WaveOutBuf[i].lpWaveHdr->dwUser = i;
515
+ WaveOutBuf[i].lpWaveHdr->dwFlags = 0L;
516
+ WaveOutBuf[i].lpWaveHdr->dwLoops = 1L;
517
+ wResult = waveOutPrepareHeader(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
518
+ if (wResult != 0)
519
+ {
520
+ wResult=waveOutClose(hWaveOut);
521
+ WaveOutFreeAll();
522
+ return 0;
523
+ }
524
+ memset(WaveOutBuf[i].lpData,0,WaveOutBuf[i].dwDataSize);
525
+ }
526
+
527
+ for (i=0; i<nPlayBuffers; i++)
528
+ {
529
+ // Now the data block can be sent to the output device. The
530
+ // waveOutWrite function returns immediately and waveform
531
+ // data is sent to the output device in the background.
532
+ wResult = waveOutWrite(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
533
+ if (wResult != 0)
534
+ {
535
+ wResult=waveOutReset(hWaveOut);
536
+ wResult=waveOutClose(hWaveOut);
537
+ WaveOutFreeAll();
538
+ return 0;
539
+ }
540
+ }
541
+ return 1;
542
+ }
543
+
544
+ int RecStart(int rate,int ChannelSize,int nbBuffers)
545
+ {
546
+ UINT wResult;
547
+ PCMWAVEFORMAT pcmWaveFormat;
548
+ int r,i;
549
+ int BitsPerSample=16;
550
+ int nChannels=1;
551
+ if (RecordInitialised)
552
+ {
553
+ return 0;
554
+ }
555
+ nRecBuffers=nbBuffers;
556
+ if (WaveInBuf)
557
+ {
558
+ free(WaveInBuf);
559
+ }
560
+ WaveInBuf=(WaveInBuffer*)malloc(sizeof(WaveInBuffer)*nRecBuffers);
561
+ // memset(WaveInBuf,0,nRecBuffers*sizeof(WaveInBuffer));
562
+ // Open a waveform device for output using window callback.
563
+
564
+ if (BitsPerSample>8)
565
+ {
566
+ BitsPerSample=16;
567
+ }
568
+ pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM;
569
+ pcmWaveFormat.wf.nChannels=nChannels;
570
+ pcmWaveFormat.wf.nSamplesPerSec=rate;
571
+ pcmWaveFormat.wf.nAvgBytesPerSec=rate*nChannels*(BitsPerSample/8);
572
+ pcmWaveFormat.wf.nBlockAlign=(nChannels*BitsPerSample)/8;
573
+ pcmWaveFormat.wBitsPerSample=BitsPerSample;
574
+
575
+ if (r=waveInOpen((LPHWAVEIN)&hWaveIn, WAVE_MAPPER,
576
+ (LPWAVEFORMATEX)&pcmWaveFormat,
577
+ (LONG)mainwin, 0L, CALLBACK_WINDOW))
578
+ {
579
+ // MMechostr(MSKDEBUG,"Record: Unable to Open Device\n");
580
+ switch (r)
581
+ {
582
+ case MMSYSERR_ALLOCATED :
583
+ printf("Device allready allocated\n");
584
+ break;
585
+ case MMSYSERR_BADDEVICEID :
586
+ printf("Bad device ID\n");
587
+ break;
588
+ case MMSYSERR_NODRIVER :
589
+ printf("No driver\n");
590
+ break;
591
+ case WAVERR_BADFORMAT :
592
+ printf("Bad Format\n");
593
+ break;
594
+ case MMSYSERR_NOMEM :
595
+ printf("No memory\n");
596
+ break;
597
+ }
598
+ return 0;
599
+ }
600
+
601
+ for (i=0; i<nRecBuffers; i++)
602
+ {
603
+ WaveInBuf[i].nChannels=nChannels;
604
+ WaveInBuf[i].Rate=rate;
605
+ WaveInBuf[i].Resolution=BitsPerSample;
606
+ WaveInBuf[i].dwDataSize=nChannels*(BitsPerSample/8)*ChannelSize;
607
+ WaveInBuf[i].hwnd=mainwin;
608
+ // Allocate and lock memory for the waveform data.
609
+ WaveInBuf[i].hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, WaveInBuf[i].dwDataSize );
610
+ if (!WaveInBuf[i].hData)
611
+ {
612
+ // MMechostr(MSKDEBUG,"Record: Unable to Allocate memory\n");
613
+ return 0;
614
+ }
615
+ if ((WaveInBuf[i].lpData = (char*)GlobalLock(WaveInBuf[i].hData)) == NULL)
616
+ {
617
+ // MMechostr(MSKDEBUG,"Record: Unable to Lock Memory\n");
618
+ WaveInFreeAll();
619
+ return 0;
620
+ }
621
+
622
+ // Allocate and lock memory for the header.
623
+ WaveInBuf[i].hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
624
+ (DWORD) sizeof(WAVEHDR));
625
+ if (WaveInBuf[i].hWaveHdr == NULL)
626
+ {
627
+ WaveInFreeAll();
628
+ // MMechostr(MSKDEBUG,"Record: Unable to allocate wave header\n");
629
+ return 0;
630
+ }
631
+
632
+ WaveInBuf[i].lpWaveHdr = (LPWAVEHDR) GlobalLock(WaveInBuf[i].hWaveHdr);
633
+ if (WaveInBuf[i].lpWaveHdr == NULL)
634
+ {
635
+ WaveInFreeAll();
636
+ // MMechostr(MSKDEBUG,"Record: Unable to Lock wave header memory\n");
637
+ return 0;
638
+ }
639
+
640
+ // After allocation, set up and prepare header.
641
+
642
+ WaveInBuf[i].lpWaveHdr->lpData = WaveInBuf[i].lpData;
643
+ WaveInBuf[i].lpWaveHdr->dwBufferLength = WaveInBuf[i].dwDataSize;
644
+ WaveInBuf[i].lpWaveHdr->dwUser = i;
645
+ WaveInBuf[i].lpWaveHdr->dwFlags = 0L;
646
+ WaveInBuf[i].lpWaveHdr->dwLoops = 0L;
647
+ wResult=waveInPrepareHeader(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
648
+ if (wResult != 0)
649
+ {
650
+ char buf[MAXERRORLENGTH];
651
+ WaveInFreeAll();
652
+ // MMechostr(MSKDEBUG,"PrepareRecBuf: Unable to write block to the device\n");
653
+ waveOutGetErrorText(wResult,buf,MAXERRORLENGTH);
654
+ // MMechostr(MSKDEBUG,buf);
655
+ // MMechostr(MSKDEBUG,"\n");
656
+ return 0;
657
+ }
658
+
659
+ }
660
+
661
+ for (i=0; i<nRecBuffers; i++)
662
+ {
663
+ // Now the data block can be sent to the input device. The
664
+ // waveOutWrite function returns immediately and waveform
665
+ // data is sent to the output device in the background.
666
+ wResult = waveInAddBuffer(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
667
+ if (wResult != 0)
668
+ {
669
+ WaveInFreeAll();
670
+ // MMechostr(MSKDEBUG,"Record: Unable to write block to the device\n");
671
+ return 0;
672
+ }
673
+ SendInBuffers++;
674
+ }
675
+ waveInStart(hWaveIn);
676
+ StopRecord=FALSE;
677
+ RecordInitialised=TRUE;
678
+
679
+ return 1;
680
+ }
681
+
682
+ int PlayStart()
683
+ {
684
+ PlayState=PLAYST_START;
685
+ PlayStateEof=0;
686
+ mp3simuinit();
687
+ return 0;
688
+ }
689
+
690
+ int PlayStop()
691
+ {
692
+ PlayState=PLAYST_IDLE;
693
+ return 0;
694
+ }
695
+
696
+ int PlayEof()
697
+ {
698
+ PlayStateEof=1;
699
+ return 0;
700
+ }
701
+
702
+ int RecStop()
703
+ {
704
+ // waveInReset(hWaveIn);
705
+ // waveInClose(hWaveIn);
706
+ StopRecord=TRUE;
707
+ return 0;
708
+ }
709
+
710
+ int PlayData(int i)
711
+ {
712
+ WORD wResult;
713
+
714
+ OutFillBuffer(i);
715
+ wResult = waveOutWrite(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
716
+ if (wResult != 0)
717
+ {
718
+ WaveOutFreeAll();
719
+ return 0;
720
+ }
721
+ return 1;
722
+ }
723
+
724
+ int RecData(int i)
725
+ {
726
+ WORD wResult;
727
+
728
+ SendInBuffers--;
729
+ if (!StopRecord)
730
+ {
731
+ InFillBuffer(i);
732
+ wResult = waveInAddBuffer(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
733
+ if (wResult != 0)
734
+ {
735
+ WaveInFreeAll();
736
+ return 0;
737
+ }
738
+ SendInBuffers++;
739
+ }
740
+ else
741
+ {
742
+ if (!SendInBuffers)
743
+ {
744
+ WaveInFreeAll();
745
+ StopRecord=FALSE;
746
+ waveInClose(hWaveIn);
747
+ }
748
+ }
749
+ return 1;
750
+ }
751
+
752
+ int audioEventPlayData(int eventId, int wParam, int lParam)
753
+ {
754
+ // printf("###play cb\n");
755
+ PlayData(((LPWAVEHDR)lParam)->dwUser);
756
+ return 0;
757
+ }
758
+
759
+ int audioEventRecData(int eventId, int wParam, int lParam)
760
+ {
761
+ // printf("###rec cb\n");
762
+ RecData(((LPWAVEHDR)lParam)->dwUser);
763
+ return 0;
764
+ }
765
+
766
+ void audioSetVolume(int vol)
767
+ {
768
+ vol&=255;
769
+ waveOutSetVolume(hWaveOut,(vol<<24)+(vol<<8));
770
+ }
771
+
772
+ int simuaudioinit()
773
+ {
774
+ nPlayBuffers=0;
775
+ PlayState=PLAYST_IDLE;
776
+
777
+ nRecBuffers=0;
778
+ SendInBuffers=0;
779
+ StopRecord=FALSE;
780
+ RecordInitialised=FALSE;
781
+
782
+ PlayInit(44100,10000 //22050
783
+ ,8);
784
+
785
+ return 0;
786
+ }
787
+