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

Implement input options #136

Merged
merged 8 commits into from
Apr 8, 2022

Conversation

ork821
Copy link
Contributor

@ork821 ork821 commented Apr 5, 2022

Partial implement of #51

Copy link
Owner

@orottier orottier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ork821, thanks for contributing! Could you have a look at my comments?
Also the CI checks fail, could you run cargo fmt and commit those changes too?
Are you planning to add latency_hint too in this PR?

src/media/mic.rs Outdated
///
/// Field is optional and will be set to hardcoded value.
#[derive(Clone, Debug, Default)]
pub struct MicrophoneInitOptions {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the name MicrophoneOptions would be more consistent with AudioContextOptions

src/media/mic.rs Outdated Show resolved Hide resolved
src/io.rs Outdated
Some(sample_rate)=> supported_configs_range
.next()
.expect("no supported config?!")
.with_sample_rate(CpalSampleRate(sample_rate)),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting a specific sample rate should be done after determining the fallback config.
The fallback config is put in the variable default_config and should only contain values that were actually supplied by cpal, so they should never fail.
Could you try to set the requested sample rate only on the config settings?

@ork821 ork821 closed this Apr 6, 2022
@ork821 ork821 reopened this Apr 6, 2022
@ork821
Copy link
Contributor Author

ork821 commented Apr 6, 2022

Hi, @orottier! I appreciate you for commenting my first contribution and my first Rust open source experience!
I just have pushed fixed code according to your comments.
As for adding latency_hint, I think it would be better to merge PR only with sample rate option.
At first sight, I can't exactly say how to implement this. So you can merge this PR and after that start researching about implementing latency_hint

src/io.rs Outdated
@@ -425,6 +427,10 @@ pub fn build_input() -> (Stream, StreamConfig, Receiver<AudioBuffer>) {

let mut config: StreamConfig = supported_config.into();
config.buffer_size = cpal::BufferSize::Fixed(input_buffer_size);
if options.sample_rate.is_some() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written better as:

if let Some(sample_rate) = options.sample_rate {
     // do stuff with sample_rate
}

This avoids the panic path of the unwrap call. See also https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html

@orottier
Copy link
Owner

orottier commented Apr 7, 2022

This is going in the right direction :)

I left one nitpick on your code, please have a look.

Also, the build is still failing, you can check it locally with cargo clippy --all-targets
It says: error: this argument is passed by value, but not consumed in the function body which I think is a false positive and can be ignored with #[allow(clippy::needless_pass_by_value)] similar to https://github.com/orottier/web-audio-api-rs/blob/main/src/context/online.rs#L103

After that we can merge and leave the latency for another time

@orottier
Copy link
Owner

orottier commented Apr 7, 2022

If you'd like I can get you started with some other issues, maybe #44 or #101

@ork821
Copy link
Contributor Author

ork821 commented Apr 7, 2022

This is going in the right direction :)

I left one nitpick on your code, please have a look.

I fixed this. Thank you for your help. This is very important for my skills.

Also, the build is still failing, you can check it locally with cargo clippy --all-targets It says: error: this argument is passed by value, but not consumed in the function body which I think is a false positive and can be ignored with #[allow(clippy::needless_pass_by_value)] similar to https://github.com/orottier/web-audio-api-rs/blob/main/src/context/online.rs#L103

Also fixed this. cargo build, cargo test and cargo clippy --all-targets runs without warnings.

As for other issues, maybe I would try #101. Or will find another issue to fix :)

@orottier
Copy link
Owner

orottier commented Apr 8, 2022

Great. You forgot about cargo fmt though, but I fixed it for you + an unrelated fix because rust 1.60 was released yesterday.

orottier added 2 commits April 8, 2022 08:58
Add a note that we currently ignore the value.
Since the constructor options are now identical for the AudioContext, we
can use the AudioBufferOptions for the mic too.
@orottier
Copy link
Owner

orottier commented Apr 8, 2022

@ork821 Since we are nearing a v1 release we need to take care of API stability.
Hence I added the latency_hint option for the mic too (but it is left unimplemented). We can fix this within the v1 version
Please have a look at my latest commit :)

@ork821
Copy link
Contributor Author

ork821 commented Apr 8, 2022

@ork821 Since we are nearing a v1 release we need to take care of API stability. Hence I added the latency_hint option for the mic too (but it is left unimplemented). We can fix this within the v1 version Please have a look at my latest commit :)

Hi, @orottier. Thank you for fixing my small cargo fmt fault.

I am not sure that usage same struct AudioContextOptions is good desition.
For example, according to https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/AudioContext we can have only latencyHint and sampleRate. But according to https://w3c.github.io/mediacapture-main/#dom-mediatrackconstraintset we can have echoCancellation, channelCount, deviceId (and so on) as input device options. If for now it's ok, we can merge.

@orottier
Copy link
Owner

orottier commented Apr 8, 2022

That's a valid point, however I would prefer to keep it this way, because either way we need breaking changes when we add the features that you mention to AudioInputOptions.
https://doc.rust-lang.org/cargo/reference/semver.html#struct-add-public-field-when-no-private

Let's op for a new constructor in the future
fn new_with_config(audio_opts: AudioContextOptions, mic_opts: MicrophoneOptions) -> Microphone
We can add all mic specific fields to MicrophoneOptions. The new constructor is not a breaking change so can be added within the v1 version. This gives us a bit of time to flesh out the complete featureset around mic input

@ork821
Copy link
Contributor Author

ork821 commented Apr 8, 2022

That's a valid point, however I would prefer to keep it this way, because either way we need breaking changes when we add the features that you mention to AudioInputOptions. https://doc.rust-lang.org/cargo/reference/semver.html#struct-add-public-field-when-no-private

Let's op for a new constructor in the future fn new_with_config(audio_opts: AudioContextOptions, mic_opts: MicrophoneOptions) -> Microphone We can add all mic specific fields to MicrophoneOptions. The new constructor is not a breaking change so can be added within the v1 version. This gives us a bit of time to flesh out the complete featureset around mic input

Yes, you are actually right about breaking changes. It would be better to implement mentioned method later if we need it. I think you can merge if everything is okay.

@orottier orottier merged commit c55b7c1 into orottier:main Apr 8, 2022
@ork821 ork821 deleted the feature/add_input_device_options branch April 8, 2022 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants