#FEATURE by exept
class: SoundStream
added:
#pause:
#testOctaves
changed: #testMelody
class: SoundStream class
added: #noteToHertz:
--- a/SoundStream.st Sat Jan 04 02:55:34 2020 +0100
+++ b/SoundStream.st Sat Jan 04 03:30:05 2020 +0100
@@ -1132,6 +1132,48 @@
^ false.
!
+noteToHertz:noteName
+ "notename is one of:
+ a1, b2, f3, c#3 etc."
+
+ |octave lastDigit note f sharp|
+
+ octave := 4.
+ (lastDigit := noteName last) isDigit ifTrue:[
+ octave := lastDigit digitValue
+ ].
+ note := noteName first.
+ sharp := (noteName size > 1) and:[noteName second == $#].
+ note = $a ifTrue:[
+ f := sharp ifTrue:[466.2] ifFalse:[440]
+ ] ifFalse:[ note = $b ifTrue:[
+ f := 493.8
+ ] ifFalse:[ note = $c ifTrue:[
+ f := sharp ifTrue:[277.2] ifFalse:[261.6]
+ ] ifFalse:[ note = $d ifTrue:[
+ f := sharp ifTrue:[311.1] ifFalse:[293.6]
+ ] ifFalse:[ note = $e ifTrue:[
+ f := 329.6
+ ] ifFalse:[ note = $f ifTrue:[
+ f := sharp ifTrue:[370] ifFalse:[349.2]
+ ] ifFalse:[ note = $g ifTrue:[
+ f := sharp ifTrue:[415.3] ifFalse:[392]
+ ]]]]]]].
+
+ f := f * (2 raisedTo:octave-4).
+ ^ f.
+
+ "
+ SoundStream noteToHertz:'a'
+ SoundStream noteToHertz:'g4'
+ SoundStream noteToHertz:'e5'
+ SoundStream noteToHertz:'b3'
+ SoundStream noteToHertz:'c#3'
+ "
+
+ "Modified: / 31.1.1999 / 12:06:27 / cg"
+!
+
usedAudio
"returns a symbol describing which audio system is used;
one of PORTAUDIO, DEV_AUDIO, WIN32_WAVESOUND, IRIS_AUDIO"
@@ -1471,6 +1513,32 @@
!SoundStream methodsFor:'sine wave generation'!
+pause:nSeconds
+ "output noting for nSeconds"
+
+ |buffer numSamples restSamples|
+
+ "allocate memory for 1 sec playing time"
+ numSamples := self sampleRate.
+ audioFormat == #U16 ifTrue:[
+ buffer := WordArray new:numSamples withAll:16r8000.
+ ] ifFalse:[ audioFormat == #S16 ifTrue:[
+ buffer := SignedWordArray new:numSamples withAll:0.
+ ] ifFalse:[ audioFormat == #F32 ifTrue:[
+ buffer := FloatArray new:numSamples withAll:0.0.
+ ] ifFalse:[
+ self halt
+ ]]].
+
+ 1 to:nSeconds truncated do:[:s |
+ self nextPutBytes:(numSamples*2) from:buffer startingAt:1
+ ].
+ restSamples := ((nSeconds - nSeconds truncated) * numSamples) truncated.
+ restSamples > 0 ifTrue:[
+ self nextPutBytes:(restSamples*2) from:buffer startingAt:1
+ ].
+!
+
playSine16:freq forSeconds:nSeconds
"output some tone for some time
in S16 audioFormat - a test method"
@@ -1589,16 +1657,43 @@
!
testMelody
+ #(g 0.5
+ e 0.5
+ e 0.5
+ f 0.5
+ d 0.5
+ d 0.5
+ c 0.5
+ d 0.5
+ e 0.5
+ f 0.5
+ g 0.5
+ g 0.5
+ g 0.5
+ ) pairWiseDo:[:note :duration |
+ self playSine:(self class noteToHertz:note) forSeconds:duration.
+ self pause:0.05.
+ ].
+
+ "
+ self writing testMelody; close
+ "
+
+ "Created: / 31.1.1999 / 12:07:45 / cg"
+ "Modified: / 31.1.1999 / 12:16:09 / cg"
+!
+
+testOctaves
3 timesRepeat:[
- self playSine:220 forSeconds:0.5.
- self playSine:440 forSeconds:0.5.
- self playSine:880 forSeconds:0.5.
- self playSine:1760 forSeconds:0.5.
- self playSine:3520 forSeconds:0.5.
+ self playSine:220 forSeconds:0.5.
+ self playSine:440 forSeconds:0.5.
+ self playSine:880 forSeconds:0.5.
+ self playSine:1760 forSeconds:0.5.
+ self playSine:3520 forSeconds:0.5.
]
"
- self writing testMelody; close
+ self writing testOctaves; close
"
"Created: / 31.1.1999 / 12:07:45 / cg"