diff -r fcff6c911d4a -r 8d0920f791f1 SoundStream.st --- a/SoundStream.st Mon Dec 15 12:04:11 1997 +0100 +++ b/SoundStream.st Mon Dec 15 13:20:26 1997 +0100 @@ -88,12 +88,6 @@ ] ! ! -!SoundStream class methodsFor:'Signal constants'! - -unsupportedOperationSignal - ^ UnsupportedOperationSignal -! ! - !SoundStream class methodsFor:'instance creation'! reading @@ -153,8 +147,103 @@ "Modified: / 12.12.1997 / 16:51:49 / cg" ! ! +!SoundStream class methodsFor:'Signal constants'! + +unsupportedOperationSignal + ^ UnsupportedOperationSignal +! ! + !SoundStream class methodsFor:'conversion helpers'! +linear16ToUlaw:a16bitSignedValue + "given a 16it signed value, encode into uLaw byte" + + |absVal sign exp mantissa byte| + +%{ + /* + * so heavily used when playing sounds ... + * made it a primitive. + */ + if (__isSmallInteger(a16bitSignedValue)) { + int __sign = 0; + int __absVal = __intVal(a16bitSignedValue); + int __exp, __mantissa, __byte; + static char __uLawExp[] = + { + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 + }; + + if (__absVal < 0) { + if (__absVal <= -32256) { + RETURN (__MKSMALLINT(0)); + } + __absVal = -__absVal; + __sign = 0x80; + } else { + if (__absVal >= 32256) { + RETURN (__MKSMALLINT(128)); + } + } + + __exp = __uLawExp[__absVal >> 8]; + __mantissa = (__absVal >> (__exp+3)) & 0xF; + __byte = ~(__sign | (__exp<<4) | __mantissa) & 0xFF; + RETURN (__MKSMALLINT(__byte)); + } +%}. + "/ + "/ fallback for non-integral argument + "/ + sign := 0. + (absVal := a16bitSignedValue asInteger) < 0 ifTrue:[ + (absVal <= -32256) ifTrue:[ + ^ 0 + ]. + absVal := absVal negated. + sign := 16r80 + ] ifFalse:[ + absVal >= 32256 ifTrue:[ + ^ 128 + ] + ]. + + exp := #[ + 0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + ] at:(absVal bitShift:-1)+1. + mantissa := (absVal bitShift:(exp+3) negated) bitAnd:16r0F. + byte := ((sign bitOr:(exp bitShift:4)) bitOr:mantissa) bitInvert bitAnd:16rFF. + ^ byte + + " + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:0) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:32256) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-32256) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:32767) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-32767) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:100) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-100) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:104) + SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-104) + " + + "Modified: / 9.12.1997 / 16:46:24 / cg" +! + uLawToLinear16:uLawValue "given a uLaw byte, return the decoded signed 16bit value. Currently unused - but will be" @@ -419,109 +508,20 @@ ) at:(uLawValue + 1) "Modified: / 9.12.1997 / 16:34:17 / cg" -! - -linear16ToUlaw:a16bitSignedValue - "given a 16it signed value, encode into uLaw byte" - - |absVal sign exp mantissa byte| - -%{ - /* - * so heavily used when playing sounds ... - * made it a primitive. - */ - if (__isSmallInteger(a16bitSignedValue)) { - int __sign = 0; - int __absVal = __intVal(a16bitSignedValue); - int __exp, __mantissa, __byte; - static char __uLawExp[] = - { - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, - 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 - }; - - if (__absVal < 0) { - if (__absVal <= -32256) { - RETURN (__MKSMALLINT(0)); - } - __absVal = -__absVal; - __sign = 0x80; - } else { - if (__absVal >= 32256) { - RETURN (__MKSMALLINT(128)); - } - } - - __exp = __uLawExp[__absVal >> 8]; - __mantissa = (__absVal >> (__exp+3)) & 0xF; - __byte = ~(__sign | (__exp<<4) | __mantissa) & 0xFF; - RETURN (__MKSMALLINT(__byte)); - } -%}. - "/ - "/ fallback for non-integral argument - "/ - sign := 0. - (absVal := a16bitSignedValue asInteger) < 0 ifTrue:[ - (absVal <= -32256) ifTrue:[ - ^ 0 - ]. - absVal := absVal negated. - sign := 16r80 - ] ifFalse:[ - absVal >= 32256 ifTrue:[ - ^ 128 - ] - ]. - - exp := #[ - 0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 - 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 - 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 - 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 - 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 - 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 - 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 - 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 - ] at:(absVal bitShift:-1)+1. - mantissa := (absVal bitShift:(exp+3) negated) bitAnd:16r0F. - byte := ((sign bitOr:(exp bitShift:4)) bitOr:mantissa) bitInvert bitAnd:16rFF. - ^ byte - - " - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:0) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:32256) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-32256) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:32767) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-32767) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:100) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-100) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:104) - SoundStream uLawToLinear16:(SoundStream linear16ToUlaw:-104) - " - - "Modified: / 9.12.1997 / 16:46:24 / cg" ! ! !SoundStream class methodsFor:'default values'! +defaultAudioFormat + ^ #U8 +! + defaultBitsPerSample "minimum, supported by all audio systems" ^ 8 ! -defaultAudioFormat - ^ #U8 -! - defaultNumberOfChannels "minimum, supported by all audio systems" @@ -580,6 +580,59 @@ "Modified: 17.11.1995 / 17:45:40 / cg" ! ! +!SoundStream methodsFor:'catching invalid methods'! + +pathName:filename + "catch pathname access - its fixed here" + + self shouldNotImplement +! + +pathName:filename in:aDirectory + "catch pathname access - its fixed here" + + self shouldNotImplement +! ! + +!SoundStream methodsFor:'mode setting'! + +bitsPerSample + "return the number of bits per sample - usually 8" + + ^ bitsPerSample +! + +bitsPerSample:aNumber + "set the number of bits per sample" + + bitsPerSample := aNumber +! + +numberOfChannels + "return the number of channels (1 or 2; usually 1)" + + ^ numberOfChannels +! + +numberOfChannels:aNumber + "set the number of channels" + + numberOfChannels := aNumber +! + +sampleRate + "return the sample rate" + + ^ sampleRate +! + +sampleRate:aNumber + "set the sample rate in hertz - on some + devices, this is a nop" + + self setSampleRate:aNumber. +! ! + !SoundStream methodsFor:'private'! dumpSettings @@ -640,113 +693,28 @@ " ! -supportedAudioFormats - "return a collection of supported audio formats. - returned symbols are: - U8 unsigned 8bit samples - S8 signed 8bit samples - MU_LAW u-law encoded 8bit samples - A_LAW a-law encoded 8bit samples - IMA_ADPCM adpcm encoded - U16 unsigned 16bit samples - U16_LE unsigned 16bit big endian samples - U16_BE unsigned 16bit big endian samples - S16 signed 16bit little endian samples - S16_LE signed 16bit little endian samples - S16_BE signed 16bit big endian samples - MPEG audio mpeg encoded - " - - |fd audioFormatMask - supportedFormats - supports_MU_LAW supports_A_LAW - supports_IMA_ADPCM supports_U8 - supports_S16_LE supports_S16_BE - supports_S8 supports_U16_LE - supports_U16_BE supports_MPEG| +initialize + "initialize for least common mode" - fd := self fileDescriptor. - fd isNil ifTrue:[ - self error. - ^ nil - ]. -%{ - int f = __intVal(fd); - int __audioFormatMask = 0; - -#if defined(DEV_AUDIO) -# if defined(LINUX) - if (ioctl(f, SNDCTL_DSP_GETFMTS, &__audioFormatMask) >= 0) { - audioFormatMask = __MKSMALLINT(__audioFormatMask); - - supports_MU_LAW = (__audioFormatMask & AFMT_MU_LAW) ? true : false; - supports_A_LAW = (__audioFormatMask & AFMT_A_LAW) ? true : false; - supports_IMA_ADPCM = (__audioFormatMask & AFMT_IMA_ADPCM) ? true : false; - supports_U8 = (__audioFormatMask & AFMT_U8) ? true : false; - supports_S16_LE = (__audioFormatMask & AFMT_S16_LE) ? true : false; - supports_S16_BE = (__audioFormatMask & AFMT_S16_BE) ? true : false; - supports_S8 = (__audioFormatMask & AFMT_S8) ? true : false; - supports_U16_LE = (__audioFormatMask & AFMT_U16_LE) ? true : false; - supports_U16_BE = (__audioFormatMask & AFMT_U16_BE) ? true : false; - supports_MPEG = (__audioFormatMask & AFMT_MPEG) ? true : false; - } -# else - supports_MU_LAW = true; -# endif + buffered := false. + bitsPerSample := 8. + audioFormat := #U8. + numberOfChannels := 1. + sampleRate := 8000. -#endif /* DEV_AUDIO */ - -#ifdef IRIS_AUDIO - supports_U8 = true; - supports_U16_BE = true; -#endif - -%}. - supportedFormats := IdentitySet new. - supports_MU_LAW ifTrue:[ - supportedFormats add:#'MU_LAW' - ]. - supports_A_LAW ifTrue:[ - supportedFormats add:#'A_LAW' - ]. - supports_IMA_ADPCM ifTrue:[ - supportedFormats add:#'IMA_ADPCM' - ]. - supports_S8 ifTrue:[ - supportedFormats add:#'S8' - ]. - supports_U8 ifTrue:[ - supportedFormats add:#'U8' + '/dev/audio' asFilename exists ifTrue:[ + "/ + "/ sunos or linux + "/ + pathName := '/dev/audio'. ]. - supports_S16_LE ifTrue:[ - supportedFormats add:#'S16_LE'. - supportedFormats add:#'S16'. - ]. - supports_S16_BE ifTrue:[ - supportedFormats add:#'S16_BE'. - supportedFormats add:#'S16'. - ]. - supports_U16_LE ifTrue:[ - supportedFormats add:#'U16_LE'. - supportedFormats add:#'U16'. + + OperatingSystem getOSType = 'irix' ifTrue:[ + "no device, use special library calls" + pathName := nil. ]. - supports_U16_BE ifTrue:[ - supportedFormats add:#'U16_BE'. - supportedFormats add:#'U16'. - ]. - supports_MPEG ifTrue:[ - supportedFormats add:#'MPEG' - ]. - ^ supportedFormats. - " - |s formats| - - s := self writing. - formats := s supportedAudioFormats. - s close. - formats - " + "Created: 17.11.1995 / 17:28:14 / cg" ! resetSoundCard @@ -951,85 +919,115 @@ self writing setSampleRate:10000; dumpSettings; close self writing setSampleRate:1000; dumpSettings; close " -! ! - -!SoundStream methodsFor:'catching invalid methods'! - -pathName:filename - "catch pathname access - its fixed here" - - self shouldNotImplement -! - -pathName:filename in:aDirectory - "catch pathname access - its fixed here" - - self shouldNotImplement -! ! - -!SoundStream methodsFor:'mode setting'! - -bitsPerSample - "return the number of bits per sample - usually 8" - - ^ bitsPerSample -! - -bitsPerSample:aNumber - "set the number of bits per sample" - - bitsPerSample := aNumber -! - -numberOfChannels - "return the number of channels (1 or 2; usually 1)" - - ^ numberOfChannels ! -numberOfChannels:aNumber - "set the number of channels" +supportedAudioFormats + "return a collection of supported audio formats. + returned symbols are: + U8 unsigned 8bit samples + S8 signed 8bit samples + MU_LAW u-law encoded 8bit samples + A_LAW a-law encoded 8bit samples + IMA_ADPCM adpcm encoded + U16 unsigned 16bit samples + U16_LE unsigned 16bit big endian samples + U16_BE unsigned 16bit big endian samples + S16 signed 16bit little endian samples + S16_LE signed 16bit little endian samples + S16_BE signed 16bit big endian samples + MPEG audio mpeg encoded + " - numberOfChannels := aNumber -! - -sampleRate - "return the sample rate" + |fd audioFormatMask + supportedFormats + supports_MU_LAW supports_A_LAW + supports_IMA_ADPCM supports_U8 + supports_S16_LE supports_S16_BE + supports_S8 supports_U16_LE + supports_U16_BE supports_MPEG| - ^ sampleRate -! + fd := self fileDescriptor. + fd isNil ifTrue:[ + self error. + ^ nil + ]. +%{ + int f = __intVal(fd); + int __audioFormatMask = 0; + +#if defined(DEV_AUDIO) +# if defined(LINUX) + if (ioctl(f, SNDCTL_DSP_GETFMTS, &__audioFormatMask) >= 0) { + audioFormatMask = __MKSMALLINT(__audioFormatMask); -sampleRate:aNumber - "set the sample rate in hertz - on some - devices, this is a nop" - - self setSampleRate:aNumber. -! ! - -!SoundStream methodsFor:'private'! + supports_MU_LAW = (__audioFormatMask & AFMT_MU_LAW) ? true : false; + supports_A_LAW = (__audioFormatMask & AFMT_A_LAW) ? true : false; + supports_IMA_ADPCM = (__audioFormatMask & AFMT_IMA_ADPCM) ? true : false; + supports_U8 = (__audioFormatMask & AFMT_U8) ? true : false; + supports_S16_LE = (__audioFormatMask & AFMT_S16_LE) ? true : false; + supports_S16_BE = (__audioFormatMask & AFMT_S16_BE) ? true : false; + supports_S8 = (__audioFormatMask & AFMT_S8) ? true : false; + supports_U16_LE = (__audioFormatMask & AFMT_U16_LE) ? true : false; + supports_U16_BE = (__audioFormatMask & AFMT_U16_BE) ? true : false; + supports_MPEG = (__audioFormatMask & AFMT_MPEG) ? true : false; + } +# else + supports_MU_LAW = true; +# endif -initialize - "initialize for least common mode" +#endif /* DEV_AUDIO */ - buffered := false. - bitsPerSample := 8. - audioFormat := #U8. - numberOfChannels := 1. - sampleRate := 8000. +#ifdef IRIS_AUDIO + supports_U8 = true; + supports_U16_BE = true; +#endif - '/dev/audio' asFilename exists ifTrue:[ - "/ - "/ sunos or linux - "/ - pathName := '/dev/audio'. +%}. + supportedFormats := IdentitySet new. + supports_MU_LAW ifTrue:[ + supportedFormats add:#'MU_LAW' + ]. + supports_A_LAW ifTrue:[ + supportedFormats add:#'A_LAW' + ]. + supports_IMA_ADPCM ifTrue:[ + supportedFormats add:#'IMA_ADPCM' + ]. + supports_S8 ifTrue:[ + supportedFormats add:#'S8' + ]. + supports_U8 ifTrue:[ + supportedFormats add:#'U8' ]. + supports_S16_LE ifTrue:[ + supportedFormats add:#'S16_LE'. + supportedFormats add:#'S16'. + ]. + supports_S16_BE ifTrue:[ + supportedFormats add:#'S16_BE'. + supportedFormats add:#'S16'. + ]. + supports_U16_LE ifTrue:[ + supportedFormats add:#'U16_LE'. + supportedFormats add:#'U16'. + ]. + supports_U16_BE ifTrue:[ + supportedFormats add:#'U16_BE'. + supportedFormats add:#'U16'. + ]. + supports_MPEG ifTrue:[ + supportedFormats add:#'MPEG' + ]. + ^ supportedFormats. - OperatingSystem getOSType = 'irix' ifTrue:[ - "no device, use special library calls" - pathName := nil. - ]. + " + |s formats| - "Created: 17.11.1995 / 17:28:14 / cg" + s := self writing. + formats := s supportedAudioFormats. + s close. + formats + " ! ! !SoundStream methodsFor:'redefined'! @@ -1258,6 +1256,18 @@ ^ super nextPutBytes:count from:anObject startingAt:start ! +openForWriting + "open the file writeonly. + If the file does not exist its an error, return nil; + otherwise return the receiver." + + mode := #writeonly. + didWrite := true. + ^ self openWithMode:WriteMode + + "Created: / 15.12.1997 / 13:13:56 / cg" +! + openWithMode:aMode ((aMode = 'r') or:[aMode = 'w']) ifFalse:[ self error:'invalid mode'. @@ -1339,6 +1349,22 @@ !SoundStream methodsFor:'sine wave generation'! +tuneTone + self tuneTone:440 + + " + SoundStream writing tuneTone; close + SoundStream writing setSampleRate:4000; tuneTone; close + SoundStream writing setSampleRate:8000; tuneTone; close + SoundStream writing setSampleRate:10000; tuneTone; close + SoundStream writing setSampleRate:20000; tuneTone; close + SoundStream writing setSampleRate:40000; tuneTone; close + SoundStream writing setSampleRate:20000; dumpSettings; close + " + + "Modified: / 12.12.1997 / 20:34:42 / cg" +! + tuneTone:freq |buffer numSamples val scale oldFormat| @@ -1386,25 +1412,10 @@ " "Modified: / 12.12.1997 / 20:34:27 / cg" -! - -tuneTone - self tuneTone:440 - - " - SoundStream writing tuneTone; close - SoundStream writing setSampleRate:4000; tuneTone; close - SoundStream writing setSampleRate:8000; tuneTone; close - SoundStream writing setSampleRate:10000; tuneTone; close - SoundStream writing setSampleRate:20000; tuneTone; close - SoundStream writing setSampleRate:40000; tuneTone; close - SoundStream writing setSampleRate:20000; dumpSettings; close - " - - "Modified: / 12.12.1997 / 20:34:42 / cg" ! ! !SoundStream class methodsFor:'documentation'! version -^ '$Header: /cvs/stx/stx/libbasic2/SoundStream.st,v 1.29 1997-12-15 11:04:11 cg Exp $'! ! +^ '$Header: /cvs/stx/stx/libbasic2/SoundStream.st,v 1.30 1997-12-15 12:20:26 cg Exp $'! ! +SoundStream initialize!