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