@@ -70,43 +70,72 @@ public class ScratchSoundPlayer {
70
70
public function ScratchSoundPlayer (wavFileData :ByteArray ) {
71
71
getSample = getSample16Uncompressed;
72
72
if (wavFileData != null ) {
73
- var info :* = WAVFile. decode (wavFileData);
74
- soundData = wavFileData;
75
- startOffset = info . sampleDataStart;
76
- endOffset = startOffset + info . sampleDataSize;
77
- stepSize = info . samplesPerSecond / 44100.0 ;
78
- if (info . encoding == 17 ) {
79
- adpcmBlockSize = info . adpcmBlockSize;
80
- getSample = getSampleADPCM;
81
- } else {
82
- if (info . bitsPerSample == 8 ) getSample = getSample8Uncompressed;
83
- if (info . bitsPerSample == 16 ) getSample = getSample16Uncompressed;
73
+ try {
74
+ var info :* = WAVFile. decode (wavFileData);
75
+ soundData = wavFileData;
76
+ startOffset = info . sampleDataStart;
77
+ endOffset = startOffset + info . sampleDataSize;
78
+ stepSize = info . samplesPerSecond / 44100.0 ;
79
+ if (info . encoding == 17 ) {
80
+ adpcmBlockSize = info . adpcmBlockSize;
81
+ getSample = getSampleADPCM;
82
+ } else {
83
+ if (info . bitsPerSample == 8 ) getSample = getSample8Uncompressed;
84
+ if (info . bitsPerSample == 16 ) getSample = getSample16Uncompressed;
85
+ }
84
86
}
87
+ catch (e:* ) {}
85
88
}
86
89
}
87
90
88
- public function atEnd ():Boolean { return soundChannel == null }
91
+ public function isPlaying (snd :ByteArray = null ):Boolean {
92
+ return (activeSounds. indexOf (this ) > - 1 && (! snd || soundData == snd));
93
+ }
94
+
95
+ public function atEnd ():Boolean { return soundChannel == null ; }
89
96
90
97
public function stopPlaying ():void {
91
98
if (soundChannel != null ) {
92
- soundChannel . stop () ;
99
+ var sc : SoundChannel = soundChannel ;
93
100
soundChannel = null ;
101
+ sc. stop ();
102
+ sc. dispatchEvent (new Event (Event . SOUND_COMPLETE ));
94
103
}
95
104
var i: int = activeSounds. indexOf (this );
96
105
if (i >= 0 ) activeSounds. splice (i, 1 );
97
106
}
98
107
108
+ public function createNative ():void {
109
+ if (!! scratchSound. nativeSound) return ;
110
+
111
+ var flashSnd: Sound = scratchSound. nativeSound = new Sound ();
112
+ var convertedSamples: ByteArray = new ByteArray ();
113
+ convertedSamples. length = endOffset - startOffset;
114
+ bytePosition = startOffset;
115
+ var sampleCount: uint = 0 ;
116
+ while (bytePosition < endOffset) {
117
+ var n: Number = interpolatedSample();
118
+ convertedSamples. writeFloat (n);
119
+ convertedSamples. writeFloat (n);
120
+ ++ sampleCount;
121
+ }
122
+
123
+ convertedSamples. position = 0 ;
124
+ flashSnd. loadPCMFromByteArray(convertedSamples, sampleCount);
125
+ }
126
+
99
127
public function startPlaying (doneFunction :Function = null ):void {
100
128
stopIfAlreadyPlaying();
101
129
activeSounds. push (this );
102
- bytePosition = startOffset;
103
- nextSample = getSample();
104
130
105
- var flashSnd : Sound = new Sound ();
106
- flashSnd . addEventListener (SampleDataEvent . SAMPLE_DATA , writeSampleData) ;
107
- soundChannel = flashSnd . play ();
131
+ createNative ();
132
+
133
+ soundChannel = scratchSound . nativeSound . play ();
108
134
if (soundChannel ) {
109
- if (doneFunction != null ) soundChannel . addEventListener (Event . SOUND_COMPLETE , doneFunction);
135
+ soundChannel . addEventListener (Event . SOUND_COMPLETE , function (e: Event ): void {
136
+ soundChannel = null ;
137
+ if (doneFunction != null ) doneFunction();
138
+ });
110
139
} else {
111
140
// User has no sound card or too many sounds already playing.
112
141
stopPlaying();
0 commit comments