nabaztag_hack_kit 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +7 -0
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.md +11 -0
  4. data/Gemfile +4 -0
  5. data/README.md +119 -0
  6. data/Rakefile +21 -0
  7. data/bin/mtl_comp +41 -0
  8. data/bin/mtl_merge +22 -0
  9. data/bin/mtl_simu +40 -0
  10. data/bytecode/lib/buffer.mtl +109 -0
  11. data/bytecode/lib/button.mtl +27 -0
  12. data/bytecode/lib/data_helper.mtl +88 -0
  13. data/bytecode/lib/ear.mtl +90 -0
  14. data/bytecode/lib/led.mtl +33 -0
  15. data/bytecode/lib/rfid.mtl +50 -0
  16. data/bytecode/main.mtl +129 -0
  17. data/config.ru +6 -0
  18. data/ext/bytecode/Commands.md +172 -0
  19. data/ext/bytecode/Docs.md +488 -0
  20. data/ext/bytecode/README.md +5 -0
  21. data/ext/bytecode/lib/arp.mtl +159 -0
  22. data/ext/bytecode/lib/cfg.mtl +74 -0
  23. data/ext/bytecode/lib/choreos.mtl +1487 -0
  24. data/ext/bytecode/lib/dhcp.mtl +152 -0
  25. data/ext/bytecode/lib/dns.mtl +89 -0
  26. data/ext/bytecode/lib/http.mtl +84 -0
  27. data/ext/bytecode/lib/tcp.mtl +340 -0
  28. data/ext/bytecode/lib/udp.mtl +49 -0
  29. data/ext/bytecode/lib/util.mtl +74 -0
  30. data/ext/bytecode/lib/var.mtl +15 -0
  31. data/ext/bytecode/lib/wifi.mtl +243 -0
  32. data/ext/bytecode/nominal-ping.mtl +5828 -0
  33. data/ext/mtl/Makefile +42 -0
  34. data/ext/mtl/README.md +13 -0
  35. data/ext/mtl/bc.cpp +1891 -0
  36. data/ext/mtl/conf.bin.sans_password +0 -0
  37. data/ext/mtl/config.txt +5 -0
  38. data/ext/mtl/dumpbc.c +2566 -0
  39. data/ext/mtl/extconf.rb +1 -0
  40. data/ext/mtl/linux_simu.c +271 -0
  41. data/ext/mtl/linux_simuaudio.c +16 -0
  42. data/ext/mtl/linux_simuaudio.h +18 -0
  43. data/ext/mtl/linux_simunet.c +620 -0
  44. data/ext/mtl/linux_simunet.h +7 -0
  45. data/ext/mtl/log.c +297 -0
  46. data/ext/mtl/log.h +20 -0
  47. data/ext/mtl/main_compiler.cpp +104 -0
  48. data/ext/mtl/main_simu.cpp +221 -0
  49. data/ext/mtl/mp3/GTKANAL.H +97 -0
  50. data/ext/mtl/mp3/LAYER3.C +2090 -0
  51. data/ext/mtl/mp3/TABINIT.C +82 -0
  52. data/ext/mtl/mp3/common.c +265 -0
  53. data/ext/mtl/mp3/dct64_i386.c +316 -0
  54. data/ext/mtl/mp3/decode_i386.c +155 -0
  55. data/ext/mtl/mp3/huffman.h +332 -0
  56. data/ext/mtl/mp3/interface.c +258 -0
  57. data/ext/mtl/mp3/mpg123.h +182 -0
  58. data/ext/mtl/mp3/mpglib.h +44 -0
  59. data/ext/mtl/properties.c +293 -0
  60. data/ext/mtl/properties.h +10 -0
  61. data/ext/mtl/simu.c +750 -0
  62. data/ext/mtl/simuaudio.c +662 -0
  63. data/ext/mtl/simuaudio.h +74 -0
  64. data/ext/mtl/simunet.c +400 -0
  65. data/ext/mtl/simunet.h +30 -0
  66. data/ext/mtl/utils/correct_const.sh +34 -0
  67. data/ext/mtl/vaudio.c +677 -0
  68. data/ext/mtl/vaudio.h +46 -0
  69. data/ext/mtl/vbc.h +160 -0
  70. data/ext/mtl/vbc_str.h +166 -0
  71. data/ext/mtl/vcomp/Makefile +29 -0
  72. data/ext/mtl/vcomp/bootstrap.cpp +89 -0
  73. data/ext/mtl/vcomp/compiler.cpp +470 -0
  74. data/ext/mtl/vcomp/compiler.h +200 -0
  75. data/ext/mtl/vcomp/compiler_file.cpp +929 -0
  76. data/ext/mtl/vcomp/compiler_prog.cpp +250 -0
  77. data/ext/mtl/vcomp/compiler_term.cpp +1053 -0
  78. data/ext/mtl/vcomp/compiler_type.cpp +872 -0
  79. data/ext/mtl/vcomp/compiler_var.cpp +289 -0
  80. data/ext/mtl/vcomp/file.cpp +79 -0
  81. data/ext/mtl/vcomp/file.h +39 -0
  82. data/ext/mtl/vcomp/filesystem.h +14 -0
  83. data/ext/mtl/vcomp/interpreter.cpp +85 -0
  84. data/ext/mtl/vcomp/interpreter.h +121 -0
  85. data/ext/mtl/vcomp/memory.cpp +241 -0
  86. data/ext/mtl/vcomp/memory.h +326 -0
  87. data/ext/mtl/vcomp/param.h +95 -0
  88. data/ext/mtl/vcomp/parser.cpp +427 -0
  89. data/ext/mtl/vcomp/parser.h +97 -0
  90. data/ext/mtl/vcomp/parser_xml.cpp +124 -0
  91. data/ext/mtl/vcomp/prodbuffer.cpp +125 -0
  92. data/ext/mtl/vcomp/prodbuffer.h +42 -0
  93. data/ext/mtl/vcomp/resource.h +17 -0
  94. data/ext/mtl/vcomp/stdlib_core.cpp +122 -0
  95. data/ext/mtl/vcomp/terminal.cpp +73 -0
  96. data/ext/mtl/vcomp/terminal.h +30 -0
  97. data/ext/mtl/vcomp/util.cpp +48 -0
  98. data/ext/mtl/vcomp/util.h +31 -0
  99. data/ext/mtl/vinterp.c +1349 -0
  100. data/ext/mtl/vinterp.h +11 -0
  101. data/ext/mtl/vloader.c +127 -0
  102. data/ext/mtl/vloader.h +31 -0
  103. data/ext/mtl/vlog.c +589 -0
  104. data/ext/mtl/vlog.h +69 -0
  105. data/ext/mtl/vmem.c +424 -0
  106. data/ext/mtl/vmem.h +107 -0
  107. data/ext/mtl/vnet.c +255 -0
  108. data/ext/mtl/vnet.h +19 -0
  109. data/lib/nabaztag_hack_kit/message/api.rb +39 -0
  110. data/lib/nabaztag_hack_kit/message/helper.rb +39 -0
  111. data/lib/nabaztag_hack_kit/message.rb +36 -0
  112. data/lib/nabaztag_hack_kit/server.rb +50 -0
  113. data/lib/nabaztag_hack_kit/version.rb +3 -0
  114. data/lib/nabaztag_hack_kit.rb +4 -0
  115. data/nabaztag_hack_kit.gemspec +29 -0
  116. data/public/bytecode.bin +0 -0
  117. data/test/bytecode/helper.mtl +60 -0
  118. data/test/bytecode/native.mtl +28 -0
  119. data/test/bytecode/test.mtl +221 -0
  120. data/test/spec_helper.rb +5 -0
  121. data/test/unit/message_spec.rb +56 -0
  122. metadata +209 -0
@@ -0,0 +1,662 @@
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) memcpy((char*)dst,(char*)src,len);
71
+ }
72
+ if (stereo==1)
73
+ {
74
+ int i;
75
+ // int k=dstlen/len;
76
+ dstlen/=2;
77
+ len/=2;
78
+ for(i=0;i<len;i++)
79
+ {
80
+ int j;
81
+ int k=(i+1)*dstlen/len;
82
+ for(j=i*dstlen/len;j<k;j++) *(dst++)=*src;
83
+ src++;
84
+ }
85
+ }
86
+ }
87
+
88
+ void wavscale(short *src,int len,short *dst)
89
+ {
90
+ int i;
91
+ len/=2;
92
+ for(i=0;i<len;i++)
93
+ {
94
+ int j;
95
+ for(j=0;j<10;j++) *(dst++)=*src;
96
+ src++;
97
+ }
98
+ }
99
+
100
+ int getmp3len(int freq)
101
+ {
102
+ if (freq==16000) return 4608*44100/32000;
103
+ return 4608;
104
+ }
105
+
106
+ void simuFetchMP3(int ask)
107
+ {
108
+ int dstlen;
109
+ int len;
110
+ int ret;
111
+ int loop=1;
112
+
113
+ while((loop)&&(bufplayout_i<ask))
114
+ {
115
+ ret = decodeMP3(&mp,NULL,0,bufmp3,4608,&len);
116
+ if (ret==MP3_OK)
117
+ {
118
+
119
+ // printf("freq=%d lsf=%d stereo=%d -> %d\n",freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,len);
120
+ dstlen=getmp3len(freqs[mp.fr.sampling_frequency]);
121
+ mp3scale((short*)bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen);
122
+ bufplayout_i+=dstlen;
123
+ }
124
+ else
125
+ {
126
+ len=audioPlayFetch(bufplayin+bufplaystart,BUFMP3IN_LENGTH-bufplaystart);
127
+ len+=bufplaystart;
128
+ // printf("simuFetchWav get=%d\n",len);
129
+ if (len)
130
+ {
131
+ bufplaystart=0;
132
+ ret = decodeMP3(&mp,bufplayin,len,bufmp3,4608,&len);
133
+ if (ret==MP3_OK)
134
+ {
135
+ // printf("freq=%d lsf=%d stereo=%d -> %d\n",freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,len);
136
+ dstlen=getmp3len(freqs[mp.fr.sampling_frequency]);
137
+ mp3scale(bufmp3,len,bufplayout+bufplayout_i,freqs[mp.fr.sampling_frequency],mp.fr.lsf,mp.fr.stereo,dstlen);
138
+ bufplayout_i+=dstlen;
139
+ }
140
+ }
141
+ else loop=0;
142
+ }
143
+ };
144
+ }
145
+
146
+ void simuFetchWav(int ask)
147
+ {
148
+ int len;
149
+ int loop=1;
150
+ int askwav=2+(((ask-bufplayout_i)/10)&(-2));
151
+
152
+ while((loop)&&(bufplaystart<askwav))
153
+ {
154
+ len=audioPlayFetch(bufplayin+bufplaystart,askwav-bufplaystart);
155
+ printf("simuFetchWav get=%d\n",len);
156
+ if (len) bufplaystart+=len;
157
+ else loop=0;
158
+ }
159
+
160
+ len=bufplaystart&(-2);
161
+ if (len>=askwav) len=askwav;
162
+
163
+ wavscale(bufplayin,len,bufplayout+bufplayout_i);
164
+ bufplayout_i+=len*10;
165
+ if (bufplaystart!=len)
166
+ {
167
+ memcpy(bufplayin,bufplayin+len,bufplaystart-len);
168
+ bufplaystart-=len;
169
+ }
170
+ else bufplaystart=0;
171
+ }
172
+
173
+ void simuFetchAdpcm(int ask)
174
+ {
175
+ printf("simuFetchAdpcm %d\n",ask);
176
+ while(bufplayout_i<ask)
177
+ {
178
+ if (bufplaystart<256) bufplaystart+=audioPlayFetch(bufplayin+bufplaystart,256-bufplaystart);
179
+ if (bufplaystart<256) return;
180
+
181
+ adpcmdecode(bufplayin,bufmp3);
182
+ wavscale(bufmp3,1010,bufplayout+bufplayout_i);
183
+ bufplayout_i+=1010*10;
184
+ bufplaystart-=256;
185
+ memcpy(bufplayin,bufplayin+256,bufplaystart);
186
+ }
187
+ }
188
+
189
+ int simuFetchPlay(char* dst,int ask)
190
+ {
191
+ // printf("simuFetchPlay %d\n",PlayType);
192
+
193
+ if (PlayType==PLAYTYPE_MP3)simuFetchMP3(ask);
194
+ if (PlayType==PLAYTYPE_WAV)simuFetchWav(ask);
195
+ if (PlayType==PLAYTYPE_ADPCM)simuFetchAdpcm(ask);
196
+ // printf("bufplayout_i=%d\n",bufplayout_i);
197
+ if (bufplayout_i<ask) ask=bufplayout_i;
198
+ if ((PlayStateEof)&&(!bufplayout_i)) PlayState=PLAYST_IDLE;
199
+ if (!ask) return 0;
200
+
201
+ memcpy(dst,bufplayout,ask);
202
+ memcpy(bufplayout,bufplayout+ask,bufplayout_i-ask);
203
+ bufplayout_i-=ask;
204
+ return ask;
205
+ }
206
+
207
+ void simuFetchStart()
208
+ {
209
+ while(1)
210
+ {
211
+ int len=BUFMP3IN_LENGTH-bufplaystart;
212
+ if (len) len=audioPlayFetch(bufplayin+bufplaystart,len);
213
+ printf("simuFetchStart get=%d\n",len);
214
+
215
+ if (!len)
216
+ {
217
+ if (PlayStateEof) PlayState=PLAYST_IDLE;
218
+ return;
219
+ }
220
+ bufplaystart+=len;
221
+
222
+ if (1)//(((bufplaystart>=2)&&(bufplayin[0]=='I')&&(bufplayin[1]=='D')&&(bufplayin[2]=='3')) ||((bufplayin[0]&255)==255))
223
+ {
224
+ int i=0;
225
+ int head=0;
226
+ PlayType=PLAYTYPE_MP3;
227
+ printf("detect MP3\n");
228
+ PlayState=PLAYST_PLAY;
229
+ return;
230
+
231
+ /* while((i<bufplaystart)&&(!checkmp3header(head)))
232
+ {
233
+ head=(head<<8)+(bufplayin[i++]&255);
234
+ }
235
+ if (i<bufplaystart)
236
+ {
237
+ int j;
238
+ printf("detect start at %d\n",i);
239
+ i-=4;
240
+ len=bufplaystart-i;
241
+ for(j=0;j<len;j++) bufplayin[j]=bufplayin[j+i];
242
+ bufplaystart=len;
243
+ PlayState=PLAYST_PLAY;
244
+ return;
245
+ }
246
+ */ }
247
+ else if ((bufplaystart>20)&&(bufplayin[0]=='R')&&(bufplayin[1]=='I')&&(bufplayin[2]=='F'))
248
+ {
249
+ int i;
250
+ printf("detect wav bufplaystart=%d\n",bufplaystart);
251
+ PlayType=(bufplayin[20]==0x11)?PLAYTYPE_ADPCM:PLAYTYPE_WAV;
252
+ for(i=0;i<bufplaystart-8;i++) if (!strncmp(bufplayin+i,"data",4))
253
+ {
254
+ int j;
255
+ i+=8;
256
+ len=bufplaystart-i;
257
+ for(j=0;j<len;j++) bufplayin[j]=bufplayin[j+i];
258
+ bufplaystart=len;
259
+ PlayState=PLAYST_PLAY;
260
+ return;
261
+ }
262
+ }
263
+ }
264
+ }
265
+
266
+ int simuFetch(char* dst,int ask)
267
+ {
268
+ // printf("simuFetch %d\n",PlayState);
269
+ if (PlayState==PLAYST_IDLE) return 0;
270
+ if (PlayState==PLAYST_START) simuFetchStart();
271
+ if (PlayState==PLAYST_PLAY) return simuFetchPlay(dst,ask);
272
+ return 0;
273
+ }
274
+
275
+
276
+ extern HWND mainwin;
277
+
278
+ int OutFillBuffer(int i)
279
+ {
280
+ int k,len;
281
+
282
+ if (WaveOutBuf[i].lpData==NULL) return 0;
283
+ k=simuFetch(WaveOutBuf[i].lpData,WaveOutBuf[i].dwDataSize);
284
+ // printf("#outfill\n");
285
+ len=WaveOutBuf[i].dwDataSize-k;
286
+ if (len>0) memset(WaveOutBuf[i].lpData+k,0,len);
287
+
288
+ return 0;
289
+ }
290
+ int InFillBuffer(int i)
291
+ {
292
+ char *p;
293
+
294
+ if (WaveInBuf[i].lpData==NULL) return 0;
295
+
296
+ p=audioRecFeed_begin(256);
297
+ adpcmencode(WaveInBuf[i].lpData,p);
298
+ /* p=audioRecFeed_begin(WaveInBuf[i].dwDataSize);
299
+ memcpy(p,WaveInBuf[i].lpData,WaveInBuf[i].dwDataSize);
300
+ */ audioRecFeed_end();
301
+
302
+ return 0;
303
+ }
304
+
305
+ void WaveOutFreeAll()
306
+ {
307
+ int i;
308
+ for (i=0;i<nPlayBuffers;i++)
309
+ {
310
+ waveOutUnprepareHeader(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
311
+
312
+ if (WaveOutBuf[i].lpData != NULL)
313
+ GlobalUnlock(WaveOutBuf[i].hData);
314
+ if (WaveOutBuf[i].hData)
315
+ GlobalFree(WaveOutBuf[i].hData);
316
+
317
+ if (WaveOutBuf[i].lpWaveHdr != NULL)
318
+ GlobalUnlock(WaveOutBuf[i].hWaveHdr);
319
+ if (WaveOutBuf[i].hWaveHdr)
320
+ GlobalFree(WaveOutBuf[i].hWaveHdr);
321
+ }
322
+ memset(WaveOutBuf,0,nPlayBuffers*sizeof(WaveOutBuffer));
323
+ }
324
+ void WaveInFreeAll()
325
+ {
326
+ int i;
327
+ for (i=0;i<nRecBuffers;i++)
328
+ {
329
+ waveInUnprepareHeader(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
330
+
331
+ if (WaveInBuf[i].lpData != NULL)
332
+ GlobalUnlock(WaveInBuf[i].hData);
333
+ if (WaveInBuf[i].hData)
334
+ GlobalFree(WaveInBuf[i].hData);
335
+
336
+ if (WaveInBuf[i].lpWaveHdr != NULL)
337
+ GlobalUnlock(WaveInBuf[i].hWaveHdr);
338
+ if (WaveInBuf[i].hWaveHdr)
339
+ GlobalFree(WaveInBuf[i].hWaveHdr);
340
+ }
341
+ memset(WaveInBuf,0,nRecBuffers*sizeof(WaveInBuffer));
342
+ RecordInitialised=FALSE;
343
+ SendInBuffers=0;
344
+ }
345
+
346
+
347
+ int PlayInit(int rate,int ChannelSize,int nbBuffers)
348
+ {
349
+ UINT wResult;
350
+ PCMWAVEFORMAT pcmWaveFormat;
351
+ int i;
352
+
353
+ // Open a waveform device for output using window callback.
354
+ int nChannels=2;
355
+ int BitsPerSample=16;
356
+
357
+ pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM;
358
+ pcmWaveFormat.wf.nChannels=nChannels;
359
+ pcmWaveFormat.wf.nSamplesPerSec=rate;
360
+ pcmWaveFormat.wf.nAvgBytesPerSec=rate*nChannels*(BitsPerSample/8);
361
+ pcmWaveFormat.wf.nBlockAlign=(nChannels*BitsPerSample)/8;
362
+ pcmWaveFormat.wBitsPerSample=BitsPerSample;
363
+
364
+ wResult=waveOutOpen((LPHWAVEOUT)&hWaveOut, WAVE_MAPPER,
365
+ (LPWAVEFORMATEX)&pcmWaveFormat,
366
+ (LONG)mainwin, 0L, CALLBACK_WINDOW);
367
+ if (wResult != 0) return 0;
368
+
369
+ nPlayBuffers=nbBuffers;
370
+ if (WaveOutBuf) free(WaveOutBuf);
371
+ WaveOutBuf=(WaveOutBuffer*)malloc(sizeof(WaveOutBuffer)*nPlayBuffers);
372
+
373
+
374
+ for (i=0;i<nPlayBuffers;i++)
375
+ {
376
+ WaveOutBuf[i].nChannels=nChannels;
377
+ WaveOutBuf[i].Rate=rate;
378
+ WaveOutBuf[i].Resolution=BitsPerSample;
379
+ WaveOutBuf[i].hwnd=mainwin;
380
+ WaveOutBuf[i].dwDataSize=ChannelSize;
381
+
382
+ // Allocate and lock memory for the waveform data.
383
+ WaveOutBuf[i].hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, WaveOutBuf[i].dwDataSize );
384
+ if (!WaveOutBuf[i].hData) return 0;
385
+ if ((WaveOutBuf[i].lpData = (char*)GlobalLock(WaveOutBuf[i].hData)) == NULL) {
386
+ WaveOutFreeAll();
387
+ return 0;
388
+ }
389
+
390
+ // Allocate and lock memory for the header.
391
+ WaveOutBuf[i].hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,(DWORD) sizeof(WAVEHDR));
392
+ if (WaveOutBuf[i].hWaveHdr == NULL)
393
+ {
394
+ WaveOutFreeAll();
395
+ return 0;
396
+ }
397
+
398
+ WaveOutBuf[i].lpWaveHdr = (LPWAVEHDR) GlobalLock(WaveOutBuf[i].hWaveHdr);
399
+ if (WaveOutBuf[i].lpWaveHdr == NULL)
400
+ {
401
+ WaveOutFreeAll();
402
+ return 0;
403
+ }
404
+
405
+ // After allocation, set up and prepare header.
406
+
407
+ WaveOutBuf[i].lpWaveHdr->lpData = WaveOutBuf[i].lpData;
408
+ WaveOutBuf[i].lpWaveHdr->dwBufferLength = WaveOutBuf[i].dwDataSize;
409
+ WaveOutBuf[i].lpWaveHdr->dwUser = i;
410
+ WaveOutBuf[i].lpWaveHdr->dwFlags = 0L;
411
+ WaveOutBuf[i].lpWaveHdr->dwLoops = 1L;
412
+ wResult = waveOutPrepareHeader(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
413
+ if (wResult != 0)
414
+ {
415
+ wResult=waveOutClose(hWaveOut);
416
+ WaveOutFreeAll();
417
+ return 0;
418
+ }
419
+ memset(WaveOutBuf[i].lpData,0,WaveOutBuf[i].dwDataSize);
420
+ }
421
+
422
+ for (i=0;i<nPlayBuffers;i++)
423
+ {
424
+ // Now the data block can be sent to the output device. The
425
+ // waveOutWrite function returns immediately and waveform
426
+ // data is sent to the output device in the background.
427
+ wResult = waveOutWrite(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
428
+ if (wResult != 0)
429
+ {
430
+ wResult=waveOutReset(hWaveOut);
431
+ wResult=waveOutClose(hWaveOut);
432
+ WaveOutFreeAll();
433
+ return 0;
434
+ }
435
+ }
436
+ return 1;
437
+ }
438
+
439
+ int RecStart(int rate,int ChannelSize,int nbBuffers)
440
+ {
441
+ UINT wResult;
442
+ PCMWAVEFORMAT pcmWaveFormat;
443
+ int r,i;
444
+ int BitsPerSample=16;
445
+ int nChannels=1;
446
+ if (RecordInitialised) return 0;
447
+ nRecBuffers=nbBuffers;
448
+ if (WaveInBuf) free(WaveInBuf);
449
+ WaveInBuf=(WaveInBuffer*)malloc(sizeof(WaveInBuffer)*nRecBuffers);
450
+ // memset(WaveInBuf,0,nRecBuffers*sizeof(WaveInBuffer));
451
+ // Open a waveform device for output using window callback.
452
+
453
+ if (BitsPerSample>8) BitsPerSample=16;
454
+ pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM;
455
+ pcmWaveFormat.wf.nChannels=nChannels;
456
+ pcmWaveFormat.wf.nSamplesPerSec=rate;
457
+ pcmWaveFormat.wf.nAvgBytesPerSec=rate*nChannels*(BitsPerSample/8);
458
+ pcmWaveFormat.wf.nBlockAlign=(nChannels*BitsPerSample)/8;
459
+ pcmWaveFormat.wBitsPerSample=BitsPerSample;
460
+
461
+ if (r=waveInOpen((LPHWAVEIN)&hWaveIn, WAVE_MAPPER,
462
+ (LPWAVEFORMATEX)&pcmWaveFormat,
463
+ (LONG)mainwin, 0L, CALLBACK_WINDOW))
464
+ {
465
+ // MMechostr(MSKDEBUG,"Record: Unable to Open Device\n");
466
+ switch (r)
467
+ {
468
+ case MMSYSERR_ALLOCATED : printf("Device allready allocated\n"); break;
469
+ case MMSYSERR_BADDEVICEID : printf("Bad device ID\n"); break;
470
+ case MMSYSERR_NODRIVER : printf("No driver\n"); break;
471
+ case WAVERR_BADFORMAT : printf("Bad Format\n"); break;
472
+ case MMSYSERR_NOMEM : printf("No memory\n"); break;
473
+ }
474
+ return 0;
475
+ }
476
+
477
+ for (i=0;i<nRecBuffers;i++)
478
+ {
479
+ WaveInBuf[i].nChannels=nChannels;
480
+ WaveInBuf[i].Rate=rate;
481
+ WaveInBuf[i].Resolution=BitsPerSample;
482
+ WaveInBuf[i].dwDataSize=nChannels*(BitsPerSample/8)*ChannelSize;
483
+ WaveInBuf[i].hwnd=mainwin;
484
+ // Allocate and lock memory for the waveform data.
485
+ WaveInBuf[i].hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, WaveInBuf[i].dwDataSize );
486
+ if (!WaveInBuf[i].hData)
487
+ {
488
+ // MMechostr(MSKDEBUG,"Record: Unable to Allocate memory\n");
489
+ return 0;
490
+ }
491
+ if ((WaveInBuf[i].lpData = (char*)GlobalLock(WaveInBuf[i].hData)) == NULL) {
492
+ // MMechostr(MSKDEBUG,"Record: Unable to Lock Memory\n");
493
+ WaveInFreeAll();
494
+ return 0;
495
+ }
496
+
497
+ // Allocate and lock memory for the header.
498
+ WaveInBuf[i].hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
499
+ (DWORD) sizeof(WAVEHDR));
500
+ if (WaveInBuf[i].hWaveHdr == NULL)
501
+ {
502
+ WaveInFreeAll();
503
+ // MMechostr(MSKDEBUG,"Record: Unable to allocate wave header\n");
504
+ return 0;
505
+ }
506
+
507
+ WaveInBuf[i].lpWaveHdr = (LPWAVEHDR) GlobalLock(WaveInBuf[i].hWaveHdr);
508
+ if (WaveInBuf[i].lpWaveHdr == NULL)
509
+ {
510
+ WaveInFreeAll();
511
+ // MMechostr(MSKDEBUG,"Record: Unable to Lock wave header memory\n");
512
+ return 0;
513
+ }
514
+
515
+ // After allocation, set up and prepare header.
516
+
517
+ WaveInBuf[i].lpWaveHdr->lpData = WaveInBuf[i].lpData;
518
+ WaveInBuf[i].lpWaveHdr->dwBufferLength = WaveInBuf[i].dwDataSize;
519
+ WaveInBuf[i].lpWaveHdr->dwUser = i;
520
+ WaveInBuf[i].lpWaveHdr->dwFlags = 0L;
521
+ WaveInBuf[i].lpWaveHdr->dwLoops = 0L;
522
+ wResult=waveInPrepareHeader(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
523
+ if (wResult != 0)
524
+ {
525
+ char buf[MAXERRORLENGTH];
526
+ WaveInFreeAll();
527
+ // MMechostr(MSKDEBUG,"PrepareRecBuf: Unable to write block to the device\n");
528
+ waveOutGetErrorText(wResult,buf,MAXERRORLENGTH);
529
+ // MMechostr(MSKDEBUG,buf);
530
+ // MMechostr(MSKDEBUG,"\n");
531
+ return 0;
532
+ }
533
+
534
+ }
535
+
536
+ for (i=0;i<nRecBuffers;i++)
537
+ {
538
+ // Now the data block can be sent to the input device. The
539
+ // waveOutWrite function returns immediately and waveform
540
+ // data is sent to the output device in the background.
541
+ wResult = waveInAddBuffer(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
542
+ if (wResult != 0)
543
+ {
544
+ WaveInFreeAll();
545
+ // MMechostr(MSKDEBUG,"Record: Unable to write block to the device\n");
546
+ return 0;
547
+ }
548
+ SendInBuffers++;
549
+ }
550
+ waveInStart(hWaveIn);
551
+ StopRecord=FALSE;
552
+ RecordInitialised=TRUE;
553
+
554
+ return 1;
555
+ }
556
+
557
+ int PlayStart()
558
+ {
559
+ PlayState=PLAYST_START;
560
+ PlayStateEof=0;
561
+ mp3simuinit();
562
+ return 0;
563
+ }
564
+
565
+ int PlayStop()
566
+ {
567
+ PlayState=PLAYST_IDLE;
568
+ return 0;
569
+ }
570
+
571
+ int PlayEof()
572
+ {
573
+ PlayStateEof=1;
574
+ return 0;
575
+ }
576
+
577
+ int RecStop()
578
+ {
579
+ // waveInReset(hWaveIn);
580
+ // waveInClose(hWaveIn);
581
+ StopRecord=TRUE;
582
+ return 0;
583
+ }
584
+
585
+ int PlayData(int i)
586
+ {
587
+ WORD wResult;
588
+
589
+ OutFillBuffer(i);
590
+ wResult = waveOutWrite(hWaveOut,WaveOutBuf[i].lpWaveHdr,sizeof(WAVEHDR));
591
+ if (wResult != 0)
592
+ {
593
+ WaveOutFreeAll();
594
+ return 0;
595
+ }
596
+ return 1;
597
+ }
598
+
599
+ int RecData(int i)
600
+ {
601
+ WORD wResult;
602
+
603
+ SendInBuffers--;
604
+ if (!StopRecord)
605
+ {
606
+ InFillBuffer(i);
607
+ wResult = waveInAddBuffer(hWaveIn,WaveInBuf[i].lpWaveHdr,sizeof(WAVEHDR));
608
+ if (wResult != 0)
609
+ {
610
+ WaveInFreeAll();
611
+ return 0;
612
+ }
613
+ SendInBuffers++;
614
+ }
615
+ else
616
+ {
617
+ if (!SendInBuffers)
618
+ {
619
+ WaveInFreeAll();
620
+ StopRecord=FALSE;
621
+ waveInClose(hWaveIn);
622
+ }
623
+ }
624
+ return 1;
625
+ }
626
+
627
+ int audioEventPlayData(int eventId, int wParam, int lParam)
628
+ {
629
+ // printf("###play cb\n");
630
+ PlayData(((LPWAVEHDR)lParam)->dwUser);
631
+ return 0;
632
+ }
633
+
634
+ int audioEventRecData(int eventId, int wParam, int lParam)
635
+ {
636
+ // printf("###rec cb\n");
637
+ RecData(((LPWAVEHDR)lParam)->dwUser);
638
+ return 0;
639
+ }
640
+
641
+ void audioSetVolume(int vol)
642
+ {
643
+ vol&=255;
644
+ waveOutSetVolume(hWaveOut,(vol<<24)+(vol<<8));
645
+ }
646
+
647
+ int simuaudioinit()
648
+ {
649
+ nPlayBuffers=0;
650
+ PlayState=PLAYST_IDLE;
651
+
652
+ nRecBuffers=0;
653
+ SendInBuffers=0;
654
+ StopRecord=FALSE;
655
+ RecordInitialised=FALSE;
656
+
657
+ PlayInit(44100,10000 //22050
658
+ ,8);
659
+
660
+ return 0;
661
+ }
662
+
@@ -0,0 +1,74 @@
1
+ //-------------------
2
+ // MV
3
+ // version WIN32
4
+ // Sylvain Huet
5
+ // Premiere version : 07/01/2003
6
+ // Derniere mise a jour : 07/01/2003
7
+ //
8
+
9
+ #ifndef _AUDIO_
10
+ #define _AUDIO_
11
+
12
+
13
+ // Global variables & type defs
14
+ typedef struct WaveOutBuffer
15
+ {
16
+ WORD Rate;
17
+ BYTE nChannels;
18
+ LONG dwDataSize;
19
+ BYTE Resolution;
20
+ HWND hwnd;
21
+ HGLOBAL hWaveHdr;
22
+ LPWAVEHDR lpWaveHdr;
23
+ HANDLE hData; // handle of waveform data memory
24
+ HPSTR lpData; // pointer to waveform data memory
25
+ } WaveOutBuffer;
26
+
27
+ typedef struct WaveInBuffer
28
+ {
29
+ WORD Rate;
30
+ BYTE nChannels;
31
+ LONG dwDataSize;
32
+ BYTE Resolution;
33
+ HWND hwnd;
34
+ HGLOBAL hWaveHdr;
35
+ LPWAVEHDR lpWaveHdr;
36
+ HANDLE hData; // handle of waveform data memory
37
+ HPSTR lpData; // pointer to waveform data memory
38
+ } WaveInBuffer;
39
+
40
+ // audio
41
+ extern HWAVEOUT hWaveOut;
42
+ extern HWAVEIN hWaveIn;
43
+
44
+ extern int nPlayBuffers;
45
+ extern WaveOutBuffer* WaveOutBuf;
46
+ extern int SendOutBuffers;
47
+ extern BOOL StopPlayback;
48
+ extern BOOL PlaybackInitialised;
49
+
50
+ extern int nRecBuffers;
51
+ extern WaveInBuffer* WaveInBuf;
52
+ extern int SendInBuffers;
53
+ extern BOOL StopRecord;
54
+ extern BOOL RecordInitialised;
55
+
56
+ int PlayStart();
57
+ int PlayStop();
58
+ int PlayEof();
59
+
60
+ int RecStart(int rate,int ChannelSize,int nbBuffers);
61
+ int RecStop();
62
+ int RecData(int i);
63
+
64
+ int audioEventPlayData(int eventId, int wParam, int lParam);
65
+ int audioEventRecData(int eventId, int wParam, int lParam);
66
+ void audioSetVolume(int vol);
67
+ int simuaudioinit();
68
+
69
+
70
+ void mp3simuinit();
71
+ int mp3simuFetch(char* dst,int ask);
72
+
73
+ #endif
74
+