Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multichannel decoded audio #21539

Merged
merged 3 commits into from Sep 11, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Add support for multichannel decoded audio

  • Loading branch information
ferjm committed Sep 11, 2018
commit f423ede07f79dbaf7cc2ec41795a7a8d76d98501

Some generated files are not rendered by default. Learn more.

@@ -64,7 +64,7 @@ impl AudioBuffer {
number_of_channels: u32,
length: u32,
sample_rate: f32,
initial_data: Option<&[f32]>,
initial_data: Option<&[Vec<f32>]>,
) -> DomRoot<AudioBuffer> {
let buffer = AudioBuffer::new_inherited(number_of_channels, length, sample_rate);
let buffer = reflect_dom_object(Box::new(buffer), global, AudioBufferBinding::Wrap);
@@ -93,20 +93,19 @@ impl AudioBuffer {
}

#[allow(unsafe_code)]
pub fn set_channels(&self, initial_data: Option<&[f32]>) {
pub fn set_channels(&self, initial_data: Option<&[Vec<f32>]>) {
let global = self.global();
let cx = global.get_cx();
let _ac = JSAutoCompartment::new(cx, global.reflector().get_jsobject().get());
let chans = self.js_channels.borrow_mut();
for channel in 0..self.number_of_channels {
rooted!(in (cx) let mut array = ptr::null_mut::<JSObject>());
let offset = (channel * self.length) as usize;
match initial_data {
Some(data) => {
let _ = unsafe {
Float32Array::create(
cx,
CreateWith::Slice(&data[offset..offset + (self.length as usize) - 1]),
CreateWith::Slice(data[channel as usize].as_slice()),
array.handle_mut(),
)
};
@@ -405,22 +405,38 @@ impl BaseAudioContextMethods for BaseAudioContext {
let audio_data = audio_data.to_vec();
let decoded_audio = Arc::new(Mutex::new(Vec::new()));
let decoded_audio_ = decoded_audio.clone();
let decoded_audio__ = decoded_audio.clone();
let this = Trusted::new(self);
let this_ = this.clone();
let task_source = window.dom_manipulation_task_source();
let task_source_ = window.dom_manipulation_task_source();
let canceller = window.task_canceller(TaskSourceName::DOMManipulation);
let canceller_ = window.task_canceller(TaskSourceName::DOMManipulation);
let callbacks = AudioDecoderCallbacks::new()
.ready(move |channel_count| {
decoded_audio
.lock()
.unwrap()
.resize(channel_count as usize, Vec::new());
})
.progress(move |buffer, channel| {
let mut decoded_audio = decoded_audio_.lock().unwrap();
decoded_audio[(channel - 1) as usize].extend_from_slice((*buffer).as_ref());
})
.eos(move || {
let _ = task_source.queue_with_canceller(
task!(audio_decode_eos: move || {
let this = this.root();
let decoded_audio = decoded_audio.lock().unwrap();
let decoded_audio = decoded_audio__.lock().unwrap();
let length = if decoded_audio.len() >= 1 {
decoded_audio[0].len()
} else {
0
};
let buffer = AudioBuffer::new(
&this.global().as_window(),
1, // XXX servo-media should provide this info
decoded_audio.len() as u32,
decoded_audio.len() as u32 /* number of channels */,
length as u32,
this.sample_rate,
Some(decoded_audio.as_slice()));
let mut resolvers = this.decode_resolvers.borrow_mut();
@@ -451,12 +467,6 @@ impl BaseAudioContextMethods for BaseAudioContext {
&canceller_,
);
})
.progress(move |buffer| {
decoded_audio_
.lock()
.unwrap()
.extend_from_slice((*buffer).as_ref());
})
.build();
self.audio_context_impl
.decode_audio_data(audio_data, callbacks);
@@ -144,6 +144,10 @@ impl OfflineAudioContextMethods for OfflineAudioContext {
task!(resolve: move || {
let this = this.root();
let processed_audio = processed_audio.lock().unwrap();
let processed_audio: Vec<_> = processed_audio
.chunks(this.length as usize)
.map(|channel| channel.to_vec())
.collect();
let buffer = AudioBuffer::new(
&this.global().as_window(),
this.channel_count,
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.