@@ -2,7 +2,7 @@ use super::decoders::{self, AdpcmDecoder, Decoder, PcmDecoder, SeekableDecoder};
2
2
use super :: { SoundHandle , SoundInstanceHandle , SoundStreamInfo , SoundTransform } ;
3
3
use crate :: backend:: audio:: { DecodeError , RegisterError } ;
4
4
use crate :: buffer:: Substream ;
5
- use crate :: tag_utils:: SwfSlice ;
5
+ use crate :: tag_utils:: { SwfMovie , SwfSlice } ;
6
6
use slotmap:: SlotMap ;
7
7
use std:: io:: Cursor ;
8
8
use std:: sync:: { Arc , Mutex , RwLock } ;
@@ -137,6 +137,37 @@ impl<D: Decoder> dasp::signal::Signal for DecoderStream<D> {
137
137
}
138
138
}
139
139
140
+ #[ derive( Clone ) ]
141
+ enum SoundData {
142
+ Owned ( Arc < [ u8 ] > ) ,
143
+ Movie ( SwfSlice ) ,
144
+ }
145
+
146
+ impl SoundData {
147
+ pub fn len ( & self ) -> usize {
148
+ match self {
149
+ Self :: Owned ( data) => data. len ( ) ,
150
+ Self :: Movie ( data) => data. len ( ) ,
151
+ }
152
+ }
153
+ }
154
+
155
+ impl AsRef < [ u8 ] > for SoundData {
156
+ #[ inline]
157
+ fn as_ref ( & self ) -> & [ u8 ] {
158
+ match self {
159
+ Self :: Owned ( data) => & data,
160
+ Self :: Movie ( data) => data. as_ref ( ) ,
161
+ }
162
+ }
163
+ }
164
+
165
+ // Needed for `AdpcmDecoder<std::io::Cursor<mixer::SoundData>>: SeekableDecoder` :(
166
+ impl Default for SoundData {
167
+ fn default ( ) -> Self {
168
+ Self :: Owned ( Arc :: new ( [ ] ) )
169
+ }
170
+ }
140
171
/// Contains the data and metadata for a sound in an SWF file.
141
172
///
142
173
/// A sound is defined by the `DefineSound` SWF tags and contains the audio data for the sound.
@@ -147,7 +178,7 @@ struct Sound {
147
178
/// The audio data of this sound.
148
179
///
149
180
/// This will be compressed in the format indicated by `format.compression`.
150
- data : Arc < [ u8 ] > ,
181
+ data : SoundData ,
151
182
152
183
/// Number of samples in this audio.
153
184
/// This does not include `skip_sample_frames`.
@@ -295,7 +326,7 @@ impl AudioMixer {
295
326
/// * ActionScript sounds that may have a custom start and loop setting
296
327
fn make_seekable_decoder (
297
328
format : & swf:: SoundFormat ,
298
- data : Cursor < ArcAsRef > ,
329
+ data : Cursor < SoundData > ,
299
330
) -> Result < Box < dyn SeekableDecoder > , decoders:: Error > {
300
331
let decoder: Box < dyn SeekableDecoder > = match format. compression {
301
332
AudioCompression :: UncompressedUnknownEndian => {
@@ -354,7 +385,7 @@ impl AudioMixer {
354
385
& self ,
355
386
sound : & Sound ,
356
387
settings : & swf:: SoundInfo ,
357
- data : Cursor < ArcAsRef > ,
388
+ data : Cursor < SoundData > ,
358
389
) -> Result < Box < dyn Stream > , DecodeError > {
359
390
// Instantiate a decoder for the compression that the sound data uses.
360
391
let decoder = Self :: make_seekable_decoder ( & sound. format , data) ?;
@@ -505,7 +536,7 @@ impl AudioMixer {
505
536
}
506
537
507
538
/// Registers an embedded SWF sound with the audio mixer.
508
- pub fn register_sound ( & mut self , swf_sound : & swf:: Sound ) -> Result < SoundHandle , RegisterError > {
539
+ pub fn register_sound ( & mut self , movie : Arc < SwfMovie > , swf_sound : & swf:: Sound ) -> Result < SoundHandle , RegisterError > {
509
540
// Slice off latency seek for MP3 data.
510
541
let ( skip_sample_frames, data) = if swf_sound. format . compression == AudioCompression :: Mp3 {
511
542
if swf_sound. data . len ( ) < 2 {
@@ -519,7 +550,7 @@ impl AudioMixer {
519
550
520
551
let sound = Sound {
521
552
format : swf_sound. format . clone ( ) ,
522
- data : Arc :: from ( data) ,
553
+ data : SoundData :: Movie ( SwfSlice :: new ( movie , data) ) ,
523
554
num_sample_frames : swf_sound. num_samples ,
524
555
skip_sample_frames,
525
556
} ;
@@ -539,7 +570,7 @@ impl AudioMixer {
539
570
is_stereo : true ,
540
571
is_16_bit : true ,
541
572
} ,
542
- data,
573
+ data : SoundData :: Owned ( data ) ,
543
574
num_sample_frames : metadata. num_sample_frames ,
544
575
skip_sample_frames : 0 ,
545
576
} ;
@@ -579,7 +610,7 @@ impl AudioMixer {
579
610
settings : & swf:: SoundInfo ,
580
611
) -> Result < SoundInstanceHandle , DecodeError > {
581
612
let sound = & self . sounds [ sound_handle] ;
582
- let data = Cursor :: new ( ArcAsRef ( Arc :: clone ( & sound. data ) ) ) ;
613
+ let data = Cursor :: new ( sound. data . clone ( ) ) ;
583
614
// Create a stream that decodes and resamples the sound.
584
615
let stream = if sound. skip_sample_frames == 0
585
616
&& settings. in_sample . is_none ( )
@@ -760,23 +791,6 @@ impl AudioMixerProxy {
760
791
}
761
792
}
762
793
763
- /// A dummy wrapper struct to implement `AsRef<[u8]>` for `Arc<Vec<u8>>`.
764
- /// Not having this trait causes problems when trying to use `Cursor<Vec<u8>>`.
765
- struct ArcAsRef ( Arc < [ u8 ] > ) ;
766
-
767
- impl AsRef < [ u8 ] > for ArcAsRef {
768
- #[ inline]
769
- fn as_ref ( & self ) -> & [ u8 ] {
770
- & self . 0
771
- }
772
- }
773
-
774
- impl Default for ArcAsRef {
775
- fn default ( ) -> Self {
776
- ArcAsRef ( Arc :: new ( [ ] ) )
777
- }
778
- }
779
-
780
794
/// A stream for event sound instances with custom envelopes, start/end point, or loop settings.
781
795
struct EventSoundStream {
782
796
decoder : Box < dyn SeekableDecoder > ,
@@ -1069,8 +1083,8 @@ impl dasp::signal::Signal for EnvelopeSignal {
1069
1083
macro_rules! impl_audio_mixer_backend {
1070
1084
( $mixer: ident) => {
1071
1085
#[ inline]
1072
- fn register_sound( & mut self , swf_sound: & swf:: Sound ) -> Result <SoundHandle , RegisterError > {
1073
- self . $mixer. register_sound( swf_sound)
1086
+ fn register_sound( & mut self , movie : std :: sync :: Arc <$crate :: tag_utils :: SwfMovie > , swf_sound: & swf:: Sound ) -> Result <SoundHandle , RegisterError > {
1087
+ self . $mixer. register_sound( movie , swf_sound)
1074
1088
}
1075
1089
1076
1090
#[ inline]
0 commit comments