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