Skip to content

Commit 97c13be

Browse files
authored
Merge pull request #317 from orottier/feature/fix-media-element
Fix broken resampling in MediaElement, support more media types
2 parents 7d10ff7 + 6b4e89e commit 97c13be

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rust-version = "1.70"
1616
arc-swap = "1.6"
1717
arrayvec = "0.7"
1818
cpal = { version = "0.15.0", optional = true }
19-
creek = "1.0"
19+
creek = "1.1"
2020
crossbeam-channel = "0.5"
2121
cubeb = { version = "0.10.0", optional = true }
2222
dasp_sample = "0.11"
@@ -54,12 +54,9 @@ mp3 = ["symphonia/mp3", "creek/decode-mp3"]
5454
ogg = ["symphonia/ogg", "symphonia/vorbis", "creek/decode-ogg", "creek/decode-vorbis"]
5555
flac = ["symphonia/flac", "creek/decode-flac"]
5656
wav = ["symphonia/wav", "symphonia/pcm", "creek/decode-wav", "creek/decode-pcm"]
57-
# TODO(m4a): Add "creek/decode-aac" after <https://github.com/MeadowlarkDAW/creek/pull/22> has been merged
58-
aac = ["symphonia/aac"]
59-
# TODO(m4a): Add "creek/decode-isomp4" after <https://github.com/MeadowlarkDAW/creek/pull/22> has been merged
60-
m4a = ["aac", "symphonia/isomp4"]
61-
# TODO(alac): Add "creek/decode-alac" and "creek/decode-isomp4" after <https://github.com/MeadowlarkDAW/creek/pull/22> has been merged
62-
alac = ["symphonia/alac", "symphonia/isomp4"]
57+
aac = ["symphonia/aac", "creek/decode-aac"]
58+
m4a = ["aac", "symphonia/isomp4", "creek/decode-isomp4"]
59+
alac = ["symphonia/alac", "symphonia/isomp4", "creek/decode-alac", "creek/decode-isomp4"]
6360
cpal = ["dep:cpal"]
6461
cubeb = ["dep:cubeb"]
6562
cpal-jack = ["cpal", "cpal/jack"]

src/media_element.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{AtomicF64, AudioBuffer, RENDER_QUANTUM_SIZE};
1111
/// Real time safe audio stream
1212
pub(crate) struct RTSStream {
1313
stream: ReadDiskStream<SymphoniaDecoder>,
14+
number_of_channels: usize,
1415
current_time: Arc<AtomicF64>,
1516
receiver: Receiver<MediaElementAction>,
1617
loop_: Arc<AtomicBool>,
@@ -54,6 +55,7 @@ impl MediaElement {
5455
0, // The frame in the file to start reading from.
5556
Default::default(), // Use default read stream options.
5657
)?;
58+
let number_of_channels = read_disk_stream.info().num_channels as usize;
5759

5860
// Cache the start of the file into cache with index `0`.
5961
let _ = read_disk_stream.cache(0, 0);
@@ -79,6 +81,7 @@ impl MediaElement {
7981

8082
let rts_stream = RTSStream {
8183
stream: read_disk_stream,
84+
number_of_channels,
8285
current_time: Arc::clone(&current_time),
8386
receiver,
8487
loop_: Arc::clone(&loop_),
@@ -161,7 +164,10 @@ impl Iterator for RTSStream {
161164
}
162165

163166
if self.paused.load(Ordering::SeqCst) {
164-
let silence = AudioBuffer::from(vec![vec![0.; RENDER_QUANTUM_SIZE]], sample_rate);
167+
let silence = AudioBuffer::from(
168+
vec![vec![0.; RENDER_QUANTUM_SIZE]; self.number_of_channels],
169+
sample_rate,
170+
);
165171
return Some(Ok(silence));
166172
}
167173

tests/media_element.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use web_audio_api::context::{AudioContext, AudioContextOptions, BaseAudioContext};
2+
use web_audio_api::node::AudioNode;
3+
use web_audio_api::MediaElement;
4+
5+
#[test]
6+
fn test_media_element_source_progress() {
7+
let options = AudioContextOptions {
8+
sink_id: "none".into(),
9+
..AudioContextOptions::default()
10+
};
11+
let context = AudioContext::new(options);
12+
13+
let mut media = MediaElement::new("samples/major-scale.ogg").unwrap();
14+
media.set_loop(true);
15+
16+
let src = context.create_media_element_source(&mut media);
17+
src.connect(&context.destination());
18+
media.play();
19+
20+
// TODO improve test setup of online AudioContext
21+
// <https://github.com/orottier/web-audio-api-rs/issues/323>
22+
// Sleep for a bit, make sure the audio thread has started
23+
std::thread::sleep(std::time::Duration::from_millis(100));
24+
25+
// assert the media has progressed
26+
assert!(media.current_time() > 0.05);
27+
}

0 commit comments

Comments
 (0)