2017-10-22 12:17:25 +00:00
|
|
|
use std::mem;
|
2017-10-18 18:24:05 +00:00
|
|
|
use std::os::raw::c_void;
|
2017-10-22 12:17:25 +00:00
|
|
|
use std::slice::from_raw_parts;
|
|
|
|
use stdweb;
|
|
|
|
use stdweb::Reference;
|
|
|
|
use stdweb::unstable::TryInto;
|
|
|
|
use stdweb::web::TypedArray;
|
2017-10-23 14:41:38 +00:00
|
|
|
use stdweb::web::set_timeout;
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-06-20 19:31:15 +00:00
|
|
|
use BuildStreamError;
|
2018-02-12 13:10:24 +00:00
|
|
|
use DefaultFormatError;
|
2019-06-20 22:53:11 +00:00
|
|
|
use DeviceNameError;
|
2019-06-20 20:37:36 +00:00
|
|
|
use DevicesError;
|
2017-10-18 18:24:05 +00:00
|
|
|
use Format;
|
2019-06-21 01:03:03 +00:00
|
|
|
use PauseStreamError;
|
|
|
|
use PlayStreamError;
|
2019-06-20 19:16:39 +00:00
|
|
|
use SupportedFormatsError;
|
2018-02-12 13:10:24 +00:00
|
|
|
use StreamData;
|
2019-12-14 10:50:57 +00:00
|
|
|
use StreamError;
|
2017-10-20 19:18:40 +00:00
|
|
|
use SupportedFormat;
|
2018-02-12 13:10:24 +00:00
|
|
|
use UnknownTypeOutputBuffer;
|
2019-12-14 10:50:57 +00:00
|
|
|
use traits::{DeviceTrait, HostTrait, StreamTrait};
|
|
|
|
|
|
|
|
// The emscripten backend currently works by instantiating an `AudioContext` object per `Stream`.
|
|
|
|
// Creating a stream creates a new `AudioContext`. Destroying a stream destroys it. Creation of a
|
|
|
|
// `Host` instance initializes the `stdweb` context.
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-06-24 20:38:48 +00:00
|
|
|
/// The default emscripten host type.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Host;
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
/// Content is false if the iterator is empty.
|
|
|
|
pub struct Devices(bool);
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct Device;
|
|
|
|
|
|
|
|
pub struct Stream {
|
|
|
|
// A reference to an `AudioContext` object.
|
|
|
|
audio_ctxt_ref: Reference,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index within the `streams` array of the events loop.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct StreamId(usize);
|
|
|
|
|
|
|
|
pub type SupportedInputFormats = ::std::vec::IntoIter<SupportedFormat>;
|
|
|
|
pub type SupportedOutputFormats = ::std::vec::IntoIter<SupportedFormat>;
|
|
|
|
|
2019-06-24 20:38:48 +00:00
|
|
|
impl Host {
|
|
|
|
pub fn new() -> Result<Self, crate::HostUnavailable> {
|
2019-12-14 10:50:57 +00:00
|
|
|
stdweb::initialize();
|
2019-06-24 20:38:48 +00:00
|
|
|
Ok(Host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
impl Devices {
|
|
|
|
fn new() -> Result<Self, DevicesError> {
|
|
|
|
Ok(Self::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Device {
|
|
|
|
#[inline]
|
|
|
|
fn name(&self) -> Result<String, DeviceNameError> {
|
|
|
|
Ok("Default Device".to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
|
|
|
|
// TODO: right now cpal's API doesn't allow flexibility here
|
|
|
|
// "44100" and "2" (channels) have also been hard-coded in the rest of the code ; if
|
|
|
|
// this ever becomes more flexible, don't forget to change that
|
|
|
|
// According to https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/createBuffer
|
|
|
|
// browsers must support 1 to 32 channels at leats and 8,000 Hz to 96,000 Hz.
|
|
|
|
//
|
|
|
|
// UPDATE: We can do this now. Might be best to use `crate::COMMON_SAMPLE_RATES` and
|
|
|
|
// filter out those that lay outside the range specified above.
|
|
|
|
Ok(
|
|
|
|
vec![
|
|
|
|
SupportedFormat {
|
|
|
|
channels: 2,
|
|
|
|
min_sample_rate: ::SampleRate(44100),
|
|
|
|
max_sample_rate: ::SampleRate(44100),
|
|
|
|
data_type: ::SampleFormat::F32,
|
|
|
|
},
|
|
|
|
].into_iter(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_input_format(&self) -> Result<Format, DefaultFormatError> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_output_format(&self) -> Result<Format, DefaultFormatError> {
|
|
|
|
// TODO: because it is hard coded, see supported_output_formats.
|
|
|
|
Ok(
|
|
|
|
Format {
|
|
|
|
channels: 2,
|
|
|
|
sample_rate: ::SampleRate(44100),
|
|
|
|
data_type: ::SampleFormat::F32,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:38:48 +00:00
|
|
|
impl HostTrait for Host {
|
|
|
|
type Devices = Devices;
|
|
|
|
type Device = Device;
|
|
|
|
|
|
|
|
fn is_available() -> bool {
|
2019-06-25 14:26:27 +00:00
|
|
|
// Assume this host is always available on emscripten.
|
2019-06-24 20:38:48 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn devices(&self) -> Result<Self::Devices, DevicesError> {
|
|
|
|
Devices::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_input_device(&self) -> Option<Self::Device> {
|
|
|
|
default_input_device()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_output_device(&self) -> Option<Self::Device> {
|
|
|
|
default_output_device()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceTrait for Device {
|
|
|
|
type SupportedInputFormats = SupportedInputFormats;
|
|
|
|
type SupportedOutputFormats = SupportedOutputFormats;
|
2019-12-14 10:50:57 +00:00
|
|
|
type Stream = Stream;
|
2019-06-24 20:38:48 +00:00
|
|
|
|
|
|
|
fn name(&self) -> Result<String, DeviceNameError> {
|
|
|
|
Device::name(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supported_input_formats(&self) -> Result<Self::SupportedInputFormats, SupportedFormatsError> {
|
|
|
|
Device::supported_input_formats(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supported_output_formats(&self) -> Result<Self::SupportedOutputFormats, SupportedFormatsError> {
|
|
|
|
Device::supported_output_formats(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_input_format(&self) -> Result<Format, DefaultFormatError> {
|
|
|
|
Device::default_input_format(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_output_format(&self) -> Result<Format, DefaultFormatError> {
|
|
|
|
Device::default_output_format(self)
|
|
|
|
}
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
fn build_input_stream<D, E>(
|
2019-06-24 20:38:48 +00:00
|
|
|
&self,
|
2019-12-14 10:50:57 +00:00
|
|
|
_format: &Format,
|
|
|
|
_data_callback: D,
|
|
|
|
_error_callback: E,
|
|
|
|
) -> Result<Self::Stream, BuildStreamError>
|
|
|
|
where
|
|
|
|
D: FnMut(StreamData) + Send + 'static,
|
|
|
|
E: FnMut(StreamError) + Send + 'static,
|
|
|
|
{
|
|
|
|
unimplemented!()
|
2019-06-24 20:38:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
fn build_output_stream<D, E>(
|
2019-06-24 20:38:48 +00:00
|
|
|
&self,
|
2019-12-14 10:50:57 +00:00
|
|
|
_format: &Format,
|
|
|
|
data_callback: D,
|
|
|
|
error_callback: E,
|
|
|
|
) -> Result<Self::Stream, BuildStreamError>
|
2019-06-24 20:38:48 +00:00
|
|
|
where
|
2019-12-14 10:50:57 +00:00
|
|
|
D: FnMut(StreamData) + Send + 'static,
|
|
|
|
E: FnMut(StreamError) + Send + 'static,
|
2019-06-24 20:38:48 +00:00
|
|
|
{
|
2019-12-14 10:50:57 +00:00
|
|
|
// Create the stream.
|
|
|
|
let audio_ctxt_ref = js!(return new AudioContext()).into_reference().unwrap();
|
|
|
|
let stream = Stream { audio_ctxt_ref };
|
2019-06-24 20:38:48 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
// Specify the callback.
|
|
|
|
let mut user_data = (self, data_callback, error_callback);
|
|
|
|
let user_data_ptr = &mut user_data as *mut (_, _, _);
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
// Use `set_timeout` to invoke a Rust callback repeatedly.
|
|
|
|
//
|
|
|
|
// The job of this callback is to fill the content of the audio buffers.
|
|
|
|
//
|
|
|
|
// See also: The call to `set_timeout` at the end of the `audio_callback_fn` which creates
|
|
|
|
// the loop.
|
|
|
|
set_timeout(|| audio_callback_fn::<D, E>(user_data_ptr as *mut c_void), 10);
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
Ok(stream)
|
|
|
|
}
|
2017-10-22 12:17:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
impl StreamTrait for Stream {
|
|
|
|
fn play(&self) -> Result<(), PlayStreamError> {
|
|
|
|
let audio_ctxt = &self.audio_ctxt_ref;
|
|
|
|
js!(@{audio_ctxt}.resume());
|
|
|
|
Ok(())
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
fn pause(&self) -> Result<(), PauseStreamError> {
|
|
|
|
let audio_ctxt = &self.audio_ctxt_ref;
|
|
|
|
js!(@{audio_ctxt}.suspend());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 12:17:25 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
// The first argument of the callback function (a `void*`) is a casted pointer to `self`
|
|
|
|
// and to the `callback` parameter that was passed to `run`.
|
|
|
|
fn audio_callback_fn<D, E>(user_data_ptr: *mut c_void)
|
|
|
|
where
|
|
|
|
D: FnMut(StreamData) + Send + 'static,
|
|
|
|
E: FnMut(StreamError) + Send + 'static,
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let user_data_ptr2 = user_data_ptr as *mut (&Stream, D, E);
|
|
|
|
let user_data = &mut *user_data_ptr2;
|
|
|
|
let (ref stream, ref mut data_cb, ref mut _err_cb) = user_data;
|
|
|
|
let audio_ctxt = &stream.audio_ctxt_ref;
|
|
|
|
|
|
|
|
// TODO: We should be re-using a buffer.
|
|
|
|
let mut temporary_buffer = vec![0.0; 44100 * 2 / 3];
|
2017-10-22 12:17:25 +00:00
|
|
|
|
|
|
|
{
|
2019-12-14 10:50:57 +00:00
|
|
|
let buffer = UnknownTypeOutputBuffer::F32(::OutputBuffer { buffer: &mut temporary_buffer });
|
|
|
|
let data = StreamData::Output { buffer: buffer };
|
|
|
|
data_cb(data);
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2017-10-22 12:17:25 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
// TODO: directly use a TypedArray<f32> once this is supported by stdweb
|
|
|
|
let typed_array = {
|
|
|
|
let f32_slice = temporary_buffer.as_slice();
|
|
|
|
let u8_slice: &[u8] = from_raw_parts(
|
|
|
|
f32_slice.as_ptr() as *const _,
|
|
|
|
f32_slice.len() * mem::size_of::<f32>(),
|
|
|
|
);
|
|
|
|
let typed_array: TypedArray<u8> = u8_slice.into();
|
|
|
|
typed_array
|
2017-10-18 18:24:05 +00:00
|
|
|
};
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
let num_channels = 2u32; // TODO: correct value
|
|
|
|
debug_assert_eq!(temporary_buffer.len() % num_channels as usize, 0);
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
js!(
|
|
|
|
var src_buffer = new Float32Array(@{typed_array}.buffer);
|
|
|
|
var context = @{audio_ctxt};
|
|
|
|
var buf_len = @{temporary_buffer.len() as u32};
|
|
|
|
var num_channels = @{num_channels};
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
var buffer = context.createBuffer(num_channels, buf_len / num_channels, 44100);
|
|
|
|
for (var channel = 0; channel < num_channels; ++channel) {
|
|
|
|
var buffer_content = buffer.getChannelData(channel);
|
|
|
|
for (var i = 0; i < buf_len / num_channels; ++i) {
|
|
|
|
buffer_content[i] = src_buffer[i * num_channels + channel];
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
var node = context.createBufferSource();
|
|
|
|
node.buffer = buffer;
|
|
|
|
node.connect(context.destination);
|
|
|
|
node.start();
|
|
|
|
);
|
2019-06-20 20:37:36 +00:00
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
// TODO: handle latency better ; right now we just use setInterval with the amount of sound
|
|
|
|
// data that is in each buffer ; this is obviously bad, and also the schedule is too tight
|
|
|
|
// and there may be underflows
|
|
|
|
set_timeout(|| audio_callback_fn::<D, E>(user_data_ptr), 330);
|
2019-06-20 20:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
impl Default for Devices {
|
|
|
|
fn default() -> Devices {
|
2017-10-18 18:24:05 +00:00
|
|
|
// We produce an empty iterator if the WebAudio API isn't available.
|
2018-02-12 13:10:24 +00:00
|
|
|
Devices(is_webaudio_available())
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-12 13:10:24 +00:00
|
|
|
impl Iterator for Devices {
|
|
|
|
type Item = Device;
|
2017-10-18 18:24:05 +00:00
|
|
|
#[inline]
|
2018-02-12 13:10:24 +00:00
|
|
|
fn next(&mut self) -> Option<Device> {
|
2017-10-18 18:24:05 +00:00
|
|
|
if self.0 {
|
|
|
|
self.0 = false;
|
2018-02-12 13:10:24 +00:00
|
|
|
Some(Device)
|
2017-10-18 18:24:05 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-24 20:38:48 +00:00
|
|
|
fn default_input_device() -> Option<Device> {
|
2018-02-12 13:10:24 +00:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-24 20:38:48 +00:00
|
|
|
fn default_output_device() -> Option<Device> {
|
2017-10-18 18:24:05 +00:00
|
|
|
if is_webaudio_available() {
|
2018-02-12 13:10:24 +00:00
|
|
|
Some(Device)
|
2017-10-18 18:24:05 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 10:50:57 +00:00
|
|
|
// Detects whether the `AudioContext` global variable is available.
|
|
|
|
fn is_webaudio_available() -> bool {
|
|
|
|
stdweb::initialize();
|
|
|
|
js!(if (!AudioContext) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}).try_into()
|
|
|
|
.unwrap()
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|