author | Claus Gittinger <cg@exept.de> |
Fri, 03 Jan 2020 22:23:55 +0100 | |
changeset 5410 | 24114a427d3d |
parent 5409 | 36e736c349b6 |
child 5411 | 67e5d96c2f7a |
permissions | -rw-r--r-- |
4 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
45 | 3 |
All Rights Reserved |
4 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
918 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3482 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
0 | 16 |
FileStream subclass:#SoundStream |
2418
4ea40741b2b5
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
17 |
instanceVariableNames:'sampleRate numberOfChannels bitsPerSample audioFormat handle1 |
5409 | 18 |
handle2 bufferOffset bufferSize lastErrorString fragmentSize |
19 |
openMode openAttributes' |
|
5402 | 20 |
classVariableNames:'IsInitialized UnsupportedOperationSignal ConcreteClass' |
259 | 21 |
poolDictionaries:'' |
22 |
category:'Streams-External' |
|
0 | 23 |
! |
24 |
||
5402 | 25 |
SoundStream subclass:#DevAudio |
26 |
instanceVariableNames:'' |
|
27 |
classVariableNames:'' |
|
28 |
poolDictionaries:'' |
|
29 |
privateIn:SoundStream |
|
30 |
! |
|
31 |
||
32 |
SoundStream subclass:#IRISAudio |
|
33 |
instanceVariableNames:'' |
|
34 |
classVariableNames:'' |
|
35 |
poolDictionaries:'' |
|
36 |
privateIn:SoundStream |
|
37 |
! |
|
38 |
||
39 |
SoundStream subclass:#PortAudio |
|
40 |
instanceVariableNames:'' |
|
41 |
classVariableNames:'' |
|
42 |
poolDictionaries:'' |
|
43 |
privateIn:SoundStream |
|
44 |
! |
|
45 |
||
46 |
SoundStream subclass:#Win32DirectSound |
|
47 |
instanceVariableNames:'' |
|
48 |
classVariableNames:'' |
|
49 |
poolDictionaries:'' |
|
50 |
privateIn:SoundStream |
|
51 |
! |
|
52 |
||
53 |
SoundStream subclass:#Win32WaveSound |
|
54 |
instanceVariableNames:'' |
|
55 |
classVariableNames:'' |
|
56 |
poolDictionaries:'' |
|
57 |
privateIn:SoundStream |
|
58 |
! |
|
59 |
||
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
60 |
!SoundStream primitiveDefinitions! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
61 |
%{ |
725 | 62 |
|
2818 | 63 |
#include "stxOSDefs.h" |
64 |
||
3846 | 65 |
#ifdef __win32__ |
2818 | 66 |
# ifndef NO_SOUND |
5405 | 67 |
# define xxxSUPPORT_WIN32_DIRECTSOUND |
5406 | 68 |
# ifndef SUPPORT_WIN32_WAVESOUND |
69 |
# define SUPPORT_WIN32_WAVESOUND |
|
70 |
# endif |
|
2818 | 71 |
# endif |
1583 | 72 |
#endif |
725 | 73 |
|
3846 | 74 |
#ifdef __iris__ |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
75 |
# ifndef IRIX5 |
5406 | 76 |
# ifndef SUPPORT_IRIS_AUDIO |
77 |
# define SUPPORT_IRIS_AUDIO |
|
78 |
# endif |
|
1583 | 79 |
# endif |
80 |
#endif |
|
81 |
||
5405 | 82 |
#ifndef SUPPORT_IRIS_AUDIO |
83 |
# ifndef SUPPORT_ALSA_AUDIO |
|
1583 | 84 |
# ifdef LINUX |
5406 | 85 |
# ifndef SUPPORT_DEV_AUDIO |
86 |
# define SUPPORT_DEV_AUDIO |
|
87 |
# endif |
|
5400 | 88 |
# endif |
89 |
# endif |
|
90 |
#endif |
|
91 |
||
92 |
#ifndef NO_SOUND |
|
93 |
# ifndef USED_AUDIO |
|
94 |
# ifdef __osx__ |
|
5406 | 95 |
# ifndef SUPPORT_PORTAUDIO |
96 |
# define SUPPORT_PORTAUDIO |
|
97 |
# endif |
|
1583 | 98 |
# endif |
99 |
# endif |
|
100 |
#endif |
|
101 |
||
5405 | 102 |
#ifdef SUPPORT_IRIS_AUDIO |
1583 | 103 |
# include <audio.h> |
104 |
# define __ALportVal(o) (ALport)(__externalAddressVal(o)) |
|
105 |
# define alPort handle1 |
|
106 |
#endif |
|
107 |
||
5405 | 108 |
#ifdef SUPPORT_ALSA_AUDIO |
1583 | 109 |
# ifdef LINUX |
110 |
# include <alsa/asoundlib.h> |
|
111 |
# define MAX_NR_OF_CHANNELS 8 |
|
112 |
# define DEBUG_SOUND |
|
113 |
# define readHandle handle1 |
|
114 |
# define writeHandle handle2 |
|
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
115 |
# endif |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
116 |
#endif |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
117 |
|
5405 | 118 |
#ifdef SUPPORT_DEV_AUDIO |
1583 | 119 |
# ifdef LINUX |
120 |
# include <stdio.h> |
|
121 |
# include <sys/soundcard.h> |
|
122 |
# define DEV_AUDIO_DEFAULT_FREQ (8000) |
|
123 |
# define DEV_AUDIO_DEFAULT_BUFFERSIZE (16384) |
|
124 |
# endif |
|
592 | 125 |
|
1583 | 126 |
# ifdef FREEBSD |
127 |
# include <stdio.h> |
|
128 |
# include <sys/time.h> |
|
129 |
# include <sys/ioctl.h> |
|
130 |
# include <machine/pcaudioio.h> |
|
131 |
# define DEV_AUDIO_DEFAULT_FREQ (8000) |
|
132 |
# define DEV_AUDIO_DEFAULT_BUFFERSIZE (16384) |
|
133 |
# endif |
|
594 | 134 |
|
1583 | 135 |
# if defined(sunos) || defined(solaris) |
136 |
# include <stdio.h> |
|
137 |
# ifdef solaris |
|
138 |
# include <sys/audioio.h> |
|
139 |
# else |
|
140 |
# include <sun/audioio.h> |
|
141 |
# endif |
|
142 |
# define DEV_AUDIO_DEFAULT_FREQ (8000) |
|
143 |
# define DEV_AUDIO_DEFAULT_BUFFERSIZE (16384) |
|
724 | 144 |
# endif |
145 |
||
1583 | 146 |
# if defined(hpux) |
147 |
# include <stdio.h> |
|
148 |
# include <sys/ioctl.h> |
|
149 |
# include <sys/inode.h> |
|
150 |
# include <sys/audio.h> |
|
151 |
# include <sys/time.h> |
|
152 |
# include <unistd.h> |
|
153 |
# define DEV_AUDIO_DEFAULT_FREQ (22050) |
|
154 |
# define DEV_AUDIO_DEFAULT_BUFFERSIZE (16384) |
|
155 |
# endif |
|
5405 | 156 |
#endif /* SUPPORT_DEV_AUDIO */ |
157 |
||
158 |
#ifdef SUPPORT_PORTAUDIO |
|
5400 | 159 |
// # include "pthread.h" |
160 |
# include "portaudio.h" |
|
161 |
# define PORTAUDIO_DEFAULT_FREQ (44100) |
|
162 |
# define PORTAUDIO_DEFAULT_BUFFERSIZE (16384) |
|
163 |
||
164 |
# define xINITLOCK(lk) { pthread_mutex_init(&(lk), NULL); } |
|
165 |
# define xRELEASELOCK(lk) { pthread_mutex_destroy(&(lk)); } |
|
166 |
# define xLOCK(lk) { pthread_mutex_lock(&(lk)); } |
|
167 |
# define xUNLOCK(lk) { pthread_mutex_unlock(&(lk)); } |
|
168 |
# define xMUTEX pthread_mutex_t |
|
169 |
||
170 |
# define INITLOCK(lk) { (lk) = 0; } |
|
171 |
# define RELEASELOCK(lk) { (lk) = 0; } |
|
172 |
# define LOCK(lk) { while ((lk)) ; (lk) = 1; } |
|
173 |
# define UNLOCK(lk) { (lk) = 0; } |
|
174 |
# define MUTEX int |
|
175 |
||
5405 | 176 |
#endif // SUPPORT_PORTAUDIO |
720 | 177 |
|
3846 | 178 |
#ifdef __win32__ |
712 | 179 |
# define _WIN32 |
180 |
||
181 |
# undef INT |
|
182 |
# undef Array |
|
183 |
# undef Number |
|
184 |
# undef Method |
|
185 |
# undef Point |
|
186 |
# undef Rectangle |
|
187 |
# undef Block |
|
188 |
# undef String |
|
189 |
# undef Message |
|
190 |
# undef Object |
|
191 |
# undef Context |
|
753 | 192 |
# undef Time |
193 |
# undef Date |
|
1853
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
194 |
# undef Process |
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
195 |
# undef Processor |
861 | 196 |
# undef Set |
197 |
# undef Signal |
|
198 |
# undef Delay |
|
712 | 199 |
|
200 |
/* # include <stdarg.h> /* */ |
|
201 |
# include <stdio.h> /* */ |
|
202 |
# include <windows.h> |
|
721 | 203 |
|
5405 | 204 |
# ifdef SUPPORT_WIN32_DIRECTSOUND |
721 | 205 |
# define CINTERFACE |
206 |
# include "dsound.h" |
|
207 |
# define __DirectSoundVal(o) (LPDIRECTSOUND)(__externalAddressVal(o)) |
|
208 |
# define __DSBufferVal(o) (LPDIRECTSOUNDBUFFER)(__externalAddressVal(o)) |
|
209 |
||
210 |
# define RT_BUFFER_SIZE 4096 |
|
211 |
# define NBUFS 4 |
|
212 |
||
213 |
# define pDirectSound handle1 |
|
214 |
# define pDSBuffer handle2 |
|
215 |
||
216 |
# else /* USE WAVE... */ |
|
5405 | 217 |
# ifdef SUPPORT_WIN32_WAVESOUND |
2818 | 218 |
# define __WaveHandleVal(o) (HWAVEOUT)(__externalAddressVal(o)) |
219 |
# define MAXBUF 10 /* Maximum number of buffers */ |
|
220 |
# define DATALEN 2048 /* Size of wave data buffer */ |
|
721 | 221 |
|
222 |
struct buf { |
|
223 |
WAVEHDR hdr; /* Wave data header */ |
|
722 | 224 |
char data[DATALEN]; /* Actual wave data */ |
721 | 225 |
struct buf *next; /* Next buffer in free list */ |
226 |
}; |
|
227 |
||
722 | 228 |
static struct buf *free_list = NULL; /* List of available buffers */ |
229 |
static CRITICAL_SECTION free_list_lock; |
|
230 |
static int free_buffers = 0; /* Number of buffers in free list */ |
|
231 |
static int total_buffers = 0; /* Total number of buffers allocated */ |
|
232 |
static HANDLE free_buffer_event; /* Automatic-reset event triggered whenever */ |
|
721 | 233 |
|
2818 | 234 |
# define waveHandle handle1 |
721 | 235 |
|
5405 | 236 |
# endif /* SUPPORT_WIN32_WAVESOUND */ |
237 |
# endif /* SUPPORT_WIN32_DIRECTSOUND */ |
|
712 | 238 |
|
239 |
# ifdef __DEF_Array |
|
240 |
# define Array __DEF_Array |
|
241 |
# endif |
|
242 |
# ifdef __DEF_Number |
|
243 |
# define Number __DEF_Number |
|
244 |
# endif |
|
245 |
# ifdef __DEF_Method |
|
246 |
# define Method __DEF_Method |
|
247 |
# endif |
|
248 |
# ifdef __DEF_Point |
|
249 |
# define Point __DEF_Point |
|
250 |
# endif |
|
251 |
# ifdef __DEF_Block |
|
252 |
# define Block __DEF_Block |
|
253 |
# endif |
|
254 |
# ifdef __DEF_String |
|
255 |
# define String __DEF_String |
|
256 |
# endif |
|
257 |
# ifdef __DEF_Message |
|
258 |
# define Message __DEF_Message |
|
259 |
# endif |
|
260 |
# ifdef __DEF_Object |
|
261 |
# define Object __DEF_Object |
|
262 |
# endif |
|
263 |
# ifdef __DEF_Context |
|
264 |
# define Context __DEF_Context |
|
265 |
# endif |
|
753 | 266 |
# ifdef __DEF_Time |
267 |
# define Time __DEF_Time |
|
268 |
# endif |
|
269 |
# ifdef __DEF_Date |
|
270 |
# define Date __DEF_Date |
|
271 |
# endif |
|
1853
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
272 |
#ifdef __DEF_Process |
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
273 |
# define Process __DEF_Process |
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
274 |
#endif |
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
275 |
#ifdef __DEF_Processor |
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
276 |
# define Processor __DEF_Processor |
b9c4a09bfc6a
Fix name clashes with windows header files
Stefan Vogel <sv@exept.de>
parents:
1839
diff
changeset
|
277 |
#endif |
861 | 278 |
#ifdef __DEF_Set |
279 |
# define Set __DEF_Set |
|
280 |
#endif |
|
281 |
#ifdef __DEF_Signal |
|
282 |
# define Signal __DEF_Signal |
|
283 |
#endif |
|
284 |
#ifdef __DEF_Delay |
|
285 |
# define Delay __DEF_Delay |
|
286 |
#endif |
|
753 | 287 |
|
3000 | 288 |
# define INT STX_INT |
289 |
# define UINT STX_UINT |
|
712 | 290 |
|
3846 | 291 |
#endif /* __win32__ */ |
712 | 292 |
|
1583 | 293 |
#ifdef DEBUG_SOUND |
1839 | 294 |
# define DPRINTF(x) console_printf x |
1583 | 295 |
#else |
296 |
# define DPRINTF(x) /* as nothing */ |
|
297 |
#endif |
|
298 |
||
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
299 |
%} |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
300 |
! ! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
301 |
|
721 | 302 |
!SoundStream primitiveFunctions! |
303 |
%{ |
|
5400 | 304 |
|
5405 | 305 |
#ifdef SUPPORT_WIN32_WAVESOUND |
5400 | 306 |
|
307 |
/* |
|
308 |
* This routine will be called by windows when audio is needed. |
|
309 |
* It may called at interrupt level and by a separate thread, so don't do anything |
|
310 |
* that could mess up the system like calling malloc() or free(). |
|
311 |
*/ |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
312 |
static void CALLBACK |
3000 | 313 |
waveCallBack(HWAVE waveHandle, UINT msg, DWORD_PTR inst, DWORD_PTR p1, DWORD_PTR p2) |
721 | 314 |
{ |
315 |
if (msg == MM_WOM_DONE) { |
|
316 |
struct buf *bp = (struct buf *)p1; |
|
3000 | 317 |
|
721 | 318 |
EnterCriticalSection(&free_list_lock); |
319 |
bp->next = free_list; |
|
320 |
free_list = bp; |
|
321 |
free_buffers++; |
|
322 |
SetEvent(free_buffer_event); |
|
323 |
LeaveCriticalSection(&free_list_lock); |
|
324 |
} |
|
325 |
} |
|
326 |
||
5405 | 327 |
#endif /* SUPPORT_WIN32_WAVESOUND */ |
328 |
||
329 |
#ifdef SUPPORT_PORTAUDIO |
|
5400 | 330 |
|
331 |
static int DEBUGGING = 0; |
|
332 |
||
333 |
struct paBuffer { |
|
334 |
void* sampleData; |
|
335 |
int bufferSize; |
|
336 |
struct paBuffer* nextBuffer; |
|
337 |
}; |
|
338 |
||
339 |
struct paStreamData { |
|
340 |
PaStream *stream; |
|
341 |
MUTEX lock; |
|
342 |
int readOffset; |
|
343 |
int bytesPerSample; // bytesPerSample (1, 2 or 4) |
|
344 |
int nChannels; // num channels (1 or 2) |
|
345 |
int hasFinished; |
|
346 |
struct paBuffer* currentBuffer; |
|
347 |
struct paBuffer* lastBuffer; |
|
348 |
struct paBuffer* freeList; |
|
349 |
}; |
|
350 |
||
351 |
/* |
|
352 |
* This routine will be called by the PortAudio engine when audio is needed. |
|
353 |
* It may called at interrupt level on some machines so don't do anything |
|
354 |
* that could mess up the system like calling malloc() or free(). |
|
355 |
*/ |
|
356 |
static int |
|
357 |
paCallback( const void *inputBuffer, void *outputBuffer, |
|
358 |
unsigned long framesPerBuffer, |
|
359 |
const PaStreamCallbackTimeInfo* timeInfo, |
|
360 |
PaStreamCallbackFlags statusFlags, |
|
361 |
void *userData ) |
|
362 |
{ |
|
363 |
struct paStreamData *paStreamData = (struct paStreamData *)userData; |
|
364 |
struct paBuffer *paBuffer = paStreamData->currentBuffer; |
|
365 |
int nBytesToOutput = paStreamData->bytesPerSample * paStreamData->nChannels * framesPerBuffer; |
|
366 |
int nBytesLeftInBuffer; |
|
367 |
int offsetInBuffer = paStreamData->readOffset; |
|
368 |
int nSent; |
|
369 |
int retVal = paContinue; |
|
370 |
||
371 |
if (paBuffer != NULL) { |
|
372 |
nBytesLeftInBuffer = paBuffer->bufferSize - offsetInBuffer; |
|
373 |
if (DEBUGGING) { |
|
374 |
fprintf(stderr, "pa: Stream Callback (%d from %p; next: %p last: %p)\n", |
|
375 |
nBytesLeftInBuffer, paBuffer, paBuffer->nextBuffer, paStreamData->lastBuffer); |
|
376 |
} |
|
377 |
} else { |
|
378 |
nBytesLeftInBuffer = 0; |
|
379 |
} |
|
380 |
if (nBytesLeftInBuffer >= nBytesToOutput) { |
|
381 |
memcpy(outputBuffer, paBuffer->sampleData+offsetInBuffer, nBytesToOutput); |
|
382 |
nSent = nBytesToOutput; |
|
5410 | 383 |
if (DEBUGGING) { |
384 |
fprintf(stderr, "pa: %d from buffer\n", nSent); |
|
385 |
} |
|
5400 | 386 |
} else { |
387 |
nSent = nBytesLeftInBuffer; |
|
388 |
if (nSent > 0) { |
|
389 |
memcpy(outputBuffer, paBuffer->sampleData+offsetInBuffer, nBytesLeftInBuffer); |
|
5410 | 390 |
if (DEBUGGING) { |
391 |
fprintf(stderr, "pa: %d from buffer\n", nBytesLeftInBuffer); |
|
392 |
} |
|
5400 | 393 |
} |
394 |
memset(outputBuffer+offsetInBuffer+nBytesLeftInBuffer, 0, nBytesToOutput-nBytesLeftInBuffer); |
|
5410 | 395 |
if (DEBUGGING) { |
396 |
fprintf(stderr, "pa: %d zeros\n", nBytesToOutput-nBytesLeftInBuffer); |
|
397 |
} |
|
5400 | 398 |
} |
399 |
LOCK(paStreamData->lock); |
|
400 |
if (paBuffer != NULL) { |
|
401 |
paStreamData->readOffset += nSent; |
|
402 |
if (paStreamData->readOffset >= paBuffer->bufferSize) { |
|
403 |
// next buffer |
|
404 |
paStreamData->readOffset = 0; |
|
405 |
paStreamData->currentBuffer = paBuffer->nextBuffer; |
|
406 |
// this buffer onto freeList |
|
407 |
paBuffer->nextBuffer = paStreamData->freeList; |
|
408 |
paStreamData->freeList = paBuffer; |
|
409 |
if (DEBUGGING) { |
|
410 |
fprintf(stderr, "pa: put on freelist: %p\n", paBuffer); |
|
411 |
} |
|
412 |
if (paStreamData->currentBuffer == NULL) { |
|
413 |
if (DEBUGGING) { |
|
414 |
fprintf(stderr, "pa: done with last buffer\n"); |
|
415 |
} |
|
416 |
paStreamData->lastBuffer = NULL; |
|
417 |
retVal = paComplete; |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
UNLOCK(paStreamData->lock); |
|
422 |
||
423 |
return paContinue; |
|
424 |
} |
|
425 |
||
426 |
/* |
|
427 |
* This routine is called by portaudio when playback is done. |
|
428 |
*/ |
|
429 |
static void |
|
430 |
paStreamFinished( void* userData ) |
|
431 |
{ |
|
432 |
struct paStreamData *paStreamData = (struct paStreamData *)userData; |
|
433 |
||
434 |
paStreamData->hasFinished = 1; |
|
435 |
if (DEBUGGING) { |
|
436 |
fprintf(stderr, "pa: Stream Completed\n" ); |
|
437 |
} |
|
438 |
} |
|
439 |
||
5405 | 440 |
#endif /* SUPPORT_PORTAUDIO */ |
721 | 441 |
|
442 |
%} |
|
788 | 443 |
! ! |
444 |
||
28 | 445 |
!SoundStream class methodsFor:'documentation'! |
446 |
||
31 | 447 |
copyright |
448 |
" |
|
449 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
45 | 450 |
All Rights Reserved |
31 | 451 |
|
452 |
This software is furnished under a license and may be used |
|
453 |
only in accordance with the terms of that license and with the |
|
454 |
inclusion of the above copyright notice. This software may not |
|
455 |
be provided or otherwise made available to, or used by, any |
|
456 |
other person. No title to or ownership of the software is |
|
457 |
hereby transferred. |
|
458 |
" |
|
459 |
! |
|
460 |
||
28 | 461 |
documentation |
462 |
" |
|
5400 | 463 |
Preliminary (unfinished) interface to an audio device. |
464 |
Currently works with |
|
465 |
LINUXs |
|
466 |
SUNs /dev/audio driver |
|
467 |
IRIX (indy). |
|
593 | 468 |
On iris, the default setup is for 8 bit mono |
28 | 469 |
so I can play the standard sound files I have here. |
470 |
It needs much more work, for stereo, different sampling rates etc. |
|
471 |
||
592 | 472 |
This is an experimental class - its interface & implementation |
473 |
may change in the future. |
|
474 |
||
259 | 475 |
[author:] |
277
ab17540f022a
oops - your last checkin was garbage
Claus Gittinger <cg@exept.de>
parents:
274
diff
changeset
|
476 |
Claus Gittinger |
28 | 477 |
" |
152 | 478 |
! ! |
45 | 479 |
|
595 | 480 |
!SoundStream class methodsFor:'initialization'! |
481 |
||
482 |
initialize |
|
483 |
UnsupportedOperationSignal isNil ifTrue:[ |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
484 |
UnsupportedOperationSignal := StreamError newSignalMayProceed:true. |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
485 |
UnsupportedOperationSignal nameClass:self message:#unsupportedOperationSignal. |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
486 |
UnsupportedOperationSignal notifierString:'unsupported operation'. |
595 | 487 |
] |
488 |
! ! |
|
489 |
||
0 | 490 |
!SoundStream class methodsFor:'instance creation'! |
491 |
||
5402 | 492 |
determineConcreteClass |
493 |
self allSubclassesDo:[:each | |
|
5405 | 494 |
each isAbstract ifFalse:[ |
495 |
each isSupported ifTrue:[ |
|
496 |
^ each |
|
497 |
] |
|
498 |
] |
|
5402 | 499 |
]. |
500 |
^ nil |
|
501 |
! |
|
502 |
||
503 |
new |
|
504 |
self == SoundStream ifTrue:[ |
|
5405 | 505 |
ConcreteClass isNil ifTrue:[ |
506 |
ConcreteClass := self determineConcreteClass. |
|
507 |
ConcreteClass isNil ifTrue:[ |
|
508 |
self openErrorSignal raiseErrorString:'missing sound support'. |
|
509 |
]. |
|
510 |
]. |
|
511 |
^ ConcreteClass new. |
|
5402 | 512 |
]. |
513 |
^ self basicNew initialize |
|
514 |
||
515 |
" |
|
516 |
SoundStream new |
|
517 |
" |
|
518 |
! |
|
519 |
||
932 | 520 |
readWrite |
521 |
"create and return a new soundStream for readWrite (i.e. filtering)" |
|
522 |
||
523 |
|newStream| |
|
5402 | 524 |
|
5403 | 525 |
newStream := self new. |
932 | 526 |
newStream openForReadWrite isNil ifTrue:[^nil]. |
527 |
^ newStream |
|
528 |
||
529 |
"SoundStream readWrite" |
|
530 |
! |
|
531 |
||
0 | 532 |
reading |
593 | 533 |
"create and return a new soundStream for reading (i.e. recording)" |
534 |
||
0 | 535 |
|newStream| |
5403 | 536 |
|
537 |
newStream := self new. |
|
0 | 538 |
newStream openForReading isNil ifTrue:[^nil]. |
539 |
^ newStream |
|
540 |
||
541 |
"SoundStream reading" |
|
593 | 542 |
|
543 |
"Modified: / 12.12.1997 / 16:51:56 / cg" |
|
0 | 544 |
! |
545 |
||
546 |
writing |
|
593 | 547 |
"create and return a new soundStream for writing (i.e. playback)" |
548 |
||
0 | 549 |
|newStream| |
550 |
||
5403 | 551 |
newStream := self new. |
0 | 552 |
newStream openForWriting isNil ifTrue:[^nil]. |
553 |
^ newStream |
|
554 |
||
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
555 |
" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
556 |
SoundStream writing |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
557 |
" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
558 |
|
593 | 559 |
"Created: / 17.11.1995 / 17:25:42 / cg" |
560 |
"Modified: / 12.12.1997 / 16:51:38 / cg" |
|
0 | 561 |
! |
562 |
||
714 | 563 |
writing16Bit |
564 |
"just an example, has never been tried (I also |
|
565 |
have no samples for this ... leave it as an exercise)" |
|
566 |
||
567 |
|newStream| |
|
568 |
||
5403 | 569 |
newStream := self new. |
714 | 570 |
newStream bitsPerSample:16. |
571 |
newStream openForWriting isNil ifTrue:[^nil]. |
|
572 |
^ newStream |
|
573 |
||
574 |
"SoundStream writing16Bit" |
|
575 |
||
576 |
"Modified: / 12.12.1997 / 16:51:49 / cg" |
|
577 |
"Created: / 21.12.1998 / 00:02:17 / cg" |
|
578 |
! |
|
579 |
||
712 | 580 |
writing16BitStereo |
0 | 581 |
"just an example, has never been tried (I also |
592 | 582 |
have no samples for this ... leave it as an exercise)" |
0 | 583 |
|
584 |
|newStream| |
|
585 |
||
4668 | 586 |
OperatingSystem getOSType ~= 'irix' ifTrue:[ |
5405 | 587 |
self error:'unsupported audio mode'. |
0 | 588 |
]. |
5403 | 589 |
newStream := self new. |
0 | 590 |
newStream bitsPerSample:16. |
591 |
newStream numberOfChannels:2. |
|
592 |
newStream openForWriting isNil ifTrue:[^nil]. |
|
593 |
^ newStream |
|
594 |
||
595 |
"SoundStream writing16BitStereo" |
|
593 | 596 |
|
597 |
"Modified: / 12.12.1997 / 16:51:49 / cg" |
|
5400 | 598 |
! |
599 |
||
600 |
writingFloat |
|
601 |
|newStream| |
|
602 |
||
603 |
OperatingSystem getOSType ~= 'osx' ifTrue:[ |
|
5405 | 604 |
self error:'unsupported audio mode'. |
5400 | 605 |
]. |
5403 | 606 |
newStream := self new. |
5400 | 607 |
newStream setAudioFormat:#F32. |
608 |
newStream numberOfChannels:1. |
|
609 |
newStream openForWriting isNil ifTrue:[^nil]. |
|
610 |
^ newStream |
|
611 |
||
612 |
"SoundStream writingFloat" |
|
613 |
! |
|
614 |
||
615 |
writingFloatStereo |
|
616 |
|newStream| |
|
617 |
||
618 |
OperatingSystem getOSType ~= 'osx' ifTrue:[ |
|
5405 | 619 |
self error:'unsupported audio mode'. |
5400 | 620 |
]. |
5403 | 621 |
newStream := self new. |
5400 | 622 |
newStream setAudioFormat:#F32. |
623 |
newStream numberOfChannels:2. |
|
624 |
newStream openForWriting isNil ifTrue:[^nil]. |
|
625 |
^ newStream |
|
626 |
||
627 |
"SoundStream writingFloatStereo" |
|
0 | 628 |
! ! |
629 |
||
596 | 630 |
!SoundStream class methodsFor:'Signal constants'! |
631 |
||
632 |
unsupportedOperationSignal |
|
633 |
^ UnsupportedOperationSignal |
|
634 |
! ! |
|
635 |
||
592 | 636 |
!SoundStream class methodsFor:'conversion helpers'! |
0 | 637 |
|
5401 | 638 |
aLawToLinear16:aLawValue |
639 |
"given an aLaw byte, return the decoded signed 16bit value" |
|
640 |
||
641 |
" |
|
642 |
Linearer Input Output |
|
643 |
s0000000wxyza... n000wxyz |
|
644 |
s0000001wxyza... n001wxyz |
|
645 |
s000001wxyzab... n010wxyz |
|
646 |
s00001wxyzabc... n011wxyz |
|
647 |
s0001wxyzabcd... n100wxyz |
|
648 |
s001wxyzabcde... n101wxyz |
|
649 |
s01wxyzabcdef... n110wxyz |
|
650 |
s1wxyzabcdefg... n111wxyz |
|
651 |
" |
|
652 |
||
653 |
|ix iexp mant| |
|
654 |
||
655 |
ix := aLawValue bitAnd:16r007F. "/ remove sign bit |
|
5405 | 656 |
iexp := ix rightShift: 4. "/ extract exponent |
5401 | 657 |
mant := ix bitAnd:16r000F. "/ now get mantissa |
658 |
(iexp > 0) ifTrue:[ |
|
5405 | 659 |
mant := mant + 16. "/ add leading '1', if exponent > 0 |
5401 | 660 |
]. |
5405 | 661 |
mant := (mant bitShift: 4) + 16r0008. "/ now mantissa left justified and |
662 |
"/ 1/2 quantization step added |
|
663 |
(iexp > 1) ifTrue:[ "/ now left shift according exponent |
|
664 |
mant := mant bitShift: (iexp - 1). |
|
5401 | 665 |
]. |
666 |
"/ from 12 to 16bit |
|
667 |
mant := mant bitShift:4. |
|
5405 | 668 |
aLawValue > 127 ifTrue:[ "/ invert, if negative sample |
669 |
^ mant |
|
5401 | 670 |
] ifFalse:[ |
5405 | 671 |
^ mant negated |
5401 | 672 |
]. |
673 |
||
674 |
" |
|
675 |
self aLawToLinear16:2r00000000 -8 |
|
676 |
self aLawToLinear16:2r00000001 -24 |
|
677 |
self aLawToLinear16:2r00010000 -264 |
|
678 |
self aLawToLinear16:2r00100000 -528 |
|
679 |
self aLawToLinear16:2r01000000 -2112 |
|
680 |
self aLawToLinear16:2r11000000 2112 |
|
681 |
" |
|
682 |
! |
|
683 |
||
596 | 684 |
linear16ToUlaw:a16bitSignedValue |
685 |
"given a 16it signed value, encode into uLaw byte" |
|
686 |
||
687 |
|absVal sign exp mantissa byte| |
|
688 |
||
689 |
%{ |
|
690 |
/* |
|
691 |
* so heavily used when playing sounds ... |
|
692 |
* made it a primitive. |
|
693 |
*/ |
|
694 |
if (__isSmallInteger(a16bitSignedValue)) { |
|
695 |
int __sign = 0; |
|
696 |
int __absVal = __intVal(a16bitSignedValue); |
|
697 |
int __exp, __mantissa, __byte; |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
698 |
static char __uLawExp[] = |
596 | 699 |
{ |
700 |
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, |
|
701 |
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
|
702 |
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
|
703 |
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
|
704 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
|
705 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
|
706 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
|
707 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 |
|
708 |
}; |
|
709 |
||
710 |
if (__absVal < 0) { |
|
711 |
if (__absVal <= -32256) { |
|
712 |
RETURN (__MKSMALLINT(0)); |
|
713 |
} |
|
714 |
__absVal = -__absVal; |
|
715 |
__sign = 0x80; |
|
716 |
} else { |
|
717 |
if (__absVal >= 32256) { |
|
718 |
RETURN (__MKSMALLINT(128)); |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
__exp = __uLawExp[__absVal >> 8]; |
|
723 |
__mantissa = (__absVal >> (__exp+3)) & 0xF; |
|
724 |
__byte = ~(__sign | (__exp<<4) | __mantissa) & 0xFF; |
|
725 |
RETURN (__MKSMALLINT(__byte)); |
|
726 |
} |
|
727 |
%}. |
|
728 |
"/ |
|
729 |
"/ fallback for non-integral argument |
|
730 |
"/ |
|
712 | 731 |
a16bitSignedValue isInteger ifFalse:[ |
732 |
^ self uLawToLinear16:a16bitSignedValue asInteger |
|
596 | 733 |
]. |
712 | 734 |
^ 0 |
596 | 735 |
|
736 |
" |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
737 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:0) |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
738 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:32256) |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
739 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-32256) |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
740 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:32767) |
596 | 741 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-32767) |
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
742 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:100) |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
743 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-100) |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
744 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:104) |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
745 |
SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-104) |
596 | 746 |
" |
747 |
||
748 |
"Modified: / 9.12.1997 / 16:46:24 / cg" |
|
749 |
! |
|
750 |
||
592 | 751 |
uLawToLinear16:uLawValue |
752 |
"given a uLaw byte, return the decoded signed 16bit value. |
|
753 |
Currently unused - but will be" |
|
0 | 754 |
|
755 |
^ #( |
|
45 | 756 |
"0 " -32256 |
757 |
"1 " -31228 |
|
758 |
"2 " -30200 |
|
759 |
"3 " -29172 |
|
760 |
"4 " -28143 |
|
761 |
"5 " -27115 |
|
762 |
"6 " -26087 |
|
763 |
"7 " -25059 |
|
764 |
"8 " -24031 |
|
765 |
"9 " -23002 |
|
766 |
"10 " -21974 |
|
767 |
"11 " -20946 |
|
768 |
"12 " -19918 |
|
769 |
"13 " -18889 |
|
770 |
"14 " -17861 |
|
771 |
"15 " -16833 |
|
772 |
"16 " -16062 |
|
773 |
"17 " -15548 |
|
774 |
"18 " -15033 |
|
775 |
"19 " -14519 |
|
776 |
"20 " -14005 |
|
777 |
"21 " -13491 |
|
778 |
"22 " -12977 |
|
779 |
"23 " -12463 |
|
780 |
"24 " -11949 |
|
781 |
"25 " -11435 |
|
782 |
"26 " -10920 |
|
783 |
"27 " -10406 |
|
784 |
"28 " -9892 |
|
785 |
"29 " -9378 |
|
786 |
"30 " -8864 |
|
787 |
"31 " -8350 |
|
788 |
"32 " -7964 |
|
789 |
"33 " -7707 |
|
790 |
"34 " -7450 |
|
791 |
"35 " -7193 |
|
792 |
"36 " -6936 |
|
793 |
"37 " -6679 |
|
794 |
"38 " -6422 |
|
795 |
"39 " -6165 |
|
796 |
"40 " -5908 |
|
797 |
"41 " -5651 |
|
798 |
"42 " -5394 |
|
799 |
"43 " -5137 |
|
800 |
"44 " -4880 |
|
801 |
"45 " -4623 |
|
802 |
"46 " -4365 |
|
803 |
"47 " -4108 |
|
804 |
"48 " -3916 |
|
805 |
"49 " -3787 |
|
806 |
"50 " -3659 |
|
807 |
"51 " -3530 |
|
808 |
"52 " -3402 |
|
809 |
"53 " -3273 |
|
810 |
"54 " -3144 |
|
811 |
"55 " -3016 |
|
812 |
"56 " -2887 |
|
813 |
"57 " -2759 |
|
814 |
"58 " -2630 |
|
815 |
"59 " -2502 |
|
816 |
"60 " -2373 |
|
817 |
"61 " -2245 |
|
818 |
"62 " -2116 |
|
819 |
"63 " -1988 |
|
820 |
"64 " -1891 |
|
821 |
"65 " -1827 |
|
822 |
"66 " -1763 |
|
823 |
"67 " -1698 |
|
824 |
"68 " -1634 |
|
825 |
"69 " -1570 |
|
826 |
"70 " -1506 |
|
827 |
"71 " -1441 |
|
828 |
"72 " -1377 |
|
829 |
"73 " -1313 |
|
830 |
"74 " -1249 |
|
831 |
"75 " -1184 |
|
832 |
"76 " -1120 |
|
833 |
"77 " -1056 |
|
834 |
"78 " -992 |
|
835 |
"79 " -927 |
|
836 |
"80 " -879 |
|
837 |
"81 " -847 |
|
838 |
"82 " -815 |
|
839 |
"83 " -783 |
|
840 |
"84 " -751 |
|
841 |
"85 " -718 |
|
842 |
"86 " -686 |
|
843 |
"87 " -654 |
|
844 |
"88 " -622 |
|
845 |
"89 " -590 |
|
846 |
"90 " -558 |
|
847 |
"91 " -526 |
|
848 |
"92 " -494 |
|
849 |
"93 " -461 |
|
850 |
"94 " -429 |
|
851 |
"95 " -397 |
|
852 |
"96 " -373 |
|
853 |
"97 " -357 |
|
854 |
"98 " -341 |
|
855 |
"99 " -325 |
|
856 |
"100" -309 |
|
857 |
"101" -293 |
|
858 |
"102" -277 |
|
859 |
"103" -261 |
|
860 |
"104" -245 |
|
861 |
"105" -228 |
|
862 |
"106" -212 |
|
863 |
"107" -196 |
|
864 |
"108" -180 |
|
865 |
"109" -164 |
|
866 |
"110" -148 |
|
867 |
"111" -132 |
|
868 |
"112" -120 |
|
869 |
"113" -112 |
|
870 |
"114" -104 |
|
871 |
"115" -96 |
|
872 |
"116" -88 |
|
873 |
"117" -80 |
|
874 |
"118" -72 |
|
875 |
"119" -64 |
|
876 |
"120" -56 |
|
877 |
"121" -48 |
|
878 |
"122" -40 |
|
879 |
"123" -32 |
|
880 |
"124" -24 |
|
881 |
"125" -16 |
|
882 |
"126" -8 |
|
883 |
"127" 0 |
|
884 |
"128" 32256 |
|
885 |
"129" 31228 |
|
886 |
"130" 30200 |
|
887 |
"131" 29172 |
|
888 |
"132" 28143 |
|
889 |
"133" 27115 |
|
890 |
"134" 26087 |
|
891 |
"135" 25059 |
|
892 |
"136" 24031 |
|
893 |
"137" 23002 |
|
894 |
"138" 21974 |
|
895 |
"139" 20946 |
|
896 |
"140" 19918 |
|
897 |
"141" 18889 |
|
898 |
"142" 17861 |
|
899 |
"143" 16833 |
|
900 |
"144" 16062 |
|
901 |
"145" 15548 |
|
902 |
"146" 15033 |
|
903 |
"147" 14519 |
|
904 |
"148" 14005 |
|
905 |
"149" 13491 |
|
906 |
"150" 12977 |
|
907 |
"151" 12463 |
|
908 |
"152" 11949 |
|
909 |
"153" 11435 |
|
910 |
"154" 10920 |
|
911 |
"155" 10406 |
|
912 |
"156" 9892 |
|
913 |
"157" 9378 |
|
914 |
"158" 8864 |
|
915 |
"159" 8350 |
|
916 |
"160" 7964 |
|
917 |
"161" 7707 |
|
918 |
"162" 7450 |
|
919 |
"163" 7193 |
|
920 |
"164" 6936 |
|
921 |
"165" 6679 |
|
922 |
"166" 6422 |
|
923 |
"167" 6165 |
|
924 |
"168" 5908 |
|
925 |
"169" 5651 |
|
926 |
"170" 5394 |
|
927 |
"171" 5137 |
|
928 |
"172" 4880 |
|
929 |
"173" 4623 |
|
930 |
"174" 4365 |
|
931 |
"175" 4108 |
|
932 |
"176" 3916 |
|
933 |
"177" 3787 |
|
934 |
"178" 3659 |
|
935 |
"179" 3530 |
|
936 |
"180" 3402 |
|
937 |
"181" 3273 |
|
938 |
"182" 3144 |
|
939 |
"183" 3016 |
|
940 |
"184" 2887 |
|
941 |
"185" 2759 |
|
942 |
"186" 2630 |
|
943 |
"187" 2502 |
|
944 |
"188" 2373 |
|
945 |
"189" 2245 |
|
946 |
"190" 2116 |
|
947 |
"191" 1988 |
|
948 |
"192" 1891 |
|
949 |
"193" 1827 |
|
950 |
"194" 1763 |
|
951 |
"195" 1698 |
|
952 |
"196" 1634 |
|
953 |
"197" 1570 |
|
954 |
"198" 1506 |
|
955 |
"199" 1441 |
|
956 |
"200" 1377 |
|
957 |
"201" 1313 |
|
958 |
"202" 1249 |
|
959 |
"203" 1184 |
|
960 |
"204" 1120 |
|
961 |
"205" 1056 |
|
962 |
"206" 992 |
|
963 |
"207" 927 |
|
964 |
"208" 879 |
|
965 |
"209" 847 |
|
966 |
"210" 815 |
|
967 |
"211" 783 |
|
968 |
"212" 751 |
|
969 |
"213" 718 |
|
970 |
"214" 686 |
|
971 |
"215" 654 |
|
972 |
"216" 622 |
|
973 |
"217" 590 |
|
974 |
"218" 558 |
|
975 |
"219" 526 |
|
976 |
"220" 494 |
|
977 |
"221" 461 |
|
978 |
"222" 429 |
|
979 |
"223" 397 |
|
980 |
"224" 373 |
|
981 |
"225" 357 |
|
982 |
"226" 341 |
|
983 |
"227" 325 |
|
984 |
"228" 309 |
|
985 |
"229" 293 |
|
986 |
"230" 277 |
|
987 |
"231" 261 |
|
988 |
"232" 245 |
|
989 |
"233" 228 |
|
990 |
"234" 212 |
|
991 |
"235" 196 |
|
992 |
"236" 180 |
|
993 |
"237" 164 |
|
994 |
"238" 148 |
|
995 |
"239" 132 |
|
996 |
"240" 120 |
|
997 |
"241" 112 |
|
998 |
"242" 104 |
|
999 |
"243" 96 |
|
1000 |
"244" 88 |
|
1001 |
"245" 80 |
|
1002 |
"246" 72 |
|
1003 |
"247" 64 |
|
1004 |
"248" 56 |
|
1005 |
"249" 48 |
|
1006 |
"250" 40 |
|
1007 |
"251" 32 |
|
1008 |
"252" 24 |
|
1009 |
"253" 16 |
|
1010 |
"254" 8 |
|
1011 |
"255" 0 |
|
0 | 1012 |
) at:(uLawValue + 1) |
592 | 1013 |
|
1014 |
"Modified: / 9.12.1997 / 16:34:17 / cg" |
|
0 | 1015 |
! ! |
1016 |
||
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1017 |
!SoundStream class methodsFor:'default values'! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1018 |
|
596 | 1019 |
defaultAudioFormat |
2822 | 1020 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
3000 | 1021 |
^ #S16 |
712 | 1022 |
]. |
5400 | 1023 |
OperatingSystem isOSXlike ifTrue:[ |
1024 |
^ #S16 |
|
1025 |
]. |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1026 |
^ #U8 |
596 | 1027 |
! |
1028 |
||
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1029 |
defaultBitsPerSample |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1030 |
"minimum, supported by all audio systems" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1031 |
|
2822 | 1032 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
3000 | 1033 |
^ 16 |
712 | 1034 |
]. |
5400 | 1035 |
OperatingSystem isOSXlike ifTrue:[ |
1036 |
^ 16 |
|
1037 |
]. |
|
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1038 |
^ 8 |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1039 |
! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1040 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1041 |
defaultNumberOfChannels |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1042 |
"minimum, supported by all audio systems" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1043 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1044 |
^ 1 |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1045 |
! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1046 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1047 |
defaultSampleRate |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1048 |
"minimum, supported by all audio systems" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1049 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1050 |
^ 8000 |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1051 |
! ! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1052 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1053 |
!SoundStream class methodsFor:'playing'! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1054 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1055 |
playSoundFile:aFilename |
593 | 1056 |
"play a soundFile" |
1057 |
||
5402 | 1058 |
|mime inStream soundStream count totalCount buffer startTime playTime delayedTime waitTime| |
1059 |
||
1060 |
mime := aFilename asFilename mimeTypeFromName. |
|
1061 |
mime isAudioType ifFalse:[ |
|
5405 | 1062 |
self error:'not an audio file: ',aFilename asFilename baseName |
5402 | 1063 |
]. |
1064 |
self halt. |
|
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1065 |
inStream := aFilename asFilename readStream. |
1154
6158d4545b71
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1151
diff
changeset
|
1066 |
inStream isNil ifTrue:[self error:'cannot open']. |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1067 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1068 |
soundStream := self writing. |
1154
6158d4545b71
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1151
diff
changeset
|
1069 |
soundStream isNil ifTrue:[self error:'cannot open sound device']. |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1070 |
|
1432
077bf1bfc8c4
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1071 |
startTime := Timestamp now. |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1072 |
totalCount := 0. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1073 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1074 |
buffer := ByteArray new:4096. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1075 |
[(count := inStream nextBytesInto:buffer) > 0] whileTrue:[ |
5405 | 1076 |
totalCount := totalCount + count. |
1077 |
soundStream nextPutBytes:count from:buffer. |
|
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1078 |
]. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1079 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1080 |
inStream close. |
4867 | 1081 |
soundStream flush. |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1082 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1083 |
"/ |
5400 | 1084 |
"/ at least the linux audio driver behaves funny, if we close too early... |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1085 |
"/ |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1086 |
playTime := totalCount / soundStream sampleRate. |
3226 | 1087 |
delayedTime := Timestamp now secondDeltaFrom: startTime. |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1088 |
waitTime := playTime - delayedTime + 0.1. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1089 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1090 |
(Delay forSeconds:waitTime) wait. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1091 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1092 |
soundStream close. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1093 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1094 |
" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1095 |
SoundStream playSoundFile:'/usr/local/lib/sounds/laugh.snd' |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1096 |
SoundStream playSoundFile:'/usr/local/lib/sounds/spacemusic.snd' |
5402 | 1097 |
SoundStream playSoundFile:'../../goodies/not_ported/sound/sampleFiles/pluck-pcm16.aiff' |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1098 |
" |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1099 |
|
4867 | 1100 |
"Created: / 17-11-1995 / 17:25:30 / cg" |
1101 |
"Modified: / 17-03-2019 / 15:02:57 / Claus Gittinger" |
|
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1102 |
! ! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1103 |
|
5400 | 1104 |
!SoundStream class methodsFor:'queries'! |
1105 |
||
5402 | 1106 |
isSupported |
1107 |
^ false. |
|
1108 |
! |
|
1109 |
||
5400 | 1110 |
usedAudio |
1111 |
"returns a symbol describing which audio system is used; |
|
1112 |
one of PORTAUDIO, DEV_AUDIO, WIN32_WAVESOUND, IRIS_AUDIO" |
|
1113 |
||
5406 | 1114 |
ConcreteClass isNil ifTrue:[^ nil]. |
1115 |
^ ConcreteClass nameSymbol |
|
5400 | 1116 |
! ! |
1117 |
||
596 | 1118 |
!SoundStream methodsFor:'catching invalid methods'! |
1119 |
||
1120 |
pathName:filename |
|
1121 |
"catch pathname access - its fixed here" |
|
1122 |
||
1123 |
self shouldNotImplement |
|
1124 |
! |
|
1125 |
||
1126 |
pathName:filename in:aDirectory |
|
1127 |
"catch pathname access - its fixed here" |
|
1128 |
||
1129 |
self shouldNotImplement |
|
1130 |
! ! |
|
1131 |
||
5400 | 1132 |
!SoundStream methodsFor:'error handling'! |
1133 |
||
1134 |
lastErrorString |
|
1135 |
"return a message string describing the last error" |
|
1136 |
||
1137 |
lastErrorString notNil ifTrue:[ |
|
1138 |
^ lastErrorString |
|
1139 |
]. |
|
1140 |
^ super lastErrorString |
|
1141 |
! ! |
|
1142 |
||
596 | 1143 |
!SoundStream methodsFor:'mode setting'! |
1144 |
||
1145 |
bitsPerSample |
|
1146 |
"return the number of bits per sample - usually 8" |
|
1147 |
||
1148 |
^ bitsPerSample |
|
1149 |
! |
|
1150 |
||
1151 |
bitsPerSample:aNumber |
|
1152 |
"set the number of bits per sample" |
|
1153 |
||
5400 | 1154 |
|supported ok| |
1155 |
||
1156 |
ok := false. |
|
1157 |
supported := self supportedAudioFormats. |
|
1158 |
aNumber == 16 ifTrue:[ |
|
1159 |
#(S16 S16_LE S16_BE U16 U16_LE U16_BE) doWithExit:[:fmt :exit | |
|
1160 |
(supported includes:fmt) ifTrue:[ |
|
1161 |
self setAudioFormat:fmt. |
|
1162 |
ok := true. |
|
1163 |
exit value:nil. |
|
1164 |
] |
|
1165 |
] |
|
1166 |
]. |
|
1167 |
aNumber == 8 ifTrue:[ |
|
1168 |
#(S8 U8) doWithExit:[:fmt :exit | |
|
1169 |
(supported includes:fmt) ifTrue:[ |
|
1170 |
self setAudioFormat:fmt. |
|
1171 |
ok := true. |
|
1172 |
exit value:nil. |
|
1173 |
] |
|
1174 |
] |
|
1175 |
]. |
|
1176 |
aNumber == 24 ifTrue:[ |
|
1177 |
#(S24 S24_LE S24_BE U24 U24_LE U24_BE) doWithExit:[:fmt :exit | |
|
1178 |
(supported includes:fmt) ifTrue:[ |
|
1179 |
self setAudioFormat:fmt. |
|
1180 |
ok := true. |
|
1181 |
exit value:nil. |
|
1182 |
] |
|
1183 |
] |
|
1184 |
]. |
|
1185 |
aNumber == 32 ifTrue:[ |
|
1186 |
#(S32 S32_LE S32_BE U32 U32_LE U32_BE) doWithExit:[:fmt :exit | |
|
1187 |
(supported includes:fmt) ifTrue:[ |
|
1188 |
self setAudioFormat:fmt. |
|
1189 |
ok := true. |
|
1190 |
exit value:nil. |
|
1191 |
] |
|
1192 |
] |
|
1193 |
]. |
|
1194 |
ok ifFalse:[ |
|
1195 |
UnsupportedOperationSignal raiseRequestErrorString:'unsupported format'. |
|
1196 |
]. |
|
596 | 1197 |
bitsPerSample := aNumber |
1198 |
! |
|
1199 |
||
1200 |
numberOfChannels |
|
1201 |
"return the number of channels (1 or 2; usually 1)" |
|
1202 |
||
1203 |
^ numberOfChannels |
|
1204 |
! |
|
1205 |
||
1206 |
numberOfChannels:aNumber |
|
5400 | 1207 |
"set the number of channels |
1208 |
- on some devices, this is a nop" |
|
596 | 1209 |
|
1210 |
numberOfChannels := aNumber |
|
1211 |
! |
|
1212 |
||
1213 |
sampleRate |
|
1214 |
"return the sample rate" |
|
1215 |
||
1216 |
^ sampleRate |
|
1217 |
! |
|
1218 |
||
1219 |
sampleRate:aNumber |
|
5400 | 1220 |
"set the sample rate in hertz |
1221 |
- on some devices, this is a nop" |
|
596 | 1222 |
|
1223 |
self setSampleRate:aNumber. |
|
1224 |
! ! |
|
1225 |
||
592 | 1226 |
!SoundStream methodsFor:'private'! |
1227 |
||
593 | 1228 |
dumpSettings |
1229 |
"debugging interface - dump the current settings" |
|
1230 |
||
921 | 1231 |
audioFormat == #S16 ifTrue:[ |
5405 | 1232 |
UninterpretedBytes isBigEndian ifTrue:[ |
1233 |
audioFormat := #S16_BE |
|
1234 |
] ifFalse:[ |
|
1235 |
audioFormat := #S16_LE |
|
1236 |
] |
|
921 | 1237 |
]. |
1238 |
audioFormat == #U16 ifTrue:[ |
|
5405 | 1239 |
UninterpretedBytes isBigEndian ifTrue:[ |
1240 |
audioFormat := #U16_BE |
|
1241 |
] ifFalse:[ |
|
1242 |
audioFormat := #U16_LE |
|
1243 |
] |
|
921 | 1244 |
]. |
5402 | 1245 |
Transcript show:'sampleRate: '; showCR:sampleRate. |
1246 |
Transcript show:'format: '; showCR:audioFormat. |
|
1247 |
Transcript show:'fragmentSize: '; showCR:fragmentSize. |
|
1248 |
Transcript show:'channels: '; showCR:numberOfChannels. |
|
920 | 1249 |
Transcript show:'supported audioFormats: '; showCR:(self supportedAudioFormats). |
592 | 1250 |
|
1251 |
" |
|
593 | 1252 |
self writing dumpSettings; close |
1253 |
" |
|
1254 |
! |
|
1255 |
||
5400 | 1256 |
fileDescriptorOrNil |
1257 |
handle notNil ifTrue:[ |
|
1258 |
^ self fileDescriptor. |
|
1259 |
]. |
|
1260 |
^ nil |
|
1261 |
! |
|
1262 |
||
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1263 |
initialize |
596 | 1264 |
"initialize for least common mode" |
593 | 1265 |
|
3156 | 1266 |
super initialize. |
3482 | 1267 |
"/ transparent |
1268 |
eolMode := nil. |
|
596 | 1269 |
buffered := false. |
1269
17546758fc7a
buffered:falsem to NonPositionableExternalStreams is already set.
Stefan Vogel <sv@exept.de>
parents:
1154
diff
changeset
|
1270 |
binary := true. |
712 | 1271 |
bitsPerSample := self class defaultBitsPerSample. |
1272 |
audioFormat := self class defaultAudioFormat. |
|
1273 |
numberOfChannels := self class defaultNumberOfChannels. |
|
1274 |
sampleRate := self class defaultSampleRate. |
|
1275 |
pathName := nil. |
|
592 | 1276 |
! |
1277 |
||
1278 |
resetSoundCard |
|
593 | 1279 |
"debugging interface - reset the soundCard" |
1280 |
||
595 | 1281 |
^ UnsupportedOperationSignal raise |
592 | 1282 |
|
1283 |
" |
|
593 | 1284 |
self writing resetSoundCard; dumpSettings; close |
592 | 1285 |
" |
1286 |
! |
|
1287 |
||
1288 |
setAudioFormat:aSymbol |
|
593 | 1289 |
"set the format of the audio data as specified by aSymbol. |
4617 | 1290 |
Returns true if sucessful - may fail with some formats on many sound devices." |
593 | 1291 |
|
5402 | 1292 |
audioFormat := aSymbol. |
5400 | 1293 |
((audioFormat == #U8) or:[audioFormat == #S8]) ifTrue:[ |
5405 | 1294 |
bitsPerSample := 8. |
1295 |
^ self |
|
5400 | 1296 |
]. |
1297 |
(#(U16 U16_LE U16_BE S16 S16_LE S16_BE) includes:audioFormat) ifTrue:[ |
|
5405 | 1298 |
bitsPerSample := 16. |
1299 |
^ self |
|
5400 | 1300 |
]. |
1301 |
(#(U24 U24_LE U24_BE S24 S24_LE S24_BE) includes:audioFormat) ifTrue:[ |
|
5405 | 1302 |
bitsPerSample := 24. |
1303 |
^ self |
|
5400 | 1304 |
]. |
1305 |
(#(U32 U32_LE U32_BE S32 S32_LE S32_BE) includes:audioFormat) ifTrue:[ |
|
5405 | 1306 |
bitsPerSample := 32. |
1307 |
^ self |
|
5400 | 1308 |
]. |
1309 |
(audioFormat == #F32) ifTrue:[ |
|
5405 | 1310 |
bitsPerSample := 32. |
1311 |
^ self |
|
5400 | 1312 |
]. |
1313 |
bitsPerSample := 8. |
|
592 | 1314 |
|
1315 |
" |
|
919 | 1316 |
self writing dumpSettings; close |
592 | 1317 |
self writing setAudioFormat:#'MU_LAW'; close |
597 | 1318 |
self writing setAudioFormat:#'U8'; dumpSettings; close |
1319 |
self writing setAudioFormat:#'MPEG'; dumpSettings; close |
|
592 | 1320 |
" |
593 | 1321 |
! |
1322 |
||
1323 |
setChannels:nChannels |
|
1324 |
"set the number of channels (1 -> mono; 2 -> stereo). |
|
4617 | 1325 |
Returns true if sucessful - may fail with many sound devices." |
593 | 1326 |
|
5405 | 1327 |
numberOfChannels := nChannels. |
593 | 1328 |
|
1329 |
" |
|
1330 |
self writing setChannels:2; dumpSettings; close |
|
1331 |
self writing setChannels:2; setSampleRate:10000; dumpSettings; close |
|
1332 |
self writing setChannels:2; setSampleRate:40000; dumpSettings; close |
|
1333 |
" |
|
1334 |
! |
|
1335 |
||
1336 |
setFragmentSize:blockSize |
|
5402 | 1337 |
"set the soundDriver's fragmentSize" |
1338 |
||
1339 |
fragmentSize := blockSize. |
|
593 | 1340 |
! |
1341 |
||
1342 |
setSampleRate:hz |
|
5402 | 1343 |
"set the sample rate" |
1344 |
||
1345 |
sampleRate := hz. |
|
593 | 1346 |
|
1347 |
" |
|
1348 |
self writing setSampleRate:10000; dumpSettings; close |
|
1349 |
self writing setSampleRate:1000; dumpSettings; close |
|
919 | 1350 |
self writing setSampleRate:8000; dumpSettings; close |
593 | 1351 |
" |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1352 |
! |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1353 |
|
596 | 1354 |
supportedAudioFormats |
1355 |
"return a collection of supported audio formats. |
|
5400 | 1356 |
possibly returned symbols are: |
5405 | 1357 |
U8 unsigned 8bit samples |
1358 |
S8 signed 8bit samples |
|
1359 |
U16 unsigned 16bit samples in native format |
|
1360 |
U16_LE unsigned 16bit big endian samples |
|
1361 |
U16_BE unsigned 16bit big endian samples |
|
1362 |
S16 signed 16bit little endian samples in native format |
|
1363 |
S16_LE signed 16bit little endian samples |
|
1364 |
S16_BE signed 16bit big endian samples |
|
1365 |
S24 signed 24bit little endian samples in native format |
|
1366 |
S24_LE signed 24bit little endian samples |
|
1367 |
S24_BE signed 24bit big endian samples |
|
1368 |
S32 signed 32bit little endian samples in native format |
|
1369 |
S32_LE signed 32bit little endian samples |
|
1370 |
S32_BE signed 32bit big endian samples |
|
1371 |
F32 float samples |
|
1372 |
MPEG audio mpeg encoded |
|
1373 |
MU_LAW u-law encoded 8bit samples |
|
1374 |
A_LAW a-law encoded 8bit samples |
|
1375 |
IMA_ADPCM adpcm encoded |
|
5400 | 1376 |
the set of returned symbols depends on the underlying sound hardware. |
596 | 1377 |
" |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1378 |
|
5402 | 1379 |
^ #( U8) |
0 | 1380 |
|
596 | 1381 |
" |
1382 |
|s formats| |
|
0 | 1383 |
|
596 | 1384 |
s := self writing. |
1385 |
formats := s supportedAudioFormats. |
|
1386 |
s close. |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1387 |
formats |
596 | 1388 |
" |
0 | 1389 |
! ! |
1390 |
||
5400 | 1391 |
!SoundStream methodsFor:'redefined'! |
1392 |
||
1393 |
close |
|
1394 |
self flush. |
|
1395 |
super close. |
|
1396 |
! ! |
|
1397 |
||
1474 | 1398 |
!SoundStream protectedMethodsFor:'redefined'! |
0 | 1399 |
|
593 | 1400 |
closeFile |
1401 |
"a stream has been collected - close the file" |
|
1402 |
||
5402 | 1403 |
self subclassResponsibility |
1474 | 1404 |
! ! |
1405 |
||
1406 |
!SoundStream methodsFor:'redefined'! |
|
593 | 1407 |
|
274
f45bd0d6ea31
Rename synchronizeOutput --> flush.
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
1408 |
flush |
f45bd0d6ea31
Rename synchronizeOutput --> flush.
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
1409 |
"wait until all sound has been played" |
f45bd0d6ea31
Rename synchronizeOutput --> flush.
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
1410 |
|
5402 | 1411 |
self subclassResponsibility |
1412 |
||
277
ab17540f022a
oops - your last checkin was garbage
Claus Gittinger <cg@exept.de>
parents:
274
diff
changeset
|
1413 |
! |
274
f45bd0d6ea31
Rename synchronizeOutput --> flush.
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
1414 |
|
712 | 1415 |
isOpen |
4635 | 1416 |
self isExecutor ifTrue:[ |
5400 | 1417 |
^ false. |
4635 | 1418 |
]. |
721 | 1419 |
handle1 notNil ifTrue:[^ true]. |
1583 | 1420 |
handle2 notNil ifTrue:[^ true]. |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
1853
diff
changeset
|
1421 |
^ handle notNil |
4635 | 1422 |
|
1423 |
"Modified: / 23-04-2018 / 18:29:42 / stefan" |
|
712 | 1424 |
! |
1425 |
||
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1426 |
nextBytes:count into:anObject startingAt:start |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1427 |
"read the next count bytes into an object and return the number of |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1428 |
bytes read or nil on error. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1429 |
Use with ByteArrays only." |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1430 |
|
5402 | 1431 |
self subclassResponsibility |
277
ab17540f022a
oops - your last checkin was garbage
Claus Gittinger <cg@exept.de>
parents:
274
diff
changeset
|
1432 |
! |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1433 |
|
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1434 |
nextPutBytes:count from:anObject startingAt:start |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1435 |
"write count bytes from an object starting at index start. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1436 |
return the number of bytes written or nil on error. |
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1437 |
Redefined, since IRIS audio library cannot be used with stdio. |
4193 | 1438 |
(at least I don't know). Use with ByteArrays only." |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1439 |
|
5402 | 1440 |
self subclassResponsibility |
277
ab17540f022a
oops - your last checkin was garbage
Claus Gittinger <cg@exept.de>
parents:
274
diff
changeset
|
1441 |
! |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1442 |
|
1480 | 1443 |
openWithMode:aMode attributes:attributeSpec |
5402 | 1444 |
self subclassResponsibility |
277
ab17540f022a
oops - your last checkin was garbage
Claus Gittinger <cg@exept.de>
parents:
274
diff
changeset
|
1445 |
! ! |
0 | 1446 |
|
1447 |
!SoundStream methodsFor:'sine wave generation'! |
|
1448 |
||
5400 | 1449 |
playSine16:freq forSeconds:nSeconds |
722 | 1450 |
"output some tone for some time |
1451 |
in S16 audioFormat - a test method" |
|
1452 |
||
5400 | 1453 |
|buffer numSamples val scale isUnsigned restSamples| |
923 | 1454 |
|
1455 |
(audioFormat startsWith:#U16) ifFalse:[ |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1456 |
(audioFormat startsWith:#S16) ifFalse:[ |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1457 |
self error:'must be in 16bit mode' mayProceed:true. |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1458 |
^ self |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1459 |
] |
923 | 1460 |
]. |
722 | 1461 |
|
5400 | 1462 |
numSamples := self sampleRate. |
722 | 1463 |
buffer := WordArray new:numSamples. |
1464 |
||
1465 |
"fill it with a sine wave" |
|
1466 |
||
923 | 1467 |
isUnsigned := audioFormat startsWith:#U16. |
1468 |
||
722 | 1469 |
scale := freq * 2 * (Float pi). |
1470 |
1 to:numSamples do:[:i | |
|
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1471 |
val := (scale * i / self sampleRate) sin. |
5400 | 1472 |
val := (val * 16r7FFF) rounded. |
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1473 |
isUnsigned ifTrue:[ |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1474 |
val := val + 32768 |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1475 |
]. |
5400 | 1476 |
buffer at:i put:(val bitAnd:16rFFFF) |
714 | 1477 |
]. |
722 | 1478 |
|
5400 | 1479 |
1 to:nSeconds truncated do:[:s | |
1480 |
self nextPutBytes:(numSamples*2) from:buffer startingAt:1 |
|
1481 |
]. |
|
1482 |
restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated. |
|
1483 |
restSamples > 0 ifTrue:[ |
|
1484 |
self nextPutBytes:(restSamples*2) from:buffer startingAt:1 |
|
1485 |
]. |
|
722 | 1486 |
|
1487 |
" |
|
923 | 1488 |
SoundStream writing setAudioFormat:#S16; playSine16:440 forSeconds:2; close |
1489 |
SoundStream writing setAudioFormat:#S16; playSine16:880 forSeconds:2; close |
|
1490 |
SoundStream writing setAudioFormat:#S16; playSine16:1760 forSeconds:2; close |
|
1491 |
SoundStream writing setAudioFormat:#S16; playSine16:3520 forSeconds:2; close |
|
1492 |
SoundStream writing tuneTone:440; close |
|
1493 |
||
1494 |
SoundStream writing setAudioFormat:#U16; playSine16:880 forSeconds:2; close |
|
1495 |
SoundStream writing setAudioFormat:#U16_LE; playSine16:880 forSeconds:2; close |
|
722 | 1496 |
" |
1497 |
||
1498 |
"Modified: / 31.1.1999 / 12:12:41 / cg" |
|
1499 |
! |
|
1500 |
||
5400 | 1501 |
playSine:freq forSeconds:nSeconds |
1502 |
"output some tone for some time - a test method" |
|
1503 |
||
1504 |
audioFormat == #F32 ifTrue:[ |
|
1505 |
self playSineF32:freq forSeconds:nSeconds. |
|
1506 |
^ self. |
|
1507 |
]. |
|
1508 |
self playSine16:freq forSeconds:nSeconds. |
|
1509 |
||
1510 |
" |
|
1511 |
SoundStream writing setAudioFormat:#F32; playSine:440 forSeconds:2; close |
|
1512 |
SoundStream writing setAudioFormat:#F32; playSine:880 forSeconds:2; close |
|
1513 |
SoundStream writing setAudioFormat:#F32; playSine:1760 forSeconds:2; close |
|
1514 |
SoundStream writing setAudioFormat:#F32; playSine:3520 forSeconds:2; close |
|
1515 |
" |
|
1516 |
! |
|
1517 |
||
1518 |
playSineF32:freq forSeconds:nSeconds |
|
1519 |
"output some tone for some time |
|
1520 |
in F32 audioFormat - a test method" |
|
1521 |
||
5407 | 1522 |
|buffer numSamples val scale restSamples numChannels| |
5400 | 1523 |
|
1524 |
(audioFormat == #F32) ifFalse:[ |
|
5409 | 1525 |
self error:'must be in float mode' mayProceed:true. |
5400 | 1526 |
]. |
1527 |
||
1528 |
numSamples := self sampleRate. |
|
5407 | 1529 |
numChannels := self numberOfChannels. |
1530 |
buffer := FloatArray new:(numSamples * numChannels). |
|
5400 | 1531 |
|
1532 |
"fill it with a sine wave" |
|
1533 |
||
1534 |
scale := freq * 2 * (Float pi). |
|
5407 | 1535 |
numChannels == 2 ifTrue:[ |
5409 | 1536 |
1 to:numSamples do:[:i | |
1537 |
val := (scale * i / self sampleRate) sin. |
|
1538 |
buffer at:(i-1)*2 put:val. |
|
1539 |
buffer at:(i-1)*2+1 put:0. |
|
1540 |
]. |
|
5407 | 1541 |
] ifFalse:[ |
5409 | 1542 |
1 to:numSamples do:[:i | |
1543 |
val := (scale * i / self sampleRate) sin. |
|
1544 |
buffer at:i put:val |
|
1545 |
]. |
|
5400 | 1546 |
]. |
1547 |
||
1548 |
1 to:nSeconds truncated do:[:s | |
|
5409 | 1549 |
self nextPutBytes:(numSamples*4) from:buffer startingAt:1 |
5400 | 1550 |
]. |
1551 |
restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated. |
|
1552 |
restSamples > 0 ifTrue:[ |
|
5409 | 1553 |
self nextPutBytes:(restSamples*4) from:buffer startingAt:1 |
5400 | 1554 |
]. |
1555 |
||
1556 |
" |
|
1557 |
SoundStream writing setAudioFormat:#F32; playSineF32:440 forSeconds:2; close |
|
1558 |
SoundStream writing setAudioFormat:#F32; playSineF32:880 forSeconds:2; close |
|
1559 |
SoundStream writing setAudioFormat:#F32; playSineF32:1760 forSeconds:2; close |
|
1560 |
SoundStream writing setAudioFormat:#F32; playSineF32:3520 forSeconds:2; close |
|
1561 |
" |
|
1562 |
||
1563 |
"Modified: / 31.1.1999 / 12:12:41 / cg" |
|
1564 |
! |
|
1565 |
||
788 | 1566 |
testMelody |
1567 |
3 timesRepeat:[ |
|
5400 | 1568 |
self playSine:220 forSeconds:0.5. |
1569 |
self playSine:440 forSeconds:0.5. |
|
1570 |
self playSine:880 forSeconds:0.5. |
|
1571 |
self playSine:1760 forSeconds:0.5. |
|
1572 |
self playSine:3520 forSeconds:0.5. |
|
788 | 1573 |
] |
1574 |
||
1575 |
" |
|
1576 |
self writing testMelody; close |
|
1577 |
" |
|
1578 |
||
1579 |
"Created: / 31.1.1999 / 12:07:45 / cg" |
|
1580 |
"Modified: / 31.1.1999 / 12:16:09 / cg" |
|
1581 |
! |
|
1582 |
||
722 | 1583 |
tuneTone |
1584 |
^ self tuneTone:440 |
|
596 | 1585 |
|
1586 |
" |
|
1587 |
SoundStream writing tuneTone; close |
|
1588 |
SoundStream writing setSampleRate:4000; tuneTone; close |
|
1589 |
SoundStream writing setSampleRate:8000; tuneTone; close |
|
1590 |
SoundStream writing setSampleRate:10000; tuneTone; close |
|
1591 |
SoundStream writing setSampleRate:20000; tuneTone; close |
|
1592 |
SoundStream writing setSampleRate:40000; tuneTone; close |
|
923 | 1593 |
|
1594 |
SoundStream writing setSampleRate:40000; dumpSettings; close |
|
596 | 1595 |
SoundStream writing setSampleRate:20000; dumpSettings; close |
1596 |
" |
|
1597 |
||
722 | 1598 |
"Modified: / 31.1.1999 / 12:06:27 / cg" |
1599 |
! |
|
1600 |
||
5400 | 1601 |
tuneTone16:freq seconds:nSeconds |
1602 |
"output some tone for nSeconds in S16 audioFormat - a test method" |
|
1603 |
||
1604 |
|buffer numSamples val scale restSamples| |
|
597 | 1605 |
|
923 | 1606 |
(audioFormat startsWith:#U16) ifFalse:[ |
5409 | 1607 |
(audioFormat startsWith:#S16) ifFalse:[ |
1608 |
self error:'must be in 16bit mode' mayProceed:true. |
|
1609 |
^ self |
|
1610 |
] |
|
923 | 1611 |
]. |
1612 |
||
5400 | 1613 |
"allocate memory for 1 sec playing time" |
597 | 1614 |
numSamples := self sampleRate. |
1615 |
buffer := WordArray new:numSamples. |
|
1616 |
||
1617 |
"fill it with a sine wave" |
|
1618 |
||
1619 |
scale := freq * 2 * (Float pi). |
|
1620 |
1 to:numSamples do:[:i | |
|
5409 | 1621 |
val := (scale * i / numSamples) sin. |
1622 |
val := (val * 16r7FFF) rounded. |
|
1623 |
audioFormat == #U16 ifTrue:[ |
|
1624 |
val := val + 16r8000 |
|
1625 |
]. |
|
1626 |
buffer at:i put:(val bitAnd:16rFFFF) |
|
597 | 1627 |
]. |
1628 |
||
5400 | 1629 |
1 to:nSeconds truncated do:[:s | |
5409 | 1630 |
self nextPutBytes:(numSamples*2) from:buffer startingAt:1 |
597 | 1631 |
]. |
5400 | 1632 |
restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated. |
1633 |
restSamples > 0 ifTrue:[ |
|
5409 | 1634 |
self nextPutBytes:(restSamples*2) from:buffer startingAt:1 |
5400 | 1635 |
]. |
597 | 1636 |
|
1637 |
"of course, the frequency should be below half the |
|
1638 |
sampleRate - hear below ... |
|
1639 |
||
5407 | 1640 |
SoundStream writing setSampleRate:4000; tuneTone16:440 seconds:1; close |
1641 |
SoundStream writing setSampleRate:8000; tuneTone16:440 seconds:1; close |
|
1642 |
SoundStream writing setSampleRate:10000; tuneTone16:440 seconds:1; close |
|
1643 |
SoundStream writing setSampleRate:20000; tuneTone16:440 seconds:1; close |
|
1644 |
SoundStream writing setSampleRate:40000; tuneTone16:440 seconds:1; close |
|
1645 |
SoundStream writing tuneTone16:440 seconds:1; close |
|
597 | 1646 |
" |
1647 |
||
714 | 1648 |
"Modified: / 21.12.1998 / 09:11:30 / cg" |
597 | 1649 |
! |
1650 |
||
5400 | 1651 |
tuneTone8:freq seconds:nSeconds |
1652 |
"output some tone for nSeconds in U8 audioFormat - a test method" |
|
1653 |
||
1654 |
|buffer numSamples val scale restSamples| |
|
0 | 1655 |
|
923 | 1656 |
(audioFormat == #U8) ifFalse:[ |
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1657 |
(audioFormat == #S8) ifFalse:[ |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1658 |
self error:'must be in 8bit mode' mayProceed:true. |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1659 |
^ self |
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1660 |
] |
923 | 1661 |
]. |
1662 |
||
5400 | 1663 |
"allocate memory for 1 sec playing time" |
0 | 1664 |
numSamples := self sampleRate. |
1665 |
buffer := ByteArray new:numSamples. |
|
1666 |
||
1667 |
"fill it with a sine wave" |
|
1668 |
||
593 | 1669 |
scale := freq * 2 * (Float pi). |
0 | 1670 |
1 to:numSamples do:[:i | |
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1671 |
val := (scale * i / numSamples) sin. |
5400 | 1672 |
val := (val * 127) rounded. |
1673 |
audioFormat == #U8 ifTrue:[ |
|
1674 |
val := val + 16r80. |
|
1675 |
]. |
|
1676 |
buffer at:i put:(val bitAnd:16rFF) |
|
0 | 1677 |
]. |
1678 |
||
5400 | 1679 |
1 to:nSeconds truncated do:[:s | |
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1680 |
self nextPutBytes:numSamples from:buffer startingAt:1 |
593 | 1681 |
]. |
5400 | 1682 |
restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated. |
1683 |
restSamples > 0 ifTrue:[ |
|
1684 |
self nextPutBytes:restSamples from:buffer startingAt:1 |
|
1685 |
]. |
|
593 | 1686 |
|
1687 |
"of course, the frequency should be below half the |
|
1688 |
sampleRate - hear below ... |
|
0 | 1689 |
|
593 | 1690 |
SoundStream writing tuneTone; close |
1691 |
SoundStream writing setSampleRate:4000; tuneTone:440; close |
|
1692 |
SoundStream writing setSampleRate:4000; tuneTone:2000; close |
|
1693 |
SoundStream writing setSampleRate:8000; tuneTone:440; close |
|
1694 |
SoundStream writing setSampleRate:8000; tuneTone:2000; close |
|
1695 |
SoundStream writing setSampleRate:8000; tuneTone:4000; close |
|
1696 |
SoundStream writing setSampleRate:10000; tuneTone:440; close |
|
1697 |
SoundStream writing setSampleRate:10000; tuneTone:2000; close |
|
1698 |
SoundStream writing setSampleRate:10000; tuneTone:4000; close |
|
1699 |
SoundStream writing setSampleRate:20000; tuneTone:440; close |
|
1700 |
SoundStream writing setSampleRate:20000; tuneTone:2000; close |
|
1701 |
SoundStream writing setSampleRate:20000; tuneTone:4000; close |
|
1702 |
SoundStream writing setSampleRate:20000; tuneTone:8000; close |
|
1703 |
SoundStream writing setSampleRate:40000; tuneTone:440; close |
|
1704 |
SoundStream writing setSampleRate:40000; tuneTone:2000; close |
|
1705 |
SoundStream writing setSampleRate:40000; tuneTone:4000; close |
|
1706 |
SoundStream writing setSampleRate:40000; tuneTone:8000; close |
|
1707 |
SoundStream writing setSampleRate:40000; tuneTone:10000; close |
|
1708 |
" |
|
1709 |
||
597 | 1710 |
"Modified: / 15.12.1997 / 13:43:05 / cg" |
722 | 1711 |
"Created: / 31.1.1999 / 12:05:17 / cg" |
597 | 1712 |
! |
1713 |
||
788 | 1714 |
tuneTone:freq |
5402 | 1715 |
self tuneTone:freq seconds:3 |
1716 |
||
1717 |
" |
|
1718 |
SoundStream writing tuneTone:880; close |
|
1719 |
SoundStream writing setSampleRate:4000; tuneTone:440; close |
|
1720 |
SoundStream writing setSampleRate:4000; tuneTone:880; close |
|
1721 |
SoundStream writing setSampleRate:8000; tuneTone:440; close |
|
1722 |
SoundStream writing setSampleRate:8000; tuneTone:880; close |
|
1723 |
SoundStream writing setSampleRate:10000; tuneTone:440; close |
|
1724 |
SoundStream writing setSampleRate:10000; tuneTone:880; close |
|
1725 |
||
1726 |
SoundStream writing setSampleRate:20000; tuneTone:440; close |
|
1727 |
SoundStream writing setSampleRate:20000; tuneTone:880; close |
|
1728 |
||
1729 |
SoundStream writing setSampleRate:40000; tuneTone:440; close |
|
1730 |
SoundStream writing setSampleRate:40000; tuneTone:880; close |
|
1731 |
SoundStream writing setSampleRate:40000; tuneTone:1760; close |
|
1732 |
SoundStream writing setSampleRate:40000; tuneTone:3520; close |
|
1733 |
SoundStream writing setSampleRate:40000; tuneTone:7020; close |
|
1734 |
SoundStream writing setSampleRate:40000; tuneTone:14040; close |
|
1735 |
||
1736 |
SoundStream writing setSampleRate:44100; tuneTone:440; close |
|
1737 |
SoundStream writing setSampleRate:44100; tuneTone:880; close |
|
1738 |
SoundStream writing setSampleRate:44100; tuneTone:1760; close |
|
1739 |
SoundStream writing setSampleRate:44100; tuneTone:3520; close |
|
1740 |
SoundStream writing setSampleRate:44100; tuneTone:7020; close |
|
1741 |
SoundStream writing setSampleRate:44100; tuneTone:14040; close |
|
1742 |
||
1743 |
SoundStream writing setSampleRate:20000; dumpSettings; close |
|
1744 |
" |
|
1745 |
||
1746 |
"Modified: / 31.1.1999 / 12:07:14 / cg" |
|
1747 |
! |
|
1748 |
||
5405 | 1749 |
tuneTone:freq seconds:nSeconds |
1556
8a78c9a5a8f7
STORE macro (gcc 3.4 bug workaround)
Claus Gittinger <cg@exept.de>
parents:
1480
diff
changeset
|
1750 |
((audioFormat startsWith:#S16) |
923 | 1751 |
or:[audioFormat startsWith:#U16]) ifTrue:[ |
5405 | 1752 |
^ self tuneTone16:freq seconds:nSeconds |
5402 | 1753 |
]. |
1754 |
audioFormat == #F32 ifTrue:[ |
|
5405 | 1755 |
^ self tuneToneF32:freq seconds:nSeconds |
788 | 1756 |
]. |
1757 |
audioFormat == #MU_LAW ifTrue:[ |
|
5405 | 1758 |
^ self tuneToneMU:freq seconds:nSeconds |
788 | 1759 |
]. |
5402 | 1760 |
self tuneTone8:freq seconds:nSeconds |
788 | 1761 |
|
1762 |
" |
|
1763 |
SoundStream writing tuneTone:880; close |
|
923 | 1764 |
SoundStream writing setSampleRate:4000; tuneTone:440; close |
788 | 1765 |
SoundStream writing setSampleRate:4000; tuneTone:880; close |
923 | 1766 |
SoundStream writing setSampleRate:8000; tuneTone:440; close |
788 | 1767 |
SoundStream writing setSampleRate:8000; tuneTone:880; close |
923 | 1768 |
SoundStream writing setSampleRate:10000; tuneTone:440; close |
788 | 1769 |
SoundStream writing setSampleRate:10000; tuneTone:880; close |
923 | 1770 |
|
1771 |
SoundStream writing setSampleRate:20000; tuneTone:440; close |
|
788 | 1772 |
SoundStream writing setSampleRate:20000; tuneTone:880; close |
923 | 1773 |
|
1774 |
SoundStream writing setSampleRate:40000; tuneTone:440; close |
|
788 | 1775 |
SoundStream writing setSampleRate:40000; tuneTone:880; close |
923 | 1776 |
SoundStream writing setSampleRate:40000; tuneTone:1760; close |
1777 |
SoundStream writing setSampleRate:40000; tuneTone:3520; close |
|
1778 |
SoundStream writing setSampleRate:40000; tuneTone:7020; close |
|
1779 |
SoundStream writing setSampleRate:40000; tuneTone:14040; close |
|
1780 |
||
1781 |
SoundStream writing setSampleRate:44100; tuneTone:440; close |
|
1782 |
SoundStream writing setSampleRate:44100; tuneTone:880; close |
|
1783 |
SoundStream writing setSampleRate:44100; tuneTone:1760; close |
|
1784 |
SoundStream writing setSampleRate:44100; tuneTone:3520; close |
|
1785 |
SoundStream writing setSampleRate:44100; tuneTone:7020; close |
|
1786 |
SoundStream writing setSampleRate:44100; tuneTone:14040; close |
|
1787 |
||
788 | 1788 |
SoundStream writing setSampleRate:20000; dumpSettings; close |
1789 |
" |
|
1790 |
||
1791 |
"Modified: / 31.1.1999 / 12:07:14 / cg" |
|
1792 |
! |
|
1793 |
||
5400 | 1794 |
tuneToneF32:freq seconds:nSeconds |
1795 |
"output some tone for nSeconds in F32 audioFormat - a test method" |
|
1796 |
||
1797 |
|buffer numSamples val scale restSamples| |
|
1798 |
||
1799 |
(audioFormat == #F32) ifFalse:[ |
|
1800 |
self error:'must be in f32 mode' mayProceed:true. |
|
1801 |
^ self |
|
1802 |
]. |
|
1803 |
||
1804 |
"allocate memory for 1sec playing time" |
|
1805 |
numSamples := self sampleRate. |
|
1806 |
buffer := FloatArray new:numSamples. |
|
1807 |
||
1808 |
"fill it with a sine wave" |
|
1809 |
||
1810 |
scale := freq * 2 * (Float pi). |
|
1811 |
1 to:numSamples do:[:i | |
|
1812 |
val := (scale * i / numSamples) sin. |
|
1813 |
buffer at:i put:val |
|
1814 |
]. |
|
1815 |
||
1816 |
1 to:nSeconds truncated do:[:s | |
|
1817 |
self nextPutBytes:(numSamples*4) from:buffer startingAt:1 |
|
1818 |
]. |
|
1819 |
restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated. |
|
1820 |
restSamples > 0 ifTrue:[ |
|
1821 |
self nextPutBytes:(restSamples*4) from:buffer startingAt:1 |
|
1822 |
]. |
|
1823 |
||
1824 |
"of course, the frequency should be below half the |
|
1825 |
sampleRate - hear below ... |
|
1826 |
||
1827 |
SoundStream writing tuneTone; close |
|
1828 |
SoundStream writing setSampleRate:4000; tuneTone:440; close |
|
1829 |
SoundStream writing setSampleRate:4000; tuneTone:2000; close |
|
1830 |
SoundStream writing setSampleRate:8000; tuneTone:440; close |
|
1831 |
SoundStream writing setSampleRate:8000; tuneTone:2000; close |
|
1832 |
SoundStream writing setSampleRate:8000; tuneTone:4000; close |
|
1833 |
SoundStream writing setSampleRate:10000; tuneTone:440; close |
|
1834 |
SoundStream writing setSampleRate:10000; tuneTone:2000; close |
|
1835 |
SoundStream writing setSampleRate:10000; tuneTone:4000; close |
|
1836 |
SoundStream writing setSampleRate:20000; tuneTone:440; close |
|
1837 |
SoundStream writing setSampleRate:20000; tuneTone:2000; close |
|
1838 |
SoundStream writing setSampleRate:20000; tuneTone:4000; close |
|
1839 |
SoundStream writing setSampleRate:20000; tuneTone:8000; close |
|
1840 |
SoundStream writing setSampleRate:40000; tuneTone:440; close |
|
1841 |
SoundStream writing setSampleRate:40000; tuneTone:2000; close |
|
1842 |
SoundStream writing setSampleRate:40000; tuneTone:4000; close |
|
1843 |
SoundStream writing setSampleRate:40000; tuneTone:8000; close |
|
1844 |
SoundStream writing setSampleRate:40000; tuneTone:10000; close |
|
1845 |
" |
|
1846 |
||
1847 |
"Modified: / 15.12.1997 / 13:43:05 / cg" |
|
1848 |
"Created: / 31.1.1999 / 12:05:17 / cg" |
|
1849 |
! |
|
1850 |
||
5402 | 1851 |
tuneToneMU:freq seconds:nSeconds |
1852 |
"output some tone for nSeconds in MU_LAW audioFormat - a test method" |
|
1853 |
||
1854 |
|buffer numSamples val scale restSamples| |
|
597 | 1855 |
|
1856 |
"allocate memory for 1sec playing time" |
|
1857 |
numSamples := self sampleRate. |
|
1858 |
buffer := ByteArray new:numSamples. |
|
1859 |
||
1860 |
"fill it with a sine wave" |
|
1861 |
||
1862 |
scale := freq * 2 * (Float pi). |
|
1863 |
1 to:numSamples do:[:i | |
|
5405 | 1864 |
val := (scale * i / numSamples) sin. |
1865 |
val := (val * 16r7FFF) rounded. |
|
1866 |
buffer at:i put:(self class linear16ToUlaw:val) |
|
597 | 1867 |
]. |
1868 |
||
5402 | 1869 |
1 to:nSeconds truncated do:[:s | |
5405 | 1870 |
self nextPutBytes:numSamples from:buffer startingAt:1 |
597 | 1871 |
]. |
5402 | 1872 |
restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated. |
1873 |
restSamples > 0 ifTrue:[ |
|
5405 | 1874 |
self nextPutBytes:restSamples from:buffer startingAt:1 |
5402 | 1875 |
]. |
597 | 1876 |
|
1877 |
"of course, the frequency should be below half the |
|
1878 |
sampleRate - hear below ... |
|
1879 |
||
1880 |
SoundStream writing setSampleRate:8000; tuneToneMU:440; close |
|
1881 |
SoundStream writing setSampleRate:10000; tuneToneMU:440; close |
|
1882 |
SoundStream writing setSampleRate:20000; tuneToneMU:440; close |
|
1883 |
SoundStream writing setSampleRate:40000; tuneToneMU:440; close |
|
1884 |
" |
|
1885 |
||
1886 |
"Modified: / 15.12.1997 / 13:46:19 / cg" |
|
0 | 1887 |
! ! |
121
606a3ccd3682
linux fixes (cut off when closing)
Claus Gittinger <cg@exept.de>
parents:
112
diff
changeset
|
1888 |
|
5402 | 1889 |
!SoundStream::DevAudio class methodsFor:'default values'! |
1890 |
||
1891 |
defaultSampleRate |
|
1892 |
"minimum, supported by all audio systems" |
|
1893 |
||
1894 |
%{ |
|
1895 |
#ifdef SUPPORT_DEV_AUDIO |
|
1896 |
RETURN (__MKSMALLINT(DEV_AUDIO_DEFAULT_FREQ)); |
|
1897 |
#endif |
|
1898 |
%}. |
|
1899 |
||
1900 |
^ 8000 |
|
1901 |
! ! |
|
1902 |
||
1903 |
!SoundStream::DevAudio class methodsFor:'documentation'! |
|
1904 |
||
1905 |
documentation |
|
1906 |
" |
|
1907 |
documentation to be added. |
|
1908 |
||
1909 |
class: |
|
5405 | 1910 |
<a short class summary here, describing what instances represent> |
1911 |
||
1912 |
responsibilities: |
|
1913 |
<describing what my main role is> |
|
1914 |
||
1915 |
collaborators: |
|
1916 |
<describing with whom and how I talk to> |
|
5402 | 1917 |
|
1918 |
API: |
|
5405 | 1919 |
<public api and main messages> |
1920 |
||
5402 | 1921 |
example: |
5405 | 1922 |
<a one-line examples on how to use - can also be in a separate example method> |
5402 | 1923 |
|
1924 |
implementation: |
|
5405 | 1925 |
<implementation points> |
5402 | 1926 |
|
1927 |
[author:] |
|
5405 | 1928 |
exept MBP |
5402 | 1929 |
|
1930 |
[instance variables:] |
|
1931 |
||
1932 |
[class variables:] |
|
1933 |
||
1934 |
[see also:] |
|
1935 |
||
1936 |
" |
|
1937 |
! ! |
|
1938 |
||
1939 |
!SoundStream::DevAudio class methodsFor:'queries'! |
|
1940 |
||
1941 |
isSupported |
|
1942 |
%{ |
|
1943 |
#ifdef SUPPORT_DEV_AUDIO |
|
1944 |
RETURN(true); |
|
1945 |
#endif |
|
1946 |
%}. |
|
1947 |
^ false. |
|
1948 |
! ! |
|
1949 |
||
1950 |
!SoundStream::DevAudio methodsFor:'misc'! |
|
1951 |
||
1952 |
dumpSettings |
|
1953 |
"debugging interface - dump the current settings" |
|
1954 |
||
1955 |
|fd blockSize speed channels stereo format| |
|
1956 |
||
1957 |
fd := self fileDescriptorOrNil. |
|
1958 |
||
1959 |
audioFormat == #S16 ifTrue:[ |
|
5405 | 1960 |
UninterpretedBytes isBigEndian ifTrue:[ |
1961 |
audioFormat := #S16_BE |
|
1962 |
] ifFalse:[ |
|
1963 |
audioFormat := #S16_LE |
|
1964 |
] |
|
5402 | 1965 |
]. |
1966 |
audioFormat == #U16 ifTrue:[ |
|
5405 | 1967 |
UninterpretedBytes isBigEndian ifTrue:[ |
1968 |
audioFormat := #U16_BE |
|
1969 |
] ifFalse:[ |
|
1970 |
audioFormat := #U16_LE |
|
1971 |
] |
|
5402 | 1972 |
]. |
1973 |
||
1974 |
%{ |
|
1975 |
#ifdef SUPPORT_DEV_AUDIO |
|
1976 |
if (fd != nil) { |
|
5405 | 1977 |
int f = __intVal(fd); |
1978 |
int __blockSize = -1; |
|
1979 |
int __speed = -1; |
|
1980 |
int __channels = __intVal(__INST(numberOfChannels)); |
|
1981 |
int __stereo = __channels > 1; |
|
1982 |
int __format = -1; |
|
1983 |
||
1984 |
channels = nil; |
|
1985 |
blockSize = nil; |
|
1986 |
stereo = nil; |
|
1987 |
speed = nil; |
|
5402 | 1988 |
|
1989 |
# if defined(SNDCTL_DSP_GETBLKSIZE) |
|
5405 | 1990 |
if (ioctl(f, SNDCTL_DSP_GETBLKSIZE, &__blockSize) >= 0) { |
1991 |
blockSize = __MKSMALLINT(__blockSize); |
|
1992 |
} |
|
5402 | 1993 |
# endif |
1994 |
||
1995 |
# if defined(SNDCTL_DSP_CHANNELS) |
|
5405 | 1996 |
if (ioctl(f, SNDCTL_DSP_CHANNELS, &__channels) >= 0) { |
1997 |
channels = __MKSMALLINT(__channels); |
|
1998 |
stereo = __MKSMALLINT(__channels > 1); |
|
1999 |
} |
|
5402 | 2000 |
# else |
2001 |
# if defined(SNDCTL_DSP_STEREO) |
|
5405 | 2002 |
if (ioctl(f, SNDCTL_DSP_STEREO, &__stereo) >= 0) { |
2003 |
stereo = __MKSMALLINT(__stereo); |
|
2004 |
} |
|
5402 | 2005 |
# endif |
2006 |
# endif |
|
2007 |
||
2008 |
# if defined(SNDCTL_DSP_SPEED) |
|
5405 | 2009 |
if (ioctl(f, SNDCTL_DSP_SPEED, &__speed) >= 0) { |
2010 |
speed = __MKSMALLINT(__speed); |
|
2011 |
} |
|
5402 | 2012 |
# endif |
2013 |
||
2014 |
# if defined(SNDCTL_DSP_GETFMT) |
|
5405 | 2015 |
if (ioctl(f, SNDCTL_DSP_GETFMT, &__format) >= 0) { |
2016 |
format = __MKSMALLINT(__format); |
|
2017 |
} |
|
5402 | 2018 |
# else |
2019 |
# if defined(SNDCTL_DSP_SETFMT) && defined(AFMT_QUERY) |
|
5405 | 2020 |
__format = AFMT_QUERY; |
2021 |
if (ioctl(f, SNDCTL_DSP_SETFMT, &__format) >= 0) { |
|
2022 |
switch (__format) { |
|
5402 | 2023 |
# ifdef AFMT_MU_LAW |
5405 | 2024 |
case AFMT_MU_LAW: |
2025 |
format = @symbol(MU_LAW); |
|
2026 |
break; |
|
5402 | 2027 |
# endif |
2028 |
# ifdef AFMT_A_LAW |
|
5405 | 2029 |
case AFMT_A_LAW: |
2030 |
format = @symbol(A_LAW); |
|
2031 |
break; |
|
5402 | 2032 |
# endif |
2033 |
# ifdef AFMT_U8 |
|
5405 | 2034 |
case AFMT_U8: |
2035 |
format = @symbol(U8); |
|
2036 |
break; |
|
5402 | 2037 |
# endif |
2038 |
# ifdef AFMT_S8 |
|
5405 | 2039 |
case AFMT_S8: |
2040 |
format = @symbol(S8); |
|
2041 |
break; |
|
5402 | 2042 |
# endif |
2043 |
# ifdef AFMT_S16_LE |
|
5405 | 2044 |
case AFMT_S16_LE: |
2045 |
format = @symbol(S16_LE); |
|
2046 |
break; |
|
5402 | 2047 |
# endif |
2048 |
# ifdef AFMT_S16_BE |
|
5405 | 2049 |
case AFMT_S16_BE: |
2050 |
format = @symbol(S16_BE); |
|
2051 |
break; |
|
5402 | 2052 |
# endif |
2053 |
# ifdef AFMT_U16_LE |
|
5405 | 2054 |
case AFMT_U16_LE: |
2055 |
format = @symbol(U16_LE); |
|
2056 |
break; |
|
5402 | 2057 |
# endif |
2058 |
# ifdef AFMT_U16_BE |
|
5405 | 2059 |
case AFMT_U16_BE: |
2060 |
format = @symbol(U16_BE); |
|
2061 |
break; |
|
5402 | 2062 |
# endif |
2063 |
# ifdef AFMT_MPEG |
|
5405 | 2064 |
case AFMT_MPEG: |
2065 |
format = @symbol(MPEG); |
|
2066 |
break; |
|
5402 | 2067 |
# endif |
5405 | 2068 |
default: |
2069 |
format = nil; |
|
2070 |
} |
|
2071 |
} |
|
5402 | 2072 |
} |
2073 |
# endif |
|
2074 |
# endif |
|
2075 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2076 |
||
2077 |
%}. |
|
2078 |
format notNil ifTrue:[ |
|
5405 | 2079 |
Transcript show:'format: '; showCR:format |
5402 | 2080 |
]. |
2081 |
blockSize notNil ifTrue:[ |
|
5405 | 2082 |
Transcript show:'blockSize: '; showCR:blockSize |
5402 | 2083 |
]. |
2084 |
speed notNil ifTrue:[ |
|
5405 | 2085 |
Transcript show:'speed: '; showCR:speed |
5402 | 2086 |
]. |
2087 |
channels notNil ifTrue:[ |
|
5405 | 2088 |
Transcript show:'channels: '; showCR:channels |
5402 | 2089 |
]. |
2090 |
stereo notNil ifTrue:[ |
|
5405 | 2091 |
Transcript show:'stereo: '; showCR:stereo |
5402 | 2092 |
]. |
2093 |
||
2094 |
Transcript show:'supported audioFormats: '; showCR:(self supportedAudioFormats). |
|
2095 |
||
2096 |
" |
|
2097 |
self writing dumpSettings; close |
|
2098 |
" |
|
2099 |
! |
|
2100 |
||
2101 |
flush |
|
2102 |
"wait until all sound has been played" |
|
2103 |
||
2104 |
self fileDescriptorOrNil isNil ifTrue:[^ self]. |
|
2105 |
super flush. |
|
2106 |
! ! |
|
2107 |
||
2108 |
!SoundStream::DevAudio methodsFor:'open & close'! |
|
2109 |
||
2110 |
closeFile |
|
2111 |
"a stream has been collected - close the file" |
|
2112 |
||
2113 |
%{ |
|
2114 |
#ifdef SUPPORT_DEV_AUDIO |
|
2115 |
OBJ fp; |
|
2116 |
int fd; |
|
2117 |
FILE *f; |
|
2118 |
||
2119 |
if ((fp = __INST(handle)) != nil) { |
|
5405 | 2120 |
f = __FILEVal(fp); |
2121 |
__INST(handle) = nil; |
|
5402 | 2122 |
# ifdef LINUX |
5405 | 2123 |
sigsetmask(~0); |
5402 | 2124 |
# endif |
5405 | 2125 |
if (__INST(buffered) == true) { |
2126 |
fflush(f); |
|
2127 |
fclose(f); |
|
2128 |
} else { |
|
2129 |
fd = fileno(f); |
|
2130 |
close(fd); |
|
2131 |
fclose(f); |
|
2132 |
} |
|
5402 | 2133 |
# ifdef LINUX |
5405 | 2134 |
sigsetmask(0); |
5402 | 2135 |
# endif |
2136 |
} |
|
2137 |
RETURN (self); |
|
2138 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2139 |
%}. |
|
2140 |
! |
|
2141 |
||
2142 |
openWithMode:aMode attributes:attributeSpec |
|
2143 |
|ok error errorStringOrNil| |
|
2144 |
||
2145 |
%{ |
|
2146 |
#ifdef SUPPORT_DEV_AUDIO |
|
2147 |
int __fd; |
|
2148 |
int __mode; |
|
2149 |
FILE *f; |
|
2150 |
||
2151 |
ok = false; |
|
2152 |
if (strcmp(__stringVal(aMode), "w") == 0) { |
|
5405 | 2153 |
__mode = O_WRONLY; |
5402 | 2154 |
} else if (strcmp(__stringVal(aMode), "r") == 0) { |
5405 | 2155 |
__mode = O_RDONLY; |
5402 | 2156 |
} else { |
5405 | 2157 |
__mode = O_RDWR; |
5402 | 2158 |
} |
2159 |
do { |
|
5405 | 2160 |
__BEGIN_INTERRUPTABLE__ |
2161 |
__fd = open((char *) __stringVal(__INST(pathName)), __mode /* |O_NDELAY */); |
|
2162 |
__END_INTERRUPTABLE__ |
|
5402 | 2163 |
} while ((__fd < 0) && (errno == EINTR)); |
2164 |
||
2165 |
if (__fd >= 0) { |
|
5405 | 2166 |
/* |
2167 |
* make it a FILE * |
|
2168 |
*/ |
|
2169 |
f = fdopen(__fd, __stringVal(aMode)); |
|
2170 |
if (! f) { |
|
2171 |
error = __mkSmallInteger(errno); |
|
2172 |
__BEGIN_INTERRUPTABLE__ |
|
2173 |
close(__fd); |
|
2174 |
__END_INTERRUPTABLE__ |
|
2175 |
goto out; |
|
2176 |
} |
|
2177 |
setbuf(f, NULL); |
|
2178 |
__INST(buffered) = false; |
|
2179 |
__INST(handle) = __MKEXTERNALADDRESS(f); |
|
2180 |
__STORESELF(handle); |
|
5402 | 2181 |
|
2182 |
#if defined(PCM_ENABLE_OUTPUT) && defined(PCM_ENABLE_INPUT) |
|
2183 |
# if defined(SNDCTL_DSP_SETTRIGGER) |
|
5405 | 2184 |
if (__mode == O_RDWR) { |
2185 |
int enable_bits = ~(PCM_ENABLE_OUTPUT|PCM_ENABLE_INPUT); |
|
2186 |
||
2187 |
if (ioctl(__fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) == -1) |
|
2188 |
{ |
|
2189 |
console_fprintf(stderr, "can't request synchronous start of fullduplex operation"); |
|
2190 |
} |
|
2191 |
} |
|
5402 | 2192 |
# endif |
2193 |
#endif |
|
5405 | 2194 |
ok = true; |
5402 | 2195 |
} else { |
5405 | 2196 |
error = __mkSmallInteger(errno); |
5402 | 2197 |
} |
2198 |
out:; |
|
2199 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2200 |
%}. |
|
2201 |
ok == false ifTrue:[ |
|
5405 | 2202 |
lastErrorString := errorStringOrNil. |
2203 |
lastErrorNumber := error ? -1. |
|
2204 |
self openError:error. |
|
2205 |
"normally not reached" |
|
2206 |
^ nil. |
|
5402 | 2207 |
]. |
2208 |
self registerForFinalization. |
|
2209 |
! ! |
|
2210 |
||
2211 |
!SoundStream::DevAudio methodsFor:'private'! |
|
2212 |
||
2213 |
initialize |
|
2214 |
"initialize for least common mode" |
|
2215 |
||
2216 |
super initialize. |
|
2217 |
||
2218 |
OperatingSystem isUNIXlike ifTrue:[ |
|
5405 | 2219 |
'/dev/audio' asFilename exists ifTrue:[ |
2220 |
"/ |
|
2221 |
"/ sunos or linux |
|
2222 |
"/ |
|
2223 |
pathName := '/dev/audio'. |
|
2224 |
]. |
|
5402 | 2225 |
]. |
2226 |
! |
|
2227 |
||
2228 |
resetSoundCard |
|
2229 |
"debugging interface - reset the soundCard" |
|
2230 |
||
2231 |
|fd| |
|
2232 |
||
2233 |
fd := self fileDescriptorOrNil. |
|
2234 |
fd isNil ifTrue:[ |
|
5405 | 2235 |
self errorNotOpen. |
2236 |
^ nil |
|
5402 | 2237 |
]. |
2238 |
%{ |
|
2239 |
#ifdef SUPPORT_DEV_AUDIO |
|
2240 |
int f = __intVal(fd); |
|
2241 |
||
2242 |
if (__isSmallInteger(fd)) { |
|
5405 | 2243 |
int __dummy; |
5402 | 2244 |
# if defined(SNDCTL_DSP_RESET) |
5405 | 2245 |
if (ioctl(f, SNDCTL_DSP_RESET, &__dummy) >= 0) { |
2246 |
RETURN (self); |
|
2247 |
} |
|
5402 | 2248 |
# endif |
2249 |
} |
|
2250 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2251 |
%}. |
|
2252 |
^ UnsupportedOperationSignal raise |
|
2253 |
||
2254 |
" |
|
2255 |
self writing resetSoundCard; dumpSettings; close |
|
2256 |
" |
|
2257 |
! |
|
2258 |
||
2259 |
setAudioFormat:aSymbol |
|
2260 |
"set the format of the audio data as specified by aSymbol. |
|
2261 |
Returns true if sucessful - may fail with some formats on many sound devices." |
|
2262 |
||
2263 |
|fd ok| |
|
2264 |
||
2265 |
audioFormat == aSymbol ifTrue:[^ self]. |
|
2266 |
super setAudioFormat:aSymbol. |
|
2267 |
||
2268 |
fd := self fileDescriptor. |
|
2269 |
fd isNil ifTrue:[ |
|
5405 | 2270 |
^ self |
5402 | 2271 |
]. |
2272 |
%{ |
|
2273 |
#ifdef SUPPORT_DEV_AUDIO |
|
2274 |
OBJ sym = aSymbol; |
|
2275 |
||
2276 |
if (__isSmallInteger(fd)) { |
|
5405 | 2277 |
int f = __intVal(fd); |
2278 |
int __fmt = 0, __fmtWant; |
|
2279 |
union { |
|
2280 |
unsigned short us; |
|
2281 |
unsigned char ub[2]; |
|
2282 |
} u; |
|
2283 |
||
2284 |
if (__isSymbol(sym)) { |
|
2285 |
if (sym == @symbol(U16)) { |
|
2286 |
u.us = 0x1234; |
|
2287 |
if (u.ub[0] == 0x34) { |
|
2288 |
/* printf("U16_LE\n"); */ |
|
2289 |
sym = @symbol(U16_LE); |
|
2290 |
} else { |
|
2291 |
/* printf("U16_BE\n"); */ |
|
2292 |
sym = @symbol(U16_BE); |
|
2293 |
} |
|
2294 |
} else if (sym == @symbol(S16)) { |
|
2295 |
u.us = 0x1234; |
|
2296 |
if (u.ub[0] == 0x34) { |
|
2297 |
/* printf("S16_LE\n"); */ |
|
2298 |
sym = @symbol(S16_LE); |
|
2299 |
} else { |
|
2300 |
/* printf("S16_BE\n"); */ |
|
2301 |
sym = @symbol(S16_BE); |
|
2302 |
} |
|
2303 |
} |
|
2304 |
||
2305 |
if (0) { |
|
5402 | 2306 |
# ifdef AFMT_MU_LAW |
5405 | 2307 |
} else if (sym == @symbol(MU_LAW)) { |
2308 |
__fmt = AFMT_MU_LAW; |
|
5402 | 2309 |
# endif |
2310 |
# ifdef AFMT_A_LAW |
|
5405 | 2311 |
} else if (sym == @symbol(A_LAW)) { |
2312 |
__fmt = AFMT_A_LAW; |
|
5402 | 2313 |
# endif |
2314 |
# ifdef AFMT_IMA_ADPCM |
|
5405 | 2315 |
} else if (sym == @symbol(IMA_ADPCM)) { |
2316 |
__fmt = AFMT_IMA_ADPCM; |
|
5402 | 2317 |
# endif |
2318 |
# ifdef AFMT_U8 |
|
5405 | 2319 |
} else if (sym == @symbol(U8)) { |
2320 |
__fmt = AFMT_U8; |
|
5402 | 2321 |
# endif |
2322 |
# ifdef AFMT_S8 |
|
5405 | 2323 |
} else if (sym == @symbol(S8)) { |
2324 |
__fmt = AFMT_S8; |
|
5402 | 2325 |
# endif |
2326 |
# ifdef AFMT_U16_LE |
|
5405 | 2327 |
} else if (sym == @symbol(U16_LE)) { |
2328 |
__fmt = AFMT_U16_LE; |
|
5402 | 2329 |
# endif |
2330 |
# ifdef AFMT_U16_BE |
|
5405 | 2331 |
} else if (sym == @symbol(U16_BE)) { |
2332 |
__fmt = AFMT_U16_BE; |
|
5402 | 2333 |
# endif |
2334 |
# ifdef AFMT_S16_LE |
|
5405 | 2335 |
} else if (sym == @symbol(S16_LE)) { |
2336 |
__fmt = AFMT_S16_LE; |
|
5402 | 2337 |
# endif |
2338 |
# ifdef AFMT_S16_BE |
|
5405 | 2339 |
} else if (sym == @symbol(S16_BE)) { |
2340 |
__fmt = AFMT_S16_BE; |
|
5402 | 2341 |
# endif |
2342 |
# ifdef AFMT_MPEG |
|
5405 | 2343 |
} else if (sym == @symbol(MPEG)) { |
2344 |
__fmt = AFMT_MPEG; |
|
5402 | 2345 |
# endif |
2346 |
||
2347 |
# ifdef AUDIO_FORMAT_LINEAR16BIT |
|
5405 | 2348 |
} else if (sym == @symbol(S16)) { |
2349 |
__fmt = AUDIO_FORMAT_LINEAR16BIT; |
|
5402 | 2350 |
# endif |
2351 |
# ifdef AUDIO_FORMAT_ULAW |
|
5405 | 2352 |
} else if (sym == @symbol(MU_LAW)) { |
2353 |
__fmt = AUDIO_FORMAT_ULAW; |
|
5402 | 2354 |
# endif |
2355 |
# ifdef AUDIO_FORMAT_ALAW |
|
5405 | 2356 |
} else if (sym == @symbol(A_LAW)) { |
2357 |
__fmt = AUDIO_FORMAT_ALAW; |
|
5402 | 2358 |
# endif |
5405 | 2359 |
} else { |
2360 |
console_fprintf(stderr, "bad format: %s\n", __stringVal(sym)); |
|
2361 |
ok = false; |
|
2362 |
goto bad; |
|
2363 |
} |
|
2364 |
} |
|
5402 | 2365 |
|
2366 |
# ifdef SNDCTL_DSP_SETFMT |
|
5405 | 2367 |
__fmtWant = __fmt; |
2368 |
||
2369 |
if (ioctl(f, SNDCTL_DSP_SETFMT, &__fmt) >= 0) { |
|
2370 |
if (__fmt == __fmtWant) { |
|
2371 |
__INST(audioFormat) = sym; |
|
2372 |
} else { |
|
2373 |
/* console_fprintf(stderr, "want: %x; got: %x\n", __fmtWant, __fmt); */ |
|
2374 |
} |
|
2375 |
} else { |
|
2376 |
/* console_fprintf(stderr, "got err-ret from setFmp %x\n", __fmt); */ |
|
2377 |
ok = false; |
|
2378 |
} |
|
5402 | 2379 |
# else |
2380 |
# ifdef AUDIO_SET_DATA_FORMAT /* hpux */ |
|
5405 | 2381 |
if (ioctl (f, AUDIO_SET_DATA_FORMAT, __fmt)) { |
2382 |
/* console_fprintf(stderr, "got err-ret from AUDIO_SET_DATA_FORMAT\n"); */ |
|
2383 |
} |
|
5402 | 2384 |
# endif |
2385 |
# endif /* SNDCTL_DSP_SETFMT */ |
|
2386 |
||
2387 |
bad: ; |
|
2388 |
} |
|
2389 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2390 |
%}. |
|
2391 |
||
2392 |
" |
|
2393 |
self writing dumpSettings; close |
|
2394 |
self writing setAudioFormat:#'MU_LAW'; close |
|
2395 |
self writing setAudioFormat:#'U8'; dumpSettings; close |
|
2396 |
self writing setAudioFormat:#'MPEG'; dumpSettings; close |
|
2397 |
" |
|
2398 |
! |
|
2399 |
||
2400 |
setChannels:nChannels |
|
2401 |
"set the number of channels (1 -> mono; 2 -> stereo). |
|
2402 |
Returns true if sucessful - may fail with many sound devices." |
|
2403 |
||
2404 |
|fd| |
|
2405 |
||
2406 |
numberOfChannels := nChannels. |
|
2407 |
||
2408 |
fd := self fileDescriptorOrNil. |
|
2409 |
fd isNil ifTrue:[ |
|
5405 | 2410 |
^ self |
5402 | 2411 |
]. |
2412 |
%{ |
|
2413 |
||
2414 |
#ifdef SUPPORT_DEV_AUDIO |
|
2415 |
if (__isSmallInteger(fd) && __isSmallInteger(nChannels)) { |
|
5405 | 2416 |
int f = __intVal(fd); |
2417 |
int __nCh = __intVal(nChannels); |
|
5402 | 2418 |
|
2419 |
# ifdef SNDCTL_DSP_STEREO |
|
5405 | 2420 |
if ((__nCh == 1) || (__nCh == 2)) { |
2421 |
int __stereo = (__nCh == 2) ? 1 : 0; |
|
2422 |
||
2423 |
if (ioctl(f, SNDCTL_DSP_STEREO, &__stereo) >= 0) { |
|
2424 |
if (__stereo == 0) { |
|
2425 |
__INST(numberOfChannels) = __MKSMALLINT(1); |
|
2426 |
} else { |
|
2427 |
__INST(numberOfChannels) = __MKSMALLINT(2); |
|
2428 |
} |
|
2429 |
RETURN (self); |
|
2430 |
} |
|
2431 |
} |
|
5402 | 2432 |
# else |
2433 |
# ifdef SOUND_PCM_WRITE_CHANNELS |
|
5405 | 2434 |
if (ioctl(f, SOUND_PCM_WRITE_CHANNELS, &__nCh) >= 0) { |
2435 |
RETURN (self); |
|
2436 |
} |
|
5402 | 2437 |
# else |
2438 |
# ifdef AUDIO_SET_CHANNELS /* hpux */ |
|
5405 | 2439 |
if (ioctl (f, AUDIO_SET_CHANNELS, __nCh)) { |
2440 |
/* console_fprintf(stderr, "got err-ret from AUDIO_SET_CHANNELS\n"); */ |
|
2441 |
RETURN (self); |
|
2442 |
} |
|
5402 | 2443 |
# endif |
2444 |
# endif |
|
2445 |
# endif |
|
2446 |
} |
|
2447 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2448 |
%}. |
|
2449 |
||
2450 |
" |
|
2451 |
self writing setChannels:2; dumpSettings; close |
|
2452 |
self writing setChannels:2; setSampleRate:10000; dumpSettings; close |
|
2453 |
self writing setChannels:2; setSampleRate:40000; dumpSettings; close |
|
2454 |
" |
|
2455 |
! |
|
2456 |
||
2457 |
setFragmentSize:blockSize |
|
2458 |
"set the soundDriver's fragmentSize" |
|
2459 |
||
2460 |
|fd| |
|
2461 |
||
2462 |
fragmentSize := blockSize. |
|
2463 |
||
2464 |
fd := self fileDescriptorOrNil. |
|
2465 |
fd isNil ifTrue:[ |
|
5405 | 2466 |
^ self |
5402 | 2467 |
]. |
2468 |
||
2469 |
%{ |
|
2470 |
#ifdef SUPPORT_DEV_AUDIO |
|
2471 |
int f = __intVal(fd); |
|
2472 |
int __blockSize = __intVal(blockSize); |
|
2473 |
||
2474 |
if (__isSmallInteger(fd) && __isSmallInteger(blockSize)) { |
|
5405 | 2475 |
__blockSize = __intVal(blockSize); |
5402 | 2476 |
# if defined(SNDCTL_DSP_SETFRAGMENT) |
5405 | 2477 |
if (ioctl(f, SNDCTL_DSP_SETFRAGMENT, &__blockSize) >= 0) { |
2478 |
RETURN (self); |
|
2479 |
} |
|
5402 | 2480 |
# endif |
2481 |
# ifdef AUDIO_SET_CHANNELS /* hpux */ |
|
5405 | 2482 |
if (ioctl (f, AUDIO_SET_TXBUFSIZE, __blockSize)) { |
2483 |
/* console_fprintf(stderr, "got err-ret from AUDIO_SET_TXBUFSIZE\n"); */ |
|
2484 |
RETURN (self); |
|
2485 |
} |
|
5402 | 2486 |
# endif |
2487 |
} |
|
2488 |
#endif // SUPPORT_DEV_AUDIO |
|
2489 |
%}. |
|
2490 |
! |
|
2491 |
||
2492 |
setSampleRate:hz |
|
2493 |
"set the sample rate" |
|
2494 |
||
2495 |
|fd| |
|
2496 |
||
2497 |
sampleRate := hz. |
|
2498 |
||
2499 |
fd := self fileDescriptorOrNil. |
|
2500 |
fd isNil ifTrue:[ |
|
5405 | 2501 |
^ self |
5402 | 2502 |
]. |
2503 |
%{ |
|
2504 |
#ifdef SUPPORT_DEV_AUDIO |
|
2505 |
int f = __intVal(fd); |
|
2506 |
int __rate = __intVal(hz); |
|
2507 |
||
2508 |
if (__isSmallInteger(fd) && __isSmallInteger(hz)) { |
|
5405 | 2509 |
int __rateWant = __rate; |
5402 | 2510 |
|
2511 |
# if defined(SNDCTL_DSP_SPEED) |
|
5405 | 2512 |
if (ioctl(f, SNDCTL_DSP_SPEED, &__rate) >= 0) { |
2513 |
if (__rate != __rateWant) { |
|
2514 |
console_fprintf(stderr, "SoundStream [warning]: actual rate is %d\n", __rate); |
|
2515 |
hz = __MKSMALLINT(__rate); |
|
2516 |
} |
|
2517 |
__INST(sampleRate) = hz; |
|
2518 |
RETURN (self); |
|
2519 |
} |
|
5402 | 2520 |
# else |
2521 |
# if defined(SOUND_PCM_WRITE_RATE) |
|
5405 | 2522 |
if (ioctl(f, SOUND_PCM_WRITE_RATE, &__rate) >= 0) { |
2523 |
if (__rate != __rateWant) { |
|
2524 |
console_fprintf(stderr, "SoundStream [warning]: actual rate is %d\n", __rate); |
|
2525 |
hz = __MKSMALLINT(__rate); |
|
2526 |
} |
|
2527 |
__INST(sampleRate) = hz; |
|
2528 |
RETURN (self); |
|
2529 |
} |
|
5402 | 2530 |
# else |
2531 |
# ifdef AUDIO_SET_SAMPLE_RATE /* hpux */ |
|
5405 | 2532 |
if (ioctl (f, AUDIO_SET_SAMPLE_RATE, __rate)) { |
2533 |
/* console_fprintf(stderr, "got err-ret from AUDIO_SET_SAMPLE_RATE\n"); */ |
|
2534 |
__INST(sampleRate) = hz; |
|
2535 |
RETURN (self); |
|
2536 |
} |
|
5402 | 2537 |
# endif |
2538 |
# endif |
|
2539 |
# endif |
|
2540 |
} |
|
2541 |
#endif // SUPPORT_DEV_AUDIO |
|
2542 |
%}. |
|
2543 |
||
2544 |
" |
|
2545 |
self writing setSampleRate:10000; dumpSettings; close |
|
2546 |
self writing setSampleRate:1000; dumpSettings; close |
|
2547 |
self writing setSampleRate:8000; dumpSettings; close |
|
2548 |
" |
|
2549 |
! ! |
|
2550 |
||
2551 |
!SoundStream::DevAudio methodsFor:'queries'! |
|
2552 |
||
2553 |
supportedAudioFormats |
|
2554 |
"return a collection of supported audio formats. |
|
2555 |
possibly returned symbols are: |
|
5405 | 2556 |
U8 unsigned 8bit samples |
2557 |
S8 signed 8bit samples |
|
2558 |
U16 unsigned 16bit samples in native format |
|
2559 |
U16_LE unsigned 16bit big endian samples |
|
2560 |
U16_BE unsigned 16bit big endian samples |
|
2561 |
S16 signed 16bit little endian samples in native format |
|
2562 |
S16_LE signed 16bit little endian samples |
|
2563 |
S16_BE signed 16bit big endian samples |
|
2564 |
S24 signed 24bit little endian samples in native format |
|
2565 |
S24_LE signed 24bit little endian samples |
|
2566 |
S24_BE signed 24bit big endian samples |
|
2567 |
S32 signed 32bit little endian samples in native format |
|
2568 |
S32_LE signed 32bit little endian samples |
|
2569 |
S32_BE signed 32bit big endian samples |
|
2570 |
F32 float samples |
|
2571 |
MPEG audio mpeg encoded |
|
2572 |
MU_LAW u-law encoded 8bit samples |
|
2573 |
A_LAW a-law encoded 8bit samples |
|
2574 |
IMA_ADPCM adpcm encoded |
|
5402 | 2575 |
the set of returned symbols depends on the underlying sound hardware. |
2576 |
" |
|
2577 |
||
2578 |
|fd audioFormatMask |
|
2579 |
supportedFormats |
|
2580 |
supports_MU_LAW supports_A_LAW supports_MPEG |
|
2581 |
supports_IMA_ADPCM |
|
2582 |
supports_S8 supports_U8 |
|
2583 |
supports_S16_LE supports_S16_BE |
|
2584 |
supports_U16_LE supports_U16_BE |
|
2585 |
supports_U32_LE supports_U32_BE |
|
2586 |
supports_S24_LE supports_S24_BE |
|
2587 |
supports_S32_LE supports_S32_BE |
|
2588 |
supports_F32 |
|
2589 |
| |
|
2590 |
||
2591 |
fd := self fileDescriptorOrNil. |
|
2592 |
||
2593 |
%{ |
|
2594 |
#ifdef SUPPORT_DEV_AUDIO |
|
2595 |
supports_MU_LAW = true; |
|
2596 |
||
2597 |
if (fd != nil) { |
|
5405 | 2598 |
int f = __intVal(fd); |
2599 |
int __audioFormatMask = 0; |
|
5402 | 2600 |
|
2601 |
# if defined(SNDCTL_DSP_GETFMTS) |
|
5405 | 2602 |
if (ioctl(f, SNDCTL_DSP_GETFMTS, &__audioFormatMask) >= 0) { |
2603 |
audioFormatMask = __MKSMALLINT(__audioFormatMask); |
|
5402 | 2604 |
|
2605 |
# ifdef AFMT_MU_LAW |
|
5405 | 2606 |
supports_MU_LAW = (__audioFormatMask & AFMT_MU_LAW) ? true : false; |
5402 | 2607 |
# endif |
2608 |
# ifdef AFMT_A_LAW |
|
5405 | 2609 |
supports_A_LAW = (__audioFormatMask & AFMT_A_LAW) ? true : false; |
5402 | 2610 |
# endif |
2611 |
# ifdef AFMT_IMA_ADPCM |
|
5405 | 2612 |
supports_IMA_ADPCM = (__audioFormatMask & AFMT_IMA_ADPCM) ? true : false; |
5402 | 2613 |
# endif |
2614 |
# ifdef AFMT_U8 |
|
5405 | 2615 |
supports_U8 = (__audioFormatMask & AFMT_U8) ? true : false; |
5402 | 2616 |
# endif |
2617 |
# ifdef AFMT_S16_LE |
|
5405 | 2618 |
supports_S16_LE = (__audioFormatMask & AFMT_S16_LE) ? true : false; |
5402 | 2619 |
# endif |
2620 |
# ifdef AFMT_S16_BE |
|
5405 | 2621 |
supports_S16_BE = (__audioFormatMask & AFMT_S16_BE) ? true : false; |
5402 | 2622 |
# endif |
2623 |
# ifdef AFMT_S8 |
|
5405 | 2624 |
supports_S8 = (__audioFormatMask & AFMT_S8) ? true : false; |
5402 | 2625 |
# endif |
2626 |
# ifdef AFMT_U16_LE |
|
5405 | 2627 |
supports_U16_LE = (__audioFormatMask & AFMT_U16_LE) ? true : false; |
5402 | 2628 |
# endif |
2629 |
# ifdef AFMT_U16_BE |
|
5405 | 2630 |
supports_U16_BE = (__audioFormatMask & AFMT_U16_BE) ? true : false; |
5402 | 2631 |
# endif |
2632 |
# ifdef AFMT_MPEG |
|
5405 | 2633 |
supports_MPEG = (__audioFormatMask & AFMT_MPEG) ? true : false; |
5402 | 2634 |
# endif |
5405 | 2635 |
} |
5402 | 2636 |
} |
2637 |
# endif // SNDCTL_DSP_GETFMTS |
|
2638 |
||
2639 |
# ifdef hpux |
|
2640 |
supports_A_LAW = true; |
|
2641 |
supports_S16_BE = true; |
|
2642 |
# endif |
|
2643 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2644 |
%}. |
|
2645 |
supportedFormats := IdentitySet new. |
|
2646 |
(supports_MU_LAW ? false) ifTrue:[ |
|
5405 | 2647 |
supportedFormats add:#'MU_LAW' |
5402 | 2648 |
]. |
2649 |
(supports_A_LAW ? false) ifTrue:[ |
|
5405 | 2650 |
supportedFormats add:#'A_LAW' |
5402 | 2651 |
]. |
2652 |
(supports_IMA_ADPCM ? false) ifTrue:[ |
|
5405 | 2653 |
supportedFormats add:#'IMA_ADPCM' |
5402 | 2654 |
]. |
2655 |
(supports_MPEG ? false) ifTrue:[ |
|
5405 | 2656 |
supportedFormats add:#'MPEG' |
5402 | 2657 |
]. |
2658 |
(supports_S8 ? false) ifTrue:[ |
|
5405 | 2659 |
supportedFormats add:#'S8' |
5402 | 2660 |
]. |
2661 |
(supports_U8 ? false) ifTrue:[ |
|
5405 | 2662 |
supportedFormats add:#'U8' |
5402 | 2663 |
]. |
2664 |
(supports_S16_LE ? false) ifTrue:[ |
|
5405 | 2665 |
supportedFormats add:#'S16_LE'. |
2666 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
2667 |
supportedFormats add:#'S16'. |
|
2668 |
] |
|
5402 | 2669 |
]. |
2670 |
(supports_S16_BE ? false) ifTrue:[ |
|
5405 | 2671 |
supportedFormats add:#'S16_BE'. |
2672 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
2673 |
supportedFormats add:#'S16'. |
|
2674 |
] |
|
5402 | 2675 |
]. |
2676 |
(supports_S24_LE ? false) ifTrue:[ |
|
5405 | 2677 |
supportedFormats add:#'S24_LE'. |
2678 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
2679 |
supportedFormats add:#'S24'. |
|
2680 |
] |
|
5402 | 2681 |
]. |
2682 |
(supports_S24_BE ? false) ifTrue:[ |
|
5405 | 2683 |
supportedFormats add:#'S24_BE'. |
2684 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
2685 |
supportedFormats add:#'S24'. |
|
2686 |
] |
|
5402 | 2687 |
]. |
2688 |
(supports_S32_LE ? false) ifTrue:[ |
|
5405 | 2689 |
supportedFormats add:#'S32_LE'. |
2690 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
2691 |
supportedFormats add:#'S32'. |
|
2692 |
] |
|
5402 | 2693 |
]. |
2694 |
(supports_S32_BE ? false) ifTrue:[ |
|
5405 | 2695 |
supportedFormats add:#'S32_BE'. |
2696 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
2697 |
supportedFormats add:#'S32'. |
|
2698 |
] |
|
5402 | 2699 |
]. |
2700 |
(supports_U16_LE ? false) ifTrue:[ |
|
5405 | 2701 |
supportedFormats add:#'U16_LE'. |
2702 |
UninterpretedBytes isBigEndian ifFalse:[ |
|
2703 |
supportedFormats add:#'U16'. |
|
2704 |
] |
|
5402 | 2705 |
]. |
2706 |
(supports_U16_BE ? false) ifTrue:[ |
|
5405 | 2707 |
supportedFormats add:#'U16_BE'. |
2708 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
2709 |
supportedFormats add:#'U16'. |
|
2710 |
] |
|
5402 | 2711 |
]. |
2712 |
(supports_F32 ? false) ifTrue:[ |
|
5405 | 2713 |
supportedFormats add:#'F32' |
5402 | 2714 |
]. |
2715 |
^ supportedFormats. |
|
2716 |
||
2717 |
" |
|
2718 |
|s formats| |
|
2719 |
||
2720 |
s := self writing. |
|
2721 |
formats := s supportedAudioFormats. |
|
2722 |
s close. |
|
2723 |
formats |
|
2724 |
" |
|
2725 |
! ! |
|
2726 |
||
2727 |
!SoundStream::DevAudio methodsFor:'reading'! |
|
2728 |
||
2729 |
nextBytes:count into:anObject startingAt:start |
|
2730 |
"read the next count bytes into an object and return the number of |
|
2731 |
bytes read or nil on error. |
|
2732 |
Use with ByteArrays only." |
|
2733 |
||
2734 |
%{ |
|
2735 |
#ifdef SUPPORT_DEV_AUDIO |
|
2736 |
/* |
|
2737 |
* redefined to work around a bug in the linux sound driver; |
|
2738 |
* if a read is interrupted (EINTR), it is not defined, how many |
|
2739 |
* bytes have been read from the device. |
|
2740 |
* |
|
2741 |
* As a workaround, disable signals here to prevent being interrupted. |
|
2742 |
*/ |
|
2743 |
int cnt, offs, objSize, n; |
|
2744 |
char *cp; |
|
2745 |
OBJ fp; |
|
2746 |
FILE *f; |
|
2747 |
int fd; |
|
2748 |
||
2749 |
if ((fp = __INST(handle)) != nil) { |
|
5405 | 2750 |
f = __FILEVal(fp); |
2751 |
if (__INST(mode) != @symbol(writeonly)) { |
|
2752 |
if (__bothSmallInteger(count, start)) { |
|
2753 |
cnt = __intVal(count); |
|
2754 |
offs = __intVal(start) - 1; |
|
2755 |
||
2756 |
objSize = _Size(anObject) - OHDR_SIZE; |
|
2757 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) { |
|
2758 |
do { |
|
2759 |
cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs; |
|
2760 |
||
2761 |
n = cnt; |
|
5402 | 2762 |
# ifdef LINUX |
5405 | 2763 |
sigsetmask(~0); |
5402 | 2764 |
# endif |
5405 | 2765 |
if (__INST(buffered) == true) { |
2766 |
n = fread(cp, 1, n, f); |
|
2767 |
} else { |
|
2768 |
fd = fileno(f); |
|
2769 |
n = read(fd, cp, n); |
|
2770 |
} |
|
5402 | 2771 |
# ifdef LINUX |
5405 | 2772 |
sigsetmask(0); |
5402 | 2773 |
# endif |
5405 | 2774 |
__BEGIN_INTERRUPTABLE__ |
2775 |
__END_INTERRUPTABLE__ |
|
2776 |
// console_fprintf(stderr, "SoundStream: read %d bytes\n", n); |
|
2777 |
if (n > 0) { |
|
2778 |
offs += n; |
|
2779 |
cnt -= n; |
|
2780 |
} else { |
|
2781 |
if (n < 0) { |
|
2782 |
console_fprintf(stderr, "read error: %d\n", __threadErrno); |
|
2783 |
RETURN (count); |
|
2784 |
} |
|
2785 |
} |
|
2786 |
} while (cnt); |
|
2787 |
} |
|
2788 |
RETURN (count); |
|
2789 |
} |
|
2790 |
} |
|
5402 | 2791 |
} |
2792 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2793 |
||
2794 |
%}. |
|
2795 |
self errorUnsupportedOperation |
|
2796 |
! ! |
|
2797 |
||
2798 |
!SoundStream::DevAudio methodsFor:'writing'! |
|
2799 |
||
2800 |
nextPutBytes:count from:anObject startingAt:start |
|
2801 |
"write count bytes from an object starting at index start. |
|
2802 |
return the number of bytes written or nil on error. |
|
2803 |
Redefined, since IRIS audio library cannot be used with stdio. |
|
2804 |
(at least I don't know). Use with ByteArrays only." |
|
2805 |
||
2806 |
%{ |
|
2807 |
#ifdef SUPPORT_DEV_AUDIO |
|
2808 |
/* |
|
2809 |
* redefine to work around a bug in the linux sound driver; |
|
2810 |
* if a write is interrupted (EINTR), it is not defined, how many |
|
2811 |
* bytes have been written to the device. |
|
2812 |
* I.e. a retry of the write may lead to ever-playing without ever |
|
2813 |
* finishing. |
|
2814 |
* |
|
2815 |
* As a workaround, disable signals here to prevent the write from |
|
2816 |
* being interrupted. |
|
2817 |
*/ |
|
2818 |
int cnt, offs, objSize, n; |
|
2819 |
char *cp; |
|
2820 |
OBJ fp; |
|
2821 |
FILE *f; |
|
2822 |
int fd; |
|
2823 |
||
2824 |
if ((fp = __INST(handle)) != nil) { |
|
5405 | 2825 |
f = __FILEVal(fp); |
2826 |
if (__INST(mode) != @symbol(readonly)) { |
|
2827 |
if (__bothSmallInteger(count, start)) { |
|
2828 |
cnt = __intVal(count); |
|
2829 |
offs = __intVal(start) - 1; |
|
2830 |
||
2831 |
objSize = _Size(anObject) - OHDR_SIZE; |
|
2832 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) { |
|
2833 |
do { |
|
2834 |
cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs; |
|
2835 |
||
2836 |
n = cnt; |
|
5402 | 2837 |
# ifdef LINUX |
5405 | 2838 |
sigsetmask(~0); |
5402 | 2839 |
# endif |
5405 | 2840 |
if (__INST(buffered) == true) { |
2841 |
n = fwrite(cp, 1, n, f); |
|
2842 |
} else { |
|
2843 |
fd = fileno(f); |
|
2844 |
n = write(fd, cp, n); |
|
2845 |
} |
|
5402 | 2846 |
# ifdef LINUX |
5405 | 2847 |
sigsetmask(0); |
5402 | 2848 |
# endif |
5405 | 2849 |
__BEGIN_INTERRUPTABLE__ |
2850 |
__END_INTERRUPTABLE__ |
|
2851 |
if (n > 0) { |
|
2852 |
offs += n; |
|
2853 |
cnt -= n; |
|
2854 |
} else { |
|
2855 |
if (n < 0) { |
|
2856 |
console_fprintf(stderr, "write error: %d\n", __threadErrno); |
|
2857 |
RETURN (count); |
|
2858 |
} |
|
2859 |
} |
|
2860 |
} while (cnt); |
|
2861 |
} |
|
2862 |
RETURN (count); |
|
2863 |
} |
|
2864 |
} |
|
5402 | 2865 |
} |
2866 |
#endif /* SUPPORT_DEV_AUDIO */ |
|
2867 |
||
2868 |
%}. |
|
2869 |
^ self errorUnsupportedOperation |
|
2870 |
! ! |
|
2871 |
||
2872 |
!SoundStream::IRISAudio class methodsFor:'documentation'! |
|
2873 |
||
2874 |
documentation |
|
2875 |
" |
|
2876 |
documentation to be added. |
|
2877 |
||
2878 |
class: |
|
5405 | 2879 |
<a short class summary here, describing what instances represent> |
2880 |
||
2881 |
responsibilities: |
|
2882 |
<describing what my main role is> |
|
2883 |
||
2884 |
collaborators: |
|
2885 |
<describing with whom and how I talk to> |
|
5402 | 2886 |
|
2887 |
API: |
|
5405 | 2888 |
<public api and main messages> |
2889 |
||
5402 | 2890 |
example: |
5405 | 2891 |
<a one-line examples on how to use - can also be in a separate example method> |
5402 | 2892 |
|
2893 |
implementation: |
|
5405 | 2894 |
<implementation points> |
5402 | 2895 |
|
2896 |
[author:] |
|
5405 | 2897 |
exept MBP |
5402 | 2898 |
|
2899 |
[instance variables:] |
|
2900 |
||
2901 |
[class variables:] |
|
2902 |
||
2903 |
[see also:] |
|
2904 |
||
2905 |
" |
|
2906 |
! ! |
|
2907 |
||
2908 |
!SoundStream::IRISAudio class methodsFor:'queries'! |
|
2909 |
||
2910 |
isSupported |
|
2911 |
%{ |
|
2912 |
#ifdef SUPPORT_IRIS_AUDIO |
|
2913 |
RETURN(true); |
|
2914 |
#endif |
|
2915 |
%}. |
|
2916 |
^ false. |
|
2917 |
! ! |
|
2918 |
||
2919 |
!SoundStream::IRISAudio methodsFor:'misc'! |
|
2920 |
||
2921 |
flush |
|
2922 |
"wait until all sound has been played" |
|
2923 |
||
2924 |
%{ |
|
2925 |
#ifdef SUPPORT_IRIS_AUDIO |
|
2926 |
OPJ port; |
|
2927 |
ALport p; |
|
2928 |
||
2929 |
if ((port = __INST(alPort)) != nil) { |
|
5405 | 2930 |
p = __ALportVal(port); |
2931 |
while (ALgetfilled(p) > 0) { |
|
2932 |
sginap(1); |
|
2933 |
} |
|
5402 | 2934 |
} |
2935 |
RETURN(self); |
|
2936 |
#endif /* SUPPORT_IRIS_AUDIO */ |
|
2937 |
%}. |
|
2938 |
||
2939 |
! ! |
|
2940 |
||
2941 |
!SoundStream::IRISAudio methodsFor:'open & close'! |
|
2942 |
||
2943 |
closeFile |
|
2944 |
"a stream has been collected - close the file" |
|
2945 |
||
2946 |
%{ |
|
2947 |
#ifdef SUPPORT_IRIS_AUDIO |
|
2948 |
OBJ port; |
|
2949 |
||
2950 |
if ((port = __INST(alPort)) != nil) { |
|
5405 | 2951 |
__INST(alPort) = nil; |
2952 |
ALcloseport(__ALportVal(port)); |
|
5402 | 2953 |
} |
2954 |
RETURN (self); |
|
2955 |
#endif /* SUPPORT_IRIS_AUDIO */ |
|
2956 |
%}. |
|
2957 |
||
2958 |
! |
|
2959 |
||
2960 |
openWithMode:aMode attributes:attributeSpec |
|
2961 |
|ok error errorStringOrNil| |
|
2962 |
||
2963 |
%{ |
|
2964 |
#ifdef SUPPORT_IRIS_AUDIO |
|
2965 |
ALconfig config; |
|
2966 |
ALport p; |
|
2967 |
long params[] = { |
|
5405 | 2968 |
AL_INPUT_SOURCE, AL_INPUT_MIC, |
2969 |
AL_INPUT_RATE, 8000, |
|
2970 |
AL_OUTPUT_RATE, 8000, |
|
5402 | 2971 |
}; |
2972 |
||
2973 |
ok = false. |
|
2974 |
config = ALnewconfig(); |
|
2975 |
if (__INST(numberOfChannels) == __MKSMALLINT(2)) |
|
5405 | 2976 |
ALsetchannels(config, AL_STEREO); |
5402 | 2977 |
else |
5405 | 2978 |
ALsetchannels(config, AL_MONO); |
5402 | 2979 |
if (__INST(bitsPerSample) == __MKSMALLINT(16)) |
5405 | 2980 |
ALsetwidth(config, AL_SAMPLE_16); |
5402 | 2981 |
else |
5405 | 2982 |
ALsetwidth(config, AL_SAMPLE_8); |
5402 | 2983 |
|
2984 |
if (__isSmallInteger(__INST(sampleRate))) |
|
5405 | 2985 |
params[3] = params[5] = __intVal(__INST(sampleRate)); |
5402 | 2986 |
|
2987 |
ALsetparams(AL_DEFAULT_DEVICE, params, 6); |
|
2988 |
p = ALopenport("smallchat", (char *)_stringVal(aMode), config); |
|
2989 |
if (p) { |
|
5405 | 2990 |
OBJ t; |
2991 |
||
2992 |
t = __MKEXTERNALADDRESS(p); __INST(alPort) = t; __STORE(self, t); |
|
5402 | 2993 |
} else { |
5405 | 2994 |
__INST(alPort) = nil; |
2995 |
goto out; |
|
5402 | 2996 |
} |
2997 |
__INST(binary) = true; |
|
2998 |
||
2999 |
ALfreeconfig(config); |
|
3000 |
/* |
|
3001 |
* get the parameters actually installed |
|
3002 |
*/ |
|
3003 |
config = ALgetconfig(p); |
|
3004 |
switch (ALgetchannels(config)) { |
|
5405 | 3005 |
default: |
3006 |
/* cannot happen */ |
|
3007 |
case AL_MONO: |
|
3008 |
__INST(numberOfChannels) = __MKSMALLINT(1); |
|
3009 |
break; |
|
3010 |
case AL_STEREO: |
|
3011 |
__INST(numberOfChannels) = __MKSMALLINT(2); |
|
3012 |
break; |
|
5402 | 3013 |
} |
3014 |
switch (ALgetwidth(config)) { |
|
5405 | 3015 |
default: |
3016 |
/* cannot happen */ |
|
3017 |
case AL_SAMPLE_8: |
|
3018 |
__INST(bitsPerSample) = __MKSMALLINT(8); |
|
3019 |
break; |
|
3020 |
case AL_SAMPLE_16: |
|
3021 |
__INST(bitsPerSample) = __MKSMALLINT(16); |
|
3022 |
break; |
|
3023 |
case AL_SAMPLE_24: |
|
3024 |
__INST(bitsPerSample) = __MKSMALLINT(24); |
|
3025 |
break; |
|
5402 | 3026 |
} |
3027 |
ALgetparams(AL_DEFAULT_DEVICE, params, 6); |
|
3028 |
__INST(sampleRate) = __MKSMALLINT(params[3]); |
|
3029 |
||
3030 |
ALfreeconfig(config); |
|
3031 |
ok = true; |
|
3032 |
out:; |
|
3033 |
#endif /* SUPPORT_IRIS_AUDIO */ |
|
3034 |
||
3035 |
%}. |
|
3036 |
ok == false ifTrue:[ |
|
5405 | 3037 |
lastErrorString := errorStringOrNil. |
3038 |
lastErrorNumber := error ? -1. |
|
3039 |
self openError:error. |
|
3040 |
"normally not reached" |
|
3041 |
^ nil. |
|
5402 | 3042 |
]. |
3043 |
self registerForFinalization. |
|
3044 |
! ! |
|
3045 |
||
3046 |
!SoundStream::IRISAudio methodsFor:'queries'! |
|
3047 |
||
3048 |
supportedAudioFormats |
|
3049 |
"return a collection of supported audio formats. |
|
3050 |
possibly returned symbols are: |
|
5405 | 3051 |
U8 unsigned 8bit samples |
3052 |
S8 signed 8bit samples |
|
3053 |
U16 unsigned 16bit samples in native format |
|
3054 |
U16_LE unsigned 16bit big endian samples |
|
3055 |
U16_BE unsigned 16bit big endian samples |
|
3056 |
S16 signed 16bit little endian samples in native format |
|
3057 |
S16_LE signed 16bit little endian samples |
|
3058 |
S16_BE signed 16bit big endian samples |
|
3059 |
S24 signed 24bit little endian samples in native format |
|
3060 |
S24_LE signed 24bit little endian samples |
|
3061 |
S24_BE signed 24bit big endian samples |
|
3062 |
S32 signed 32bit little endian samples in native format |
|
3063 |
S32_LE signed 32bit little endian samples |
|
3064 |
S32_BE signed 32bit big endian samples |
|
3065 |
F32 float samples |
|
3066 |
MPEG audio mpeg encoded |
|
3067 |
MU_LAW u-law encoded 8bit samples |
|
3068 |
A_LAW a-law encoded 8bit samples |
|
3069 |
IMA_ADPCM adpcm encoded |
|
5402 | 3070 |
the set of returned symbols depends on the underlying sound hardware. |
3071 |
" |
|
3072 |
||
3073 |
^ #( U8 U16_BE U16 ) |
|
3074 |
||
3075 |
" |
|
3076 |
|s formats| |
|
3077 |
||
3078 |
s := self writing. |
|
3079 |
formats := s supportedAudioFormats. |
|
3080 |
s close. |
|
3081 |
formats |
|
3082 |
" |
|
3083 |
! ! |
|
3084 |
||
3085 |
!SoundStream::IRISAudio methodsFor:'reading'! |
|
3086 |
||
3087 |
nextBytes:count into:anObject startingAt:start |
|
3088 |
"read the next count bytes into an object and return the number of |
|
3089 |
bytes read or nil on error. |
|
3090 |
Use with ByteArrays only." |
|
3091 |
||
3092 |
%{ |
|
3093 |
#ifdef SUPPORT_IRIS_AUDIO |
|
3094 |
{ |
|
3095 |
OBJ port; |
|
3096 |
ALport p; |
|
3097 |
int cnt, offs, nSamp; |
|
3098 |
int objSize; |
|
3099 |
char *cp; |
|
3100 |
||
3101 |
if ((port = __INST(alPort)) != nil) { |
|
5405 | 3102 |
if (__INST(mode) != _writeonly) { |
3103 |
if (__bothSmallInteger(count, start)) { |
|
3104 |
cnt = __intVal(count); |
|
3105 |
offs = __intVal(start) - 1; |
|
3106 |
p = __ALportVal(port); |
|
3107 |
objSize = _Size(anObject) - OHDR_SIZE; |
|
3108 |
if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) { |
|
3109 |
cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs; |
|
3110 |
if (__INST(bitsPerSample) == __MKSMALLINT(16)) |
|
3111 |
nSamp = cnt / 2; |
|
3112 |
else |
|
3113 |
nSamp = cnt; |
|
3114 |
ALreadsamps(p, cp, nSamp); |
|
3115 |
RETURN ( __MKSMALLINT(cnt) ); |
|
3116 |
} |
|
3117 |
} |
|
3118 |
} |
|
5402 | 3119 |
} |
3120 |
} |
|
3121 |
#endif /* SUPPORT_IRIS_AUDIO */ |
|
3122 |
||
3123 |
%}. |
|
3124 |
self errorUnsupportedOperation |
|
3125 |
! ! |
|
3126 |
||
3127 |
!SoundStream::IRISAudio methodsFor:'writing'! |
|
3128 |
||
3129 |
nextPutBytes:count from:anObject startingAt:start |
|
3130 |
"write count bytes from an object starting at index start. |
|
3131 |
return the number of bytes written or nil on error. |
|
3132 |
Redefined, since IRIS audio library cannot be used with stdio. |
|
3133 |
(at least I don't know). Use with ByteArrays only." |
|
3134 |
||
3135 |
%{ |
|
3136 |
#ifdef SUPPORT_IRIS_AUDIO |
|
3137 |
{ |
|
3138 |
OBJ port; |
|
3139 |
ALport p; |
|
3140 |
int cnt, offs, nSamp; |
|
3141 |
int objSize; |
|
3142 |
char *cp; |
|
3143 |
||
3144 |
if ((port = __INST(alPort)) != nil) { |
|
5405 | 3145 |
if (__INST(mode) != @symbol(readonly)) { |
3146 |
if (__bothSmallInteger(count, start)) { |
|
3147 |
cnt = __intVal(count); |
|
3148 |
offs = __intVal(start) - 1; |
|
3149 |
p = __ALportVal(port); |
|
3150 |
||
3151 |
/* |
|
3152 |
* compute number of samples |
|
3153 |
*/ |
|
3154 |
objSize = _Size(anObject) - OHDR_SIZE; |
|
3155 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) { |
|
3156 |
cp = (char *)__InstPtr(anObject) + OHDR_SIZE + offs; |
|
3157 |
if (__INST(bitsPerSample) == __MKSMALLINT(16)) |
|
3158 |
nSamp = cnt / 2; |
|
3159 |
else |
|
3160 |
nSamp = cnt; |
|
3161 |
ALwritesamps(p, cp, cnt); |
|
3162 |
RETURN ( count ); |
|
3163 |
} |
|
3164 |
} |
|
3165 |
} |
|
5402 | 3166 |
} |
3167 |
} |
|
3168 |
#endif /* SUPPORT_IRIS_AUDIO */ |
|
3169 |
%}. |
|
5405 | 3170 |
self errorUnsupportedOperation |
5402 | 3171 |
! ! |
3172 |
||
3173 |
!SoundStream::PortAudio class methodsFor:'default values'! |
|
3174 |
||
3175 |
defaultSampleRate |
|
3176 |
"minimum, supported by all audio systems" |
|
3177 |
||
3178 |
%{ |
|
3179 |
#ifdef SUPPORT_PORTAUDIO |
|
3180 |
RETURN (__MKSMALLINT(PORTAUDIO_DEFAULT_FREQ)); |
|
3181 |
#endif |
|
3182 |
%}. |
|
3183 |
||
3184 |
^ 8000 |
|
3185 |
! ! |
|
3186 |
||
3187 |
!SoundStream::PortAudio class methodsFor:'documentation'! |
|
3188 |
||
3189 |
documentation |
|
3190 |
" |
|
3191 |
documentation to be added. |
|
3192 |
||
3193 |
class: |
|
5405 | 3194 |
<a short class summary here, describing what instances represent> |
3195 |
||
3196 |
responsibilities: |
|
3197 |
<describing what my main role is> |
|
3198 |
||
3199 |
collaborators: |
|
3200 |
<describing with whom and how I talk to> |
|
5402 | 3201 |
|
3202 |
API: |
|
5405 | 3203 |
<public api and main messages> |
3204 |
||
5402 | 3205 |
example: |
5405 | 3206 |
<a one-line examples on how to use - can also be in a separate example method> |
5402 | 3207 |
|
3208 |
implementation: |
|
5405 | 3209 |
<implementation points> |
5402 | 3210 |
|
3211 |
[author:] |
|
5405 | 3212 |
exept MBP |
5402 | 3213 |
|
3214 |
[instance variables:] |
|
3215 |
||
3216 |
[class variables:] |
|
3217 |
||
3218 |
[see also:] |
|
3219 |
||
3220 |
" |
|
3221 |
! ! |
|
3222 |
||
3223 |
!SoundStream::PortAudio class methodsFor:'initialization'! |
|
3224 |
||
3225 |
primitiveInitializeDevice |
|
3226 |
|errorMessageOrNil| |
|
3227 |
||
3228 |
%{ |
|
3229 |
#ifdef SUPPORT_PORTAUDIO |
|
3230 |
PaError paErr = 0; |
|
3231 |
||
3232 |
if (DEBUGGING) { |
|
5405 | 3233 |
fprintf(stderr, "calling Pa_Initialize...\n"); |
5402 | 3234 |
} |
3235 |
if ((paErr = Pa_Initialize()) != paNoError ) { |
|
5405 | 3236 |
errorMessageOrNil = __MKSTRING(Pa_GetErrorText( paErr ) ); |
3237 |
fprintf(stderr, "SoundStream [error]: Pa_Initialize failed\n"); |
|
5402 | 3238 |
}; |
3239 |
#endif |
|
3240 |
%}. |
|
3241 |
errorMessageOrNil notNil ifTrue:[ |
|
5405 | 3242 |
self error:'failed to initialize audio device: ',errorMessageOrNil |
5402 | 3243 |
]. |
3244 |
||
3245 |
! ! |
|
3246 |
||
3247 |
!SoundStream::PortAudio class methodsFor:'queries'! |
|
3248 |
||
3249 |
isSupported |
|
3250 |
%{ |
|
3251 |
#ifdef SUPPORT_PORTAUDIO |
|
3252 |
RETURN(true); |
|
3253 |
#endif |
|
3254 |
%}. |
|
3255 |
^ false. |
|
3256 |
! ! |
|
3257 |
||
3258 |
!SoundStream::PortAudio methodsFor:'misc'! |
|
3259 |
||
3260 |
flush |
|
3261 |
"wait until all sound has been played" |
|
3262 |
||
3263 |
%{ |
|
3264 |
#ifdef SUPPORT_PORTAUDIO |
|
3265 |
OBJ str; |
|
3266 |
if ((str = __INST(handle1)) != nil) { |
|
5405 | 3267 |
struct paStreamData* paStreamData = (struct paStreamData*)__externalAddressVal(str); |
3268 |
||
3269 |
while (paStreamData->currentBuffer != NULL) { |
|
3270 |
struct timespec a; |
|
3271 |
a.tv_sec = 0; |
|
3272 |
a.tv_nsec = 1 * 1000000; // 1 milliseconds asNanoseconds -> 1000000 |
|
3273 |
nanosleep( &a, NULL ); |
|
3274 |
} |
|
5402 | 3275 |
} |
3276 |
#endif /* SUPPORT_PORTAUDIO */ |
|
3277 |
%}. |
|
3278 |
||
3279 |
! ! |
|
3280 |
||
3281 |
!SoundStream::PortAudio methodsFor:'open & close'! |
|
3282 |
||
3283 |
closeFile |
|
3284 |
"a stream has been collected - close the file" |
|
3285 |
||
3286 |
%{ |
|
3287 |
#ifdef SUPPORT_PORTAUDIO |
|
3288 |
OBJ str; |
|
3289 |
if ((str = __INST(handle1)) != nil) { |
|
5405 | 3290 |
struct paStreamData* paStreamData = (struct paStreamData*)__externalAddressVal(str); |
3291 |
struct paBuffer* buffer; |
|
3292 |
||
3293 |
if (DEBUGGING) { |
|
3294 |
fprintf(stderr, "pa close\n"); |
|
3295 |
} |
|
3296 |
__externalAddressVal(str) = NULL; |
|
3297 |
__INST(handle1) = nil; |
|
3298 |
// Pa_StopStream( paStreamData->stream ); |
|
3299 |
Pa_CloseStream( paStreamData->stream ); |
|
3300 |
||
3301 |
LOCK(paStreamData->lock); |
|
3302 |
||
3303 |
buffer = paStreamData->currentBuffer; |
|
3304 |
paStreamData->currentBuffer = NULL; |
|
3305 |
paStreamData->lastBuffer = NULL; |
|
3306 |
while (buffer != NULL) { |
|
3307 |
struct paBuffer* nextBuffer = buffer->nextBuffer; |
|
3308 |
free(buffer->sampleData); |
|
3309 |
free(buffer); |
|
3310 |
buffer = nextBuffer; |
|
3311 |
} |
|
3312 |
buffer = paStreamData->freeList; |
|
3313 |
paStreamData->freeList = NULL; |
|
3314 |
while (buffer != NULL) { |
|
3315 |
struct paBuffer* nextBuffer = buffer->nextBuffer; |
|
3316 |
free(buffer->sampleData); |
|
3317 |
free(buffer); |
|
3318 |
buffer = nextBuffer; |
|
3319 |
} |
|
3320 |
||
3321 |
UNLOCK(paStreamData->lock); |
|
3322 |
RELEASELOCK(paStreamData->lock); |
|
3323 |
free(paStreamData); |
|
5402 | 3324 |
} |
3325 |
RETURN (self); |
|
3326 |
#endif // SUPPORT_PORTAUDIO |
|
3327 |
%}. |
|
3328 |
! |
|
3329 |
||
3330 |
openWithMode:aMode attributes:attributeSpec |
|
3331 |
|ok error errorStringOrNil| |
|
3332 |
||
5409 | 3333 |
openMode := aMode. |
3334 |
openAttributes := attributeSpec. |
|
5402 | 3335 |
%{ |
3336 |
#ifdef SUPPORT_PORTAUDIO |
|
3337 |
static PaStreamParameters outputParameters; |
|
3338 |
PaStream *stream; |
|
3339 |
PaError paErr; |
|
3340 |
struct paStreamData* paStreamData; |
|
3341 |
int nChannels, sampleRate, bytesPerSample; |
|
3342 |
# define FRAMES_PER_BUFFER 128 |
|
3343 |
||
3344 |
ok = false; |
|
3345 |
||
3346 |
/* default output device */ |
|
3347 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
|
3348 |
if (outputParameters.device == paNoDevice) { |
|
5405 | 3349 |
fprintf(stderr, "SoundStream [warning]: No default output device.\n"); |
3350 |
errorStringOrNil = __MKSTRING("No default output device"); |
|
3351 |
goto out; |
|
5402 | 3352 |
} |
3353 |
||
3354 |
if (__isSmallInteger(__INST(numberOfChannels))) { |
|
5405 | 3355 |
nChannels = __intVal(__INST(numberOfChannels)); |
5402 | 3356 |
} else { |
5405 | 3357 |
nChannels = 1; |
5402 | 3358 |
} |
3359 |
outputParameters.channelCount = nChannels; |
|
3360 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
|
3361 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
|
3362 |
||
3363 |
// The standard formats paFloat32, paInt16, paInt32, paInt24, paInt8 |
|
3364 |
// and aUInt8 are usually implemented by all implementations. |
|
3365 |
// The floating point representation (paFloat32) uses +1.0 and -1.0 as the |
|
3366 |
// maximum and minimum respectively. |
|
3367 |
// paUInt8 is an unsigned 8 bit format where 128 is considered "ground" |
|
3368 |
||
3369 |
if (__INST(audioFormat) == @symbol(S16)) { |
|
5405 | 3370 |
outputParameters.sampleFormat = paInt16; |
3371 |
bytesPerSample = 2; |
|
5402 | 3372 |
} else if (__INST(audioFormat) == @symbol(S32)) { |
5405 | 3373 |
outputParameters.sampleFormat = paInt32; |
3374 |
bytesPerSample = 4; |
|
5402 | 3375 |
} else if (__INST(audioFormat) == @symbol(S24)) { |
5405 | 3376 |
outputParameters.sampleFormat = paInt24; |
3377 |
bytesPerSample = 3; |
|
5402 | 3378 |
} else if (__INST(audioFormat) == @symbol(S8)) { |
5405 | 3379 |
outputParameters.sampleFormat = paInt8; |
3380 |
bytesPerSample = 1; |
|
5402 | 3381 |
} else if (__INST(audioFormat) == @symbol(F32)) { |
5405 | 3382 |
outputParameters.sampleFormat = paFloat32; |
3383 |
bytesPerSample = 4; |
|
5402 | 3384 |
} else if (__INST(audioFormat) == @symbol(U8)) { |
5405 | 3385 |
outputParameters.sampleFormat = paUInt8; |
3386 |
bytesPerSample = 1; |
|
5402 | 3387 |
} else { |
5405 | 3388 |
fprintf(stderr, "SoundStream [warning]: unknown format - using U8\n"); |
3389 |
outputParameters.sampleFormat = paUInt8; |
|
3390 |
bytesPerSample = 1; |
|
5402 | 3391 |
} |
3392 |
||
3393 |
if (__isSmallInteger(__INST(sampleRate))) { |
|
5405 | 3394 |
sampleRate = __intVal(__INST(sampleRate)); |
5402 | 3395 |
} else { |
5405 | 3396 |
fprintf(stderr, "SoundStream [warning]: using default sampleRate 8000\n"); |
3397 |
sampleRate = 8000; |
|
5402 | 3398 |
} |
3399 |
||
3400 |
paStreamData = (struct paStreamData*)malloc(sizeof(struct paStreamData)); |
|
3401 |
if (paStreamData == NULL) { |
|
5405 | 3402 |
fprintf(stderr, "SoundStream [warning]: failed to allocate paStream\n"); |
3403 |
errorStringOrNil = __MKSTRING("failed to allocate paStream"); |
|
3404 |
goto out; |
|
5402 | 3405 |
} |
3406 |
||
3407 |
paErr = Pa_OpenStream( |
|
5405 | 3408 |
&stream, |
3409 |
NULL, /* no input */ |
|
3410 |
&outputParameters, |
|
3411 |
sampleRate, |
|
3412 |
FRAMES_PER_BUFFER, |
|
3413 |
paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
|
3414 |
paCallback, |
|
3415 |
paStreamData ); |
|
5402 | 3416 |
|
3417 |
if (paErr != paNoError) { |
|
5405 | 3418 |
fprintf(stderr, "SoundStream [warning]: openStream: %s\n", Pa_GetErrorText( paErr )); |
3419 |
free(paStreamData); |
|
3420 |
errorStringOrNil = __MKSTRING(Pa_GetErrorText( paErr )); |
|
3421 |
goto out; |
|
5402 | 3422 |
} |
3423 |
paStreamData->stream = stream; |
|
3424 |
INITLOCK(paStreamData->lock); |
|
3425 |
paStreamData->readOffset = 0; |
|
3426 |
paStreamData->bytesPerSample = bytesPerSample; |
|
3427 |
paStreamData->nChannels = nChannels; |
|
3428 |
||
3429 |
paStreamData->currentBuffer = NULL; |
|
3430 |
paStreamData->lastBuffer = NULL; |
|
3431 |
paStreamData->freeList = NULL; |
|
3432 |
paStreamData->hasFinished = 0; |
|
3433 |
||
3434 |
{ |
|
5405 | 3435 |
OBJ t; |
3436 |
t = __MKEXTERNALADDRESS(paStreamData); __INST(handle1) = t; __STORE(self, t); |
|
5402 | 3437 |
} |
3438 |
__INST(binary) = true; |
|
3439 |
||
3440 |
paErr = Pa_SetStreamFinishedCallback( stream, &paStreamFinished ); |
|
3441 |
if( paErr != paNoError ) { |
|
5405 | 3442 |
fprintf(stderr, "SoundStream [warning]: setFinishedCallback: %s\n", Pa_GetErrorText( paErr )); |
3443 |
free(paStreamData); |
|
3444 |
errorStringOrNil = __MKSTRING(Pa_GetErrorText( paErr )); |
|
3445 |
goto out; |
|
5402 | 3446 |
}; |
3447 |
||
3448 |
ok = true; |
|
3449 |
out:; |
|
3450 |
#endif /* SUPPORT_PORTAUDIO */ |
|
3451 |
||
3452 |
%}. |
|
3453 |
ok == false ifTrue:[ |
|
5405 | 3454 |
lastErrorString := errorStringOrNil. |
3455 |
lastErrorNumber := error ? -1. |
|
3456 |
self openError:error. |
|
3457 |
"normally not reached" |
|
3458 |
^ nil. |
|
5402 | 3459 |
]. |
3460 |
self registerForFinalization. |
|
3461 |
||
5407 | 3462 |
! |
3463 |
||
3464 |
reopenStream |
|
5408 | 3465 |
handle1 isNil ifTrue:[^ self]. |
5409 | 3466 |
self closeFile. |
3467 |
self openWithMode:openMode attributes:openAttributes |
|
5402 | 3468 |
! ! |
3469 |
||
3470 |
!SoundStream::PortAudio methodsFor:'private'! |
|
3471 |
||
3472 |
initialize |
|
3473 |
"initialize for least common mode" |
|
3474 |
||
3475 |
super initialize. |
|
3476 |
||
3477 |
(IsInitialized ? false) ifFalse:[ |
|
5405 | 3478 |
self class primitiveInitializeDevice |
5402 | 3479 |
]. |
3480 |
||
3481 |
"Created: 17.11.1995 / 17:28:14 / cg" |
|
5407 | 3482 |
! |
3483 |
||
3484 |
setAudioFormat:aSymbol |
|
3485 |
super setAudioFormat:aSymbol. |
|
3486 |
self reopenStream |
|
3487 |
! |
|
3488 |
||
3489 |
setChannels:numChannels |
|
3490 |
super setChannels:numChannels. |
|
3491 |
self reopenStream |
|
3492 |
! |
|
3493 |
||
3494 |
setFragmentSize:blockSize |
|
3495 |
super setFragmentSize:blockSize. |
|
3496 |
self reopenStream |
|
3497 |
! |
|
3498 |
||
3499 |
setSampleRate:hz |
|
3500 |
super setSampleRate:hz. |
|
3501 |
self reopenStream |
|
5402 | 3502 |
! ! |
3503 |
||
3504 |
!SoundStream::PortAudio methodsFor:'queries'! |
|
3505 |
||
3506 |
supportedAudioFormats |
|
3507 |
"return a collection of supported audio formats. |
|
3508 |
possibly returned symbols are: |
|
5409 | 3509 |
U8 unsigned 8bit samples |
3510 |
S8 signed 8bit samples |
|
3511 |
U16 unsigned 16bit samples in native format |
|
3512 |
U16_LE unsigned 16bit big endian samples |
|
3513 |
U16_BE unsigned 16bit big endian samples |
|
3514 |
S16 signed 16bit little endian samples in native format |
|
3515 |
S16_LE signed 16bit little endian samples |
|
3516 |
S16_BE signed 16bit big endian samples |
|
3517 |
S24 signed 24bit little endian samples in native format |
|
3518 |
S24_LE signed 24bit little endian samples |
|
3519 |
S24_BE signed 24bit big endian samples |
|
3520 |
S32 signed 32bit little endian samples in native format |
|
3521 |
S32_LE signed 32bit little endian samples |
|
3522 |
S32_BE signed 32bit big endian samples |
|
3523 |
F32 float samples |
|
3524 |
MPEG audio mpeg encoded |
|
3525 |
MU_LAW u-law encoded 8bit samples |
|
3526 |
A_LAW a-law encoded 8bit samples |
|
3527 |
IMA_ADPCM adpcm encoded |
|
5402 | 3528 |
the set of returned symbols depends on the underlying sound hardware. |
3529 |
" |
|
3530 |
||
5408 | 3531 |
^ #(U8 S8 S16_LE S16 S24_LE S24 S32_LE S32 F32) |
5402 | 3532 |
|
3533 |
" |
|
3534 |
|s formats| |
|
3535 |
||
3536 |
s := self writing. |
|
3537 |
formats := s supportedAudioFormats. |
|
3538 |
s close. |
|
3539 |
formats |
|
3540 |
" |
|
3541 |
! ! |
|
3542 |
||
3543 |
!SoundStream::PortAudio methodsFor:'reading'! |
|
3544 |
||
3545 |
nextBytes:count into:anObject startingAt:start |
|
3546 |
"read the next count bytes into an object and return the number of |
|
3547 |
bytes read or nil on error. |
|
3548 |
Use with ByteArrays only." |
|
3549 |
||
3550 |
self errorUnsupportedOperation |
|
3551 |
! ! |
|
3552 |
||
3553 |
!SoundStream::PortAudio methodsFor:'writing'! |
|
3554 |
||
3555 |
nextPutBytes:count from:anObject startingAt:start |
|
3556 |
"write count bytes from an object starting at index start. |
|
3557 |
return the number of bytes written or nil on error. |
|
3558 |
Redefined, since IRIS audio library cannot be used with stdio. |
|
3559 |
(at least I don't know). Use with ByteArrays only." |
|
3560 |
||
3561 |
%{ |
|
3562 |
#ifdef SUPPORT_PORTAUDIO |
|
3563 |
OBJ str; |
|
3564 |
||
3565 |
if ((str = __INST(handle1)) != nil) { |
|
5405 | 3566 |
if (__INST(mode) != @symbol(readonly)) { |
3567 |
if (__bothSmallInteger(count, start)) { |
|
3568 |
// allocate a buffer |
|
3569 |
struct paStreamData* paStreamData = (struct paStreamData*)__externalAddressVal(str); |
|
3570 |
int cnt = __intVal(count); |
|
3571 |
int offs = __intVal(start) - 1; |
|
3572 |
int objSize; |
|
3573 |
||
3574 |
objSize = _Size(anObject) - OHDR_SIZE; |
|
3575 |
if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) { |
|
3576 |
struct paBuffer* newBuffer; |
|
3577 |
struct paBuffer* toFree; |
|
3578 |
int mustStart = 0; |
|
3579 |
unsigned char* newSampleData; |
|
3580 |
||
3581 |
// try freeList |
|
3582 |
LOCK(paStreamData->lock); |
|
3583 |
if ((paStreamData->freeList != NULL) |
|
3584 |
&& (paStreamData->freeList->bufferSize == cnt)) { |
|
3585 |
// reuse |
|
3586 |
newBuffer = paStreamData->freeList; |
|
3587 |
paStreamData->freeList = newBuffer->nextBuffer; |
|
3588 |
newSampleData = newBuffer->sampleData; |
|
3589 |
toFree = NULL; |
|
3590 |
if (DEBUGGING) { |
|
3591 |
fprintf(stderr, "from free: %p (->%p)\n", newBuffer, newBuffer->sampleData); |
|
3592 |
} |
|
3593 |
} else { |
|
3594 |
// free them all |
|
3595 |
toFree = paStreamData->freeList; |
|
3596 |
paStreamData->freeList = NULL; |
|
3597 |
newBuffer = malloc(sizeof(struct paBuffer)); |
|
3598 |
newSampleData = malloc(cnt); |
|
3599 |
newBuffer->sampleData = newSampleData; |
|
3600 |
newBuffer->bufferSize = cnt; |
|
3601 |
if (DEBUGGING) { |
|
3602 |
fprintf(stderr, "alloc: %p (->%p)\n", newBuffer, newBuffer->sampleData); |
|
3603 |
} |
|
3604 |
} |
|
3605 |
||
3606 |
memcpy(newSampleData, (__ByteArrayInstPtr(anObject)->ba_element)+offs, cnt); |
|
3607 |
newBuffer->nextBuffer = NULL; |
|
3608 |
||
3609 |
if (paStreamData->lastBuffer == NULL) { |
|
3610 |
// start stream's buffer list |
|
3611 |
paStreamData->currentBuffer = newBuffer; |
|
3612 |
mustStart = 1; |
|
3613 |
} else { |
|
3614 |
// append to stream's buffer list |
|
3615 |
paStreamData->lastBuffer->nextBuffer = newBuffer; |
|
3616 |
} |
|
3617 |
paStreamData->lastBuffer = newBuffer; |
|
3618 |
paStreamData->hasFinished = 0; |
|
3619 |
UNLOCK(paStreamData->lock); |
|
3620 |
||
3621 |
if (mustStart) { |
|
3622 |
PaError paErr = Pa_StartStream( paStreamData->stream ); |
|
3623 |
if ( paErr != paNoError ) { |
|
3624 |
if (DEBUGGING) { |
|
3625 |
fprintf(stderr, "start error\n"); |
|
3626 |
} |
|
3627 |
RETURN (0); |
|
3628 |
}; |
|
3629 |
} |
|
3630 |
RETURN (count); |
|
3631 |
} |
|
3632 |
} |
|
3633 |
} |
|
5402 | 3634 |
} |
3635 |
#endif /* SUPPORT_PORTAUDIO */ |
|
3636 |
||
3637 |
%}. |
|
5405 | 3638 |
self errorUnsupportedOperation |
5402 | 3639 |
! ! |
3640 |
||
3641 |
!SoundStream::Win32DirectSound class methodsFor:'documentation'! |
|
3642 |
||
3643 |
documentation |
|
3644 |
" |
|
3645 |
documentation to be added. |
|
3646 |
||
3647 |
class: |
|
5405 | 3648 |
<a short class summary here, describing what instances represent> |
3649 |
||
3650 |
responsibilities: |
|
3651 |
<describing what my main role is> |
|
3652 |
||
3653 |
collaborators: |
|
3654 |
<describing with whom and how I talk to> |
|
5402 | 3655 |
|
3656 |
API: |
|
5405 | 3657 |
<public api and main messages> |
3658 |
||
5402 | 3659 |
example: |
5405 | 3660 |
<a one-line examples on how to use - can also be in a separate example method> |
5402 | 3661 |
|
3662 |
implementation: |
|
5405 | 3663 |
<implementation points> |
5402 | 3664 |
|
3665 |
[author:] |
|
5405 | 3666 |
exept MBP |
5402 | 3667 |
|
3668 |
[instance variables:] |
|
3669 |
||
3670 |
[class variables:] |
|
3671 |
||
3672 |
[see also:] |
|
3673 |
||
3674 |
" |
|
3675 |
! ! |
|
3676 |
||
3677 |
!SoundStream::Win32DirectSound class methodsFor:'queries'! |
|
3678 |
||
3679 |
isSupported |
|
3680 |
%{ |
|
3681 |
#ifdef SUPPORT_WIN32_DIRECTSOUND |
|
3682 |
RETURN(true); |
|
3683 |
#endif |
|
3684 |
%}. |
|
3685 |
^ false. |
|
3686 |
! ! |
|
3687 |
||
3688 |
!SoundStream::Win32DirectSound methodsFor:'misc'! |
|
3689 |
||
3690 |
flush |
|
3691 |
"wait until all sound has been played" |
|
3692 |
||
3693 |
"/ as yet unimplemented |
|
3694 |
^ self |
|
3695 |
! ! |
|
3696 |
||
3697 |
!SoundStream::Win32DirectSound methodsFor:'open & close'! |
|
3698 |
||
3699 |
closeFile |
|
3700 |
"a stream has been collected - close the file" |
|
3701 |
||
3702 |
%{ |
|
3703 |
#ifdef SUPPORT_WIN32_DIRECTSOUND |
|
3704 |
OBJ oDirectSound, oDSBuffer; |
|
3705 |
LPDIRECTSOUND t_pDirectSound; |
|
3706 |
LPDIRECTSOUNDBUFFER t_pDSBuffer; |
|
3707 |
||
3708 |
if ((oDSBuffer = __INST(pDSBuffer)) != nil) { |
|
5405 | 3709 |
__INST(pDSBuffer) = nil; |
3710 |
t_pDSBuffer = __DSBufferVal(oDSBuffer); |
|
3711 |
if (t_pDSBuffer) { |
|
3712 |
IDirectSoundBuffer_Stop(t_pDSBuffer); |
|
3713 |
IDirectSoundBuffer_Release(t_pDSBuffer); |
|
3714 |
} |
|
5402 | 3715 |
} |
3716 |
if ((oDirectSound = __INST(pDirectSound)) != nil) { |
|
5405 | 3717 |
__INST(pDirectSound) = nil; |
3718 |
t_pDirectSound = __DirectSoundVal(oDirectSound); |
|
3719 |
if (t_pDirectSound) { |
|
3720 |
IDirectSound_Release(t_pDirectSound); |
|
3721 |
} |
|
5402 | 3722 |
} |
3723 |
RETURN (self); |
|
3724 |
#endif /* SUPPORT_WIN32_DIRECTSOUND */ |
|
3725 |
%}. |
|
3726 |
||
3727 |
! |
|
3728 |
||
3729 |
openWithMode:aMode attributes:attributeSpec |
|
3730 |
|ok error errorStringOrNil| |
|
3731 |
||
3732 |
%{ |
|
3733 |
#ifdef SUPPORT_WIN32_DIRECTSOUND |
|
3734 |
HRESULT result; |
|
3735 |
LPDIRECTSOUND t_pDirectSound; |
|
3736 |
LPDIRECTSOUNDBUFFER t_pDSBuffer, t_pDSPrimeBuffer; |
|
3737 |
WAVEFORMATEX wfFormat; |
|
3738 |
DSBUFFERDESC dsbdDesc, primarydsbDesc; |
|
3739 |
BYTE *pDSBuffData; |
|
3740 |
int t_cbBufSize; |
|
3741 |
int dwDataLen; |
|
3742 |
||
3743 |
ok = false; |
|
3744 |
||
3745 |
/* Create the DS object */ |
|
3746 |
if ((result = DirectSoundCreate(NULL, &t_pDirectSound, NULL)) != DS_OK) { |
|
5405 | 3747 |
console_fprintf(stderr,"SoundStream: Cannot open default sound device!!\n"); |
3748 |
goto out; |
|
5402 | 3749 |
} |
3750 |
||
3751 |
/* Define the wave format structure */ |
|
3752 |
wfFormat.wFormatTag = WAVE_FORMAT_PCM; |
|
3753 |
wfFormat.nChannels = __intVal(__INST(numberOfChannels)); |
|
3754 |
wfFormat.nSamplesPerSec = __intVal(__INST(sampleRate)); |
|
3755 |
wfFormat.wBitsPerSample = __intVal(__INST(bitsPerSample)); |
|
3756 |
wfFormat.nBlockAlign = wfFormat.nChannels * wfFormat.wBitsPerSample / 8; |
|
3757 |
wfFormat.nAvgBytesPerSec = wfFormat.nSamplesPerSec * wfFormat.nBlockAlign; |
|
3758 |
wfFormat.cbSize = 0; |
|
3759 |
||
3760 |
# if 0 |
|
3761 |
/* Setup the primary DS buffer description */ |
|
3762 |
ZeroMemory(&primarydsbDesc, sizeof(DSBUFFERDESC)); |
|
3763 |
primarydsbDesc.dwSize = sizeof(DSBUFFERDESC); |
|
3764 |
primarydsbDesc.dwFlags = DSBCAPS_PRIMARYBUFFER; |
|
3765 |
primarydsbDesc.dwBufferBytes = 0; |
|
3766 |
primarydsbDesc.lpwfxFormat = NULL; |
|
3767 |
||
3768 |
/* Create the primary DS buffer */ |
|
3769 |
if ((result = IDirectSound_CreateSoundBuffer(t_pDirectSound, &primarydsbDesc, |
|
5405 | 3770 |
&t_pDSPrimeBuffer, NULL)) != DS_OK) { |
3771 |
console_fprintf(stderr,"SoundStream: Cannot get the primary DS buffer address!\n"); |
|
3772 |
IDirectSound_Release(t_pDirectSound); |
|
3773 |
goto out; |
|
5402 | 3774 |
} |
3775 |
||
3776 |
/* Set the primary DS buffer sound format. We have to do this because |
|
3777 |
the default primary buffer is 8-bit, 22kHz! */ |
|
3778 |
if ((result = IDirectSoundBuffer_SetFormat(t_pDSPrimeBuffer, &wfFormat)) != DS_OK) { |
|
5405 | 3779 |
console_fprintf(stderr,"SoundStream: Cannot set the primary DS buffer to proper sound format (%x) (%d)!\n", result, result); |
3780 |
IDirectSoundBuffer_Stop(t_pDSPrimeBuffer); |
|
3781 |
IDirectSoundBuffer_Release(t_pDSPrimeBuffer); |
|
3782 |
IDirectSound_Release(t_pDirectSound); |
|
3783 |
goto out; |
|
5402 | 3784 |
} |
3785 |
# endif /* 0 */ |
|
3786 |
||
3787 |
/* Setup the secondary DS buffer description */ |
|
3788 |
t_cbBufSize = RT_BUFFER_SIZE * sizeof(short) * NBUFS; |
|
3789 |
__INST(bufferSize) = __MKSMALLINT(t_cbBufSize); |
|
3790 |
||
3791 |
ZeroMemory(&dsbdDesc, sizeof(DSBUFFERDESC)); |
|
3792 |
dsbdDesc.dwSize = sizeof(DSBUFFERDESC); |
|
3793 |
dsbdDesc.dwFlags = DSBCAPS_GLOBALFOCUS; |
|
3794 |
dsbdDesc.dwBufferBytes = t_cbBufSize; |
|
3795 |
dsbdDesc.lpwfxFormat = &wfFormat; |
|
3796 |
||
3797 |
/* Create the secondary DS buffer */ |
|
3798 |
if ((result = IDirectSound_CreateSoundBuffer(t_pDirectSound, &dsbdDesc, &t_pDSBuffer, NULL)) != DS_OK) { |
|
5405 | 3799 |
console_fprintf(stderr,"SoundStream: couldn't create sound buffer!\n"); |
3800 |
IDirectSoundBuffer_Stop(t_pDSPrimeBuffer); |
|
3801 |
IDirectSoundBuffer_Release(t_pDSPrimeBuffer); |
|
3802 |
IDirectSound_Release(t_pDirectSound); |
|
3803 |
goto out; |
|
5402 | 3804 |
} |
3805 |
||
3806 |
/* Lock the DS buffer */ |
|
3807 |
if ((result = IDirectSoundBuffer_Lock(t_pDSBuffer, 0, t_cbBufSize, (LPLPVOID)&pDSBuffData, |
|
5405 | 3808 |
&dwDataLen, NULL, NULL, 0)) != DS_OK) { |
3809 |
console_fprintf(stderr,"SoundStream: couldn't lock sound buffer!\n"); |
|
3810 |
goto errorAndOut; |
|
5402 | 3811 |
} |
3812 |
||
3813 |
/* Zero the DS buffer */ |
|
3814 |
ZeroMemory(pDSBuffData, dwDataLen); |
|
3815 |
||
3816 |
/* Unlock the DS buffer */ |
|
3817 |
if ((result = IDirectSoundBuffer_Unlock(t_pDSBuffer, pDSBuffData, dwDataLen, NULL, 0)) != DS_OK) { |
|
5405 | 3818 |
console_fprintf(stderr,"SoundStream: couldn't unlock sound buffer!\n"); |
3819 |
goto errorAndOut; |
|
5402 | 3820 |
} |
3821 |
||
3822 |
__INST(bufferOffset) = __MKSMALLINT(0); // reset last write position to start of buffer |
|
3823 |
||
3824 |
/* Start the buffer playback */ |
|
3825 |
if ((result = IDirectSoundBuffer_Play(t_pDSBuffer, 0, 0, DSBPLAY_LOOPING) != DS_OK)) { |
|
5405 | 3826 |
console_fprintf(stderr,"SoundStream: couldn't play sound buffer!\n"); |
5402 | 3827 |
|
3828 |
errorAndOut: |
|
5405 | 3829 |
IDirectSoundBuffer_Stop(t_pDSBuffer); |
3830 |
IDirectSoundBuffer_Stop(t_pDSPrimeBuffer); |
|
3831 |
IDirectSoundBuffer_Release(t_pDSPrimeBuffer); |
|
3832 |
IDirectSound_Release(t_pDirectSound); |
|
3833 |
goto out; |
|
5402 | 3834 |
} |
3835 |
||
3836 |
{ |
|
5405 | 3837 |
OBJ t; |
3838 |
||
3839 |
t = __MKEXTERNALADDRESS(t_pDSBuffer); __INST(pDSBuffer) = t; __STORE(self, t); |
|
3840 |
t = __MKEXTERNALADDRESS(t_pDirectSound); __INST(pDirectSound) = t; __STORE(self, t); |
|
5402 | 3841 |
} |
3842 |
ok = true; |
|
3843 |
out:; |
|
3844 |
#endif /* SUPPORT_WIN32_DIRECTSOUND */ |
|
3845 |
||
3846 |
%}. |
|
3847 |
ok == false ifTrue:[ |
|
5405 | 3848 |
lastErrorString := errorStringOrNil. |
3849 |
lastErrorNumber := error ? -1. |
|
3850 |
self openError:error. |
|
3851 |
"normally not reached" |
|
3852 |
^ nil. |
|
5402 | 3853 |
]. |
3854 |
self registerForFinalization. |
|
3855 |
! ! |
|
3856 |
||
3857 |
!SoundStream::Win32DirectSound methodsFor:'queries'! |
|
3858 |
||
3859 |
supportedAudioFormats |
|
3860 |
"return a collection of supported audio formats. |
|
3861 |
possibly returned symbols are: |
|
5405 | 3862 |
U8 unsigned 8bit samples |
3863 |
S8 signed 8bit samples |
|
3864 |
U16 unsigned 16bit samples in native format |
|
3865 |
U16_LE unsigned 16bit big endian samples |
|
3866 |
U16_BE unsigned 16bit big endian samples |
|
3867 |
S16 signed 16bit little endian samples in native format |
|
3868 |
S16_LE signed 16bit little endian samples |
|
3869 |
S16_BE signed 16bit big endian samples |
|
3870 |
S24 signed 24bit little endian samples in native format |
|
3871 |
S24_LE signed 24bit little endian samples |
|
3872 |
S24_BE signed 24bit big endian samples |
|
3873 |
S32 signed 32bit little endian samples in native format |
|
3874 |
S32_LE signed 32bit little endian samples |
|
3875 |
S32_BE signed 32bit big endian samples |
|
3876 |
F32 float samples |
|
3877 |
MPEG audio mpeg encoded |
|
3878 |
MU_LAW u-law encoded 8bit samples |
|
3879 |
A_LAW a-law encoded 8bit samples |
|
3880 |
IMA_ADPCM adpcm encoded |
|
5402 | 3881 |
the set of returned symbols depends on the underlying sound hardware. |
3882 |
" |
|
3883 |
||
3884 |
^ #(S16_LE S16) |
|
3885 |
||
3886 |
" |
|
3887 |
|s formats| |
|
3888 |
||
3889 |
s := self writing. |
|
3890 |
formats := s supportedAudioFormats. |
|
3891 |
s close. |
|
3892 |
formats |
|
3893 |
" |
|
3894 |
! ! |
|
3895 |
||
3896 |
!SoundStream::Win32DirectSound methodsFor:'reading'! |
|
3897 |
||
3898 |
nextBytes:count into:anObject startingAt:start |
|
3899 |
"read the next count bytes into an object and return the number of |
|
3900 |
bytes read or nil on error. |
|
3901 |
Use with ByteArrays only." |
|
3902 |
||
3903 |
self errorUnsupportedOperation |
|
3904 |
! ! |
|
3905 |
||
3906 |
!SoundStream::Win32DirectSound methodsFor:'writing'! |
|
3907 |
||
3908 |
nextPutBytes:count from:anObject startingAt:start |
|
3909 |
"write count bytes from an object starting at index start. |
|
3910 |
return the number of bytes written or nil on error. |
|
3911 |
Redefined, since IRIS audio library cannot be used with stdio. |
|
3912 |
(at least I don't know). Use with ByteArrays only." |
|
3913 |
||
3914 |
%{ |
|
3915 |
#ifdef SUPPORT_WIN32_DIRECTSOUND |
|
3916 |
{ |
|
3917 |
HRESULT hr; |
|
3918 |
DWORD status; |
|
3919 |
LPVOID lpbuf1 = NULL; |
|
3920 |
LPVOID lpbuf2 = NULL; |
|
3921 |
DWORD dwsize1 = 0; |
|
3922 |
DWORD dwsize2 = 0; |
|
3923 |
DWORD playPos, safePos, endWrite; |
|
3924 |
DWORD millis; |
|
3925 |
OBJ oDirectSound, oDSBuffer; |
|
3926 |
LPDIRECTSOUND t_pDirectSound = (LPDIRECTSOUND)0; |
|
3927 |
LPDIRECTSOUNDBUFFER t_pDSBuffer = (LPDIRECTSOUNDBUFFER)0; |
|
3928 |
int t_cbBufOffset, t_cbBufSize; |
|
3929 |
short *buf; |
|
3930 |
int cnt, offs; |
|
3931 |
||
3932 |
if ((oDSBuffer = __INST(pDSBuffer)) != nil) { |
|
5405 | 3933 |
t_pDSBuffer = __DSBufferVal(oDSBuffer); |
5402 | 3934 |
} |
3935 |
if ((oDirectSound = __INST(pDirectSound)) != nil) { |
|
5405 | 3936 |
t_pDirectSound = __DirectSoundVal(oDirectSound); |
5402 | 3937 |
} |
3938 |
||
3939 |
if (!t_pDSBuffer || !t_pDirectSound) { |
|
5405 | 3940 |
console_fprintf(stderr, "SoundStream not open!\n"); |
3941 |
RETURN (0); |
|
5402 | 3942 |
} |
3943 |
t_cbBufOffset = __intVal(__INST(bufferOffset)); |
|
3944 |
t_cbBufSize = __intVal(__INST(bufferSize)); |
|
3945 |
||
3946 |
cnt = __intVal(count); |
|
3947 |
offs = __intVal(start) - 1; |
|
3948 |
buf = (short *)__InstPtr(anObject) + OHDR_SIZE + offs; |
|
3949 |
||
3950 |
// Should be playing, right? |
|
3951 |
hr = IDirectSoundBuffer_GetStatus(t_pDSBuffer, &status ); |
|
3952 |
if (!(status && DSBSTATUS_PLAYING)) { |
|
5405 | 3953 |
console_fprintf(stderr, "Buffer not playing!\n"); |
3954 |
RETURN (0); |
|
5402 | 3955 |
} |
3956 |
||
3957 |
// Sleep until we have enough room in buffer. |
|
3958 |
hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos ); |
|
3959 |
if( hr != DS_OK ) { |
|
5405 | 3960 |
console_fprintf(stderr, "Cannot get position!\n"); |
3961 |
RETURN (0); |
|
5402 | 3962 |
} |
3963 |
if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; |
|
3964 |
||
3965 |
endWrite = t_cbBufOffset + (cnt * sizeof(short)); |
|
3966 |
while ( playPos < endWrite ) { |
|
5405 | 3967 |
// Calculate number of milliseconds until we will have room, as |
3968 |
// time = distance * (milliseconds/second) / ((bytes/sample) * (samples/second)), |
|
3969 |
// rounded up. |
|
3970 |
millis = (DWORD) (1.0 + ((endWrite - playPos) * 1000.0) / ( sizeof(short) * __intVal(__INST(sampleRate)))); |
|
3971 |
||
3972 |
// Sleep for that long |
|
3973 |
Sleep( millis ); |
|
3974 |
||
3975 |
// Wake up, find out where we are now |
|
3976 |
hr = IDirectSoundBuffer_GetCurrentPosition(t_pDSBuffer, &playPos, &safePos ); |
|
3977 |
if( hr != DS_OK ) { |
|
3978 |
console_fprintf(stderr, "Cannot get position!\n"); |
|
3979 |
RETURN (0); |
|
3980 |
} |
|
3981 |
if( playPos < t_cbBufOffset ) playPos += t_cbBufSize; // unwrap offset |
|
5402 | 3982 |
} |
3983 |
||
3984 |
// Lock free space in the DS |
|
3985 |
hr = IDirectSoundBuffer_Lock(t_pDSBuffer, t_cbBufOffset, cnt * sizeof(short), &lpbuf1, &dwsize1, &lpbuf2, &dwsize2, 0); |
|
3986 |
if (hr == DS_OK) { |
|
5405 | 3987 |
// Copy the buffer into the DS |
3988 |
CopyMemory(lpbuf1, buf, dwsize1); |
|
3989 |
if(NULL != lpbuf2) CopyMemory(lpbuf2, buf+dwsize1, dwsize2); |
|
3990 |
||
3991 |
// Update our buffer offset and unlock sound buffer |
|
3992 |
t_cbBufOffset = (t_cbBufOffset + dwsize1 + dwsize2) % t_cbBufSize; |
|
3993 |
IDirectSoundBuffer_Unlock(t_pDSBuffer, lpbuf1, dwsize1, lpbuf2, dwsize2); |
|
5402 | 3994 |
} |
3995 |
__INST(buffferOffset) = __MKSMALLINT(t_cbBufOffset); |
|
3996 |
||
3997 |
RETURN (count); |
|
3998 |
} |
|
3999 |
#endif /* SUPPORT_WIN32_DIRECTSOUND */ |
|
4000 |
||
4001 |
%}. |
|
5405 | 4002 |
self errorUnsupportedOperation |
5402 | 4003 |
! ! |
4004 |
||
4005 |
!SoundStream::Win32WaveSound class methodsFor:'documentation'! |
|
4006 |
||
4007 |
documentation |
|
4008 |
" |
|
4009 |
documentation to be added. |
|
4010 |
||
4011 |
class: |
|
5405 | 4012 |
<a short class summary here, describing what instances represent> |
4013 |
||
4014 |
responsibilities: |
|
4015 |
<describing what my main role is> |
|
4016 |
||
4017 |
collaborators: |
|
4018 |
<describing with whom and how I talk to> |
|
5402 | 4019 |
|
4020 |
API: |
|
5405 | 4021 |
<public api and main messages> |
4022 |
||
5402 | 4023 |
example: |
5405 | 4024 |
<a one-line examples on how to use - can also be in a separate example method> |
5402 | 4025 |
|
4026 |
implementation: |
|
5405 | 4027 |
<implementation points> |
5402 | 4028 |
|
4029 |
[author:] |
|
5405 | 4030 |
exept MBP |
5402 | 4031 |
|
4032 |
[instance variables:] |
|
4033 |
||
4034 |
[class variables:] |
|
4035 |
||
4036 |
[see also:] |
|
4037 |
||
4038 |
" |
|
4039 |
! ! |
|
4040 |
||
4041 |
!SoundStream::Win32WaveSound class methodsFor:'queries'! |
|
4042 |
||
4043 |
isSupported |
|
4044 |
%{ |
|
4045 |
#ifdef SUPPORT_WIN32_WAVESOUND |
|
4046 |
RETURN(true); |
|
4047 |
#endif |
|
4048 |
%}. |
|
4049 |
^ false. |
|
4050 |
! ! |
|
4051 |
||
4052 |
!SoundStream::Win32WaveSound methodsFor:'misc'! |
|
4053 |
||
4054 |
flush |
|
4055 |
"wait until all sound has been played" |
|
4056 |
||
4057 |
"/ as yet unimplemented |
|
4058 |
^ self |
|
4059 |
! ! |
|
4060 |
||
4061 |
!SoundStream::Win32WaveSound methodsFor:'open & close'! |
|
4062 |
||
4063 |
closeFile |
|
4064 |
"a stream has been collected - close the file" |
|
4065 |
||
4066 |
%{ |
|
4067 |
#ifdef SUPPORT_WIN32_WAVESOUND |
|
4068 |
struct buf *bp, *next; |
|
4069 |
int r; |
|
4070 |
HWAVEOUT t_waveHandle; |
|
4071 |
OBJ oWaveHandle; |
|
4072 |
||
4073 |
if ((oWaveHandle = __INST(waveHandle)) != nil) { |
|
5405 | 4074 |
t_waveHandle = __WaveHandleVal(oWaveHandle); |
5402 | 4075 |
|
4076 |
# ifdef NO_WAIT_IN_CLOSE |
|
5405 | 4077 |
/* Force cancellation of any pending buffers */ |
4078 |
(void)waveOutReset(t_waveHandle); |
|
5402 | 4079 |
# endif |
4080 |
||
5405 | 4081 |
/* Wait until all pending buffers have been freed */ |
4082 |
while (free_buffers < total_buffers) { |
|
4083 |
WaitForSingleObject(free_buffer_event, INFINITE); |
|
4084 |
} |
|
5402 | 4085 |
|
4086 |
# ifndef NO_WAIT_IN_CLOSE |
|
5405 | 4087 |
/* Force cancellation of any pending buffers */ |
4088 |
(void)waveOutReset(t_waveHandle); |
|
5402 | 4089 |
# endif |
4090 |
||
5405 | 4091 |
/* Close the device */ |
4092 |
if ((r = waveOutClose(t_waveHandle)) != 0) { |
|
4093 |
console_printf("waveOutClose\n"); |
|
4094 |
RETURN(self); |
|
4095 |
} |
|
4096 |
||
4097 |
EnterCriticalSection(&free_list_lock); |
|
4098 |
||
4099 |
/* Free allocated buffers */ |
|
4100 |
for (bp = free_list; bp != NULL; bp = next) { |
|
4101 |
next = bp->next; |
|
4102 |
(void)free(bp); |
|
4103 |
} |
|
4104 |
free_list = NULL; |
|
4105 |
||
4106 |
LeaveCriticalSection(&free_list_lock); |
|
4107 |
||
4108 |
__INST(waveHandle) = nil; |
|
5402 | 4109 |
} |
4110 |
RETURN (self); |
|
4111 |
#endif /* SUPPORT_WIN32_WAVESOUND */ |
|
4112 |
%}. |
|
4113 |
||
4114 |
! |
|
4115 |
||
4116 |
openWithMode:aMode attributes:attributeSpec |
|
4117 |
|ok error errorStringOrNil| |
|
4118 |
||
4119 |
%{ |
|
4120 |
#ifdef SUPPORT_WIN32_WAVESOUND |
|
4121 |
PCMWAVEFORMAT waveFormat; |
|
4122 |
int r; |
|
4123 |
HWAVEOUT t_waveHandle; |
|
4124 |
OBJ oWaveHandle; |
|
4125 |
||
4126 |
ok = false; |
|
4127 |
||
4128 |
if ((oWaveHandle = __INST(waveHandle)) != nil) { |
|
5405 | 4129 |
goto out; |
5402 | 4130 |
} |
4131 |
||
4132 |
waveFormat.wf.wFormatTag = WAVE_FORMAT_PCM; |
|
4133 |
waveFormat.wf.nChannels = __intVal(__INST(numberOfChannels)); |
|
4134 |
waveFormat.wf.nSamplesPerSec = __intVal(__INST(sampleRate)); |
|
4135 |
waveFormat.wBitsPerSample = __intVal(__INST(bitsPerSample)); |
|
4136 |
waveFormat.wf.nBlockAlign = waveFormat.wf.nChannels * waveFormat.wBitsPerSample / 8; |
|
4137 |
waveFormat.wf.nAvgBytesPerSec = waveFormat.wf.nSamplesPerSec * waveFormat.wf.nBlockAlign; |
|
4138 |
||
4139 |
r = waveOutOpen(&t_waveHandle, |
|
5405 | 4140 |
WAVE_MAPPER, |
4141 |
(WAVEFORMAT *)&waveFormat, |
|
4142 |
(DWORD_PTR)waveCallBack, |
|
4143 |
(DWORD_PTR)0, |
|
4144 |
CALLBACK_FUNCTION); |
|
5402 | 4145 |
if (r != 0) { |
5405 | 4146 |
console_printf("waveOutOpen\n"); |
4147 |
goto out; |
|
5402 | 4148 |
} |
4149 |
||
4150 |
(void)waveOutReset(t_waveHandle); |
|
4151 |
||
4152 |
free_list = NULL; |
|
4153 |
InitializeCriticalSection(&free_list_lock); |
|
4154 |
free_buffers = 0; |
|
4155 |
free_buffer_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
|
4156 |
total_buffers = 0; |
|
4157 |
||
4158 |
{ |
|
5405 | 4159 |
OBJ t; |
4160 |
||
4161 |
t = __MKEXTERNALADDRESS(t_waveHandle); __INST(waveHandle) = t; __STORE(self, t); |
|
5402 | 4162 |
} |
4163 |
||
4164 |
# if 0 |
|
4165 |
/* |
|
4166 |
* HACK: If we immediately start writing valid audio data to the device |
|
4167 |
* then the sound is choppy in the beginning. Writing a null packet to |
|
4168 |
* to the device first seems to fix this problem although I have no idea |
|
4169 |
* why - DAC |
|
4170 |
*/ |
|
4171 |
{ |
|
5405 | 4172 |
char null[DATALEN]; |
4173 |
int i; |
|
4174 |
for (i = 0; i < DATALEN; i++) { |
|
4175 |
null[i] = 127; |
|
4176 |
} |
|
4177 |
audioWrite(null, DATALEN); |
|
5402 | 4178 |
} |
4179 |
# endif /* 0 */ |
|
4180 |
ok = true; |
|
4181 |
||
4182 |
out:; |
|
4183 |
||
4184 |
#endif /* !SUPPORT_WIN32_DIRECTSOUND */ |
|
4185 |
%}. |
|
4186 |
ok == false ifTrue:[ |
|
5405 | 4187 |
lastErrorString := errorStringOrNil. |
4188 |
lastErrorNumber := error ? -1. |
|
4189 |
self openError:error. |
|
4190 |
"normally not reached" |
|
4191 |
^ nil. |
|
5402 | 4192 |
]. |
4193 |
self registerForFinalization. |
|
4194 |
! ! |
|
4195 |
||
4196 |
!SoundStream::Win32WaveSound methodsFor:'queries'! |
|
4197 |
||
4198 |
supportedAudioFormats |
|
4199 |
"return a collection of supported audio formats. |
|
4200 |
possibly returned symbols are: |
|
5405 | 4201 |
U8 unsigned 8bit samples |
4202 |
S8 signed 8bit samples |
|
4203 |
U16 unsigned 16bit samples in native format |
|
4204 |
U16_LE unsigned 16bit big endian samples |
|
4205 |
U16_BE unsigned 16bit big endian samples |
|
4206 |
S16 signed 16bit little endian samples in native format |
|
4207 |
S16_LE signed 16bit little endian samples |
|
4208 |
S16_BE signed 16bit big endian samples |
|
4209 |
S24 signed 24bit little endian samples in native format |
|
4210 |
S24_LE signed 24bit little endian samples |
|
4211 |
S24_BE signed 24bit big endian samples |
|
4212 |
S32 signed 32bit little endian samples in native format |
|
4213 |
S32_LE signed 32bit little endian samples |
|
4214 |
S32_BE signed 32bit big endian samples |
|
4215 |
F32 float samples |
|
4216 |
MPEG audio mpeg encoded |
|
4217 |
MU_LAW u-law encoded 8bit samples |
|
4218 |
A_LAW a-law encoded 8bit samples |
|
4219 |
IMA_ADPCM adpcm encoded |
|
5402 | 4220 |
the set of returned symbols depends on the underlying sound hardware. |
4221 |
" |
|
4222 |
||
4223 |
^ #(S16_LE S16) |
|
4224 |
||
4225 |
" |
|
4226 |
|s formats| |
|
4227 |
||
4228 |
s := self writing. |
|
4229 |
formats := s supportedAudioFormats. |
|
4230 |
s close. |
|
4231 |
formats |
|
4232 |
" |
|
4233 |
! ! |
|
4234 |
||
4235 |
!SoundStream::Win32WaveSound methodsFor:'reading'! |
|
4236 |
||
4237 |
nextBytes:count into:anObject startingAt:start |
|
4238 |
"read the next count bytes into an object and return the number of |
|
4239 |
bytes read or nil on error. |
|
4240 |
Use with ByteArrays only." |
|
4241 |
||
4242 |
self errorUnsupportedOperation |
|
4243 |
! ! |
|
4244 |
||
4245 |
!SoundStream::Win32WaveSound methodsFor:'writing'! |
|
4246 |
||
4247 |
nextPutBytes:count from:anObject startingAt:start |
|
4248 |
"write count bytes from an object starting at index start. |
|
4249 |
return the number of bytes written or nil on error. |
|
4250 |
Redefined, since IRIS audio library cannot be used with stdio. |
|
4251 |
(at least I don't know). Use with ByteArrays only." |
|
4252 |
||
4253 |
%{ |
|
4254 |
#ifdef SUPPORT_WIN32_WAVESOUND |
|
4255 |
{ |
|
4256 |
struct buf *bp; |
|
4257 |
int len, i, r; |
|
4258 |
int dataLen, offs; |
|
4259 |
short *buf; |
|
4260 |
||
4261 |
HWAVEOUT t_waveHandle; |
|
4262 |
OBJ oWaveHandle; |
|
4263 |
||
4264 |
if ((oWaveHandle = __INST(waveHandle)) == nil) { |
|
5405 | 4265 |
RETURN(0); |
5402 | 4266 |
} |
4267 |
t_waveHandle = __WaveHandleVal(oWaveHandle); |
|
4268 |
||
4269 |
dataLen = __intVal(count); |
|
4270 |
offs = __intVal(start) - 1; |
|
4271 |
buf = (short *)__InstPtr(anObject) + OHDR_SIZE + offs; |
|
4272 |
||
4273 |
while (dataLen > 0) { |
|
5405 | 4274 |
if (free_list == NULL && total_buffers < MAXBUF) { |
4275 |
/* Expand available buffer space */ |
|
4276 |
bp = (struct buf *)malloc(sizeof(struct buf)); |
|
4277 |
total_buffers++; |
|
4278 |
} else { |
|
4279 |
if (free_list == NULL) { |
|
4280 |
/* We must wait for a free buffer */ |
|
4281 |
while (free_list == NULL) { |
|
4282 |
WaitForSingleObject(free_buffer_event, INFINITE); |
|
4283 |
} |
|
4284 |
} |
|
4285 |
EnterCriticalSection(&free_list_lock); |
|
4286 |
bp = free_list; |
|
4287 |
free_list = free_list->next; |
|
4288 |
--free_buffers; |
|
4289 |
LeaveCriticalSection(&free_list_lock); |
|
4290 |
r = waveOutUnprepareHeader(t_waveHandle, &bp->hdr, sizeof(WAVEHDR)); |
|
4291 |
if (r != 0) { |
|
4292 |
console_printf("waveOutUnprepareHeader\n"); |
|
4293 |
RETURN(self); |
|
4294 |
} |
|
4295 |
} |
|
4296 |
len = min(dataLen, DATALEN); |
|
4297 |
bp->hdr.lpData = (char *)bp->data; |
|
4298 |
bp->hdr.dwBufferLength = len; |
|
4299 |
bp->hdr.dwBytesRecorded = len; |
|
4300 |
bp->hdr.dwUser = (INT)(bp); |
|
4301 |
bp->hdr.dwFlags = 0; |
|
4302 |
bp->hdr.dwLoops = 0; |
|
4303 |
r = waveOutPrepareHeader(t_waveHandle, &bp->hdr, sizeof(WAVEHDR)); |
|
4304 |
if (r != 0) { |
|
4305 |
console_printf("waveOutPrepareHeader\n"); |
|
4306 |
RETURN(self); |
|
4307 |
} |
|
4308 |
for (i = 0; i < len; i++) { |
|
4309 |
bp->data[i] = buf[i]; |
|
4310 |
} |
|
4311 |
r = waveOutWrite(t_waveHandle, &bp->hdr, sizeof(WAVEHDR)); |
|
4312 |
if (r != 0) { |
|
4313 |
console_printf("waveOutWrite\n"); |
|
4314 |
RETURN(self); |
|
4315 |
} |
|
4316 |
buf += len; |
|
4317 |
dataLen -= len; |
|
5402 | 4318 |
} |
4319 |
RETURN (count); |
|
4320 |
} |
|
4321 |
#endif /* SUPPORT_WIN32_WAVESOUND */ |
|
4322 |
%}. |
|
5405 | 4323 |
self errorUnsupportedOperation |
5402 | 4324 |
! ! |
4325 |
||
152 | 4326 |
!SoundStream class methodsFor:'documentation'! |
4327 |
||
4328 |
version |
|
3622 | 4329 |
^ '$Header$' |
2418
4ea40741b2b5
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
4330 |
! |
4ea40741b2b5
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
4331 |
|
4ea40741b2b5
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
2321
diff
changeset
|
4332 |
version_CVS |
3622 | 4333 |
^ '$Header$' |
918 | 4334 |
! ! |
1151
5963256af171
do not depend on TimeStamp - TimeStamp to return seconds-integer
Claus Gittinger <cg@exept.de>
parents:
934
diff
changeset
|
4335 |
|
3154
23ac6f3e69b6
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3000
diff
changeset
|
4336 |
|
596 | 4337 |
SoundStream initialize! |