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