Skip to content

Commit ef0ebf5

Browse files
committed
Make sinewave example work with cargo dinghy
1 parent 0ba8cfe commit ef0ebf5

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ audio_unit = ["audio_toolbox"]
3232

3333
# Unsupported
3434
open_al = []
35+
[target.'cfg(target_os = "macos")'.dependencies]
36+
libc = "0.2"
3537

3638
[dependencies]
3739
bitflags = "2.10"
38-
libc = "0.2"
3940
objc2-core-foundation = { version = "0.3", optional = true, default-features = false, features = [
4041
"std",
4142
"CFString",
@@ -67,6 +68,8 @@ objc2-core-audio-types = { version = "0.3", optional = true, default-features =
6768
"bitflags",
6869
"CoreAudioBaseTypes",
6970
] }
71+
[dev-dependencies]
72+
objc2-avf-audio = "0.3.2"
7073

7174
[package.metadata.docs.rs]
7275
all-features = true

examples/sine.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,36 @@ impl Iterator for SineWaveGenerator {
3434
}
3535

3636
fn main() -> Result<(), coreaudio::Error> {
37+
#[cfg(any(target_os = "ios", target_os = "visionos", target_os = "tvos"))]
38+
unsafe {
39+
let session = objc2_avf_audio::AVAudioSession::sharedInstance();
40+
41+
#[cfg(target_os = "tvos")]
42+
let _ = session.setCategory_error(
43+
objc2_avf_audio::AVAudioSessionCategoryPlayback.expect("Failed to get option")
44+
);
45+
#[cfg(any(target_os = "ios", target_os = "tvos"))]
46+
let _ = session.setCategory_withOptions_error(
47+
objc2_avf_audio::AVAudioSessionCategoryPlayAndRecord.expect("Failed to get options"),
48+
objc2_avf_audio::AVAudioSessionCategoryOptions::DefaultToSpeaker,
49+
);
50+
51+
}
3752
let frequency_hz = 440.;
3853
let volume = 0.15;
3954
let mut samples = SineWaveGenerator::new(frequency_hz, volume);
4055

4156
// Construct an Output audio unit that delivers audio to the default output device.
57+
#[cfg(target_os = "macos")]
4258
let mut audio_unit = AudioUnit::new(IOType::DefaultOutput)?;
4359

60+
#[cfg(any(target_os = "ios", target_os = "visionos", target_os = "tvos"))]
61+
let mut audio_unit = AudioUnit::new(coreaudio::audio_unit::IOType::RemoteIO)?;
62+
63+
// iOS/tvOS/visionOS don't let you reconfigure an "initialized" audio unit, so uninitialize it
64+
#[cfg(any(target_os = "ios", target_os = "visionos", target_os = "tvos"))]
65+
audio_unit.uninitialize()?;
66+
4467
// Read the input format. This is counterintuitive, but it's the format used when sending
4568
// audio data to the AudioUnit representing the output device. This is separate from the
4669
// format the AudioUnit later uses to send the data to the hardware device.
@@ -65,9 +88,13 @@ fn main() -> Result<(), coreaudio::Error> {
6588
}
6689
Ok(())
6790
})?;
91+
#[cfg(any(target_os = "ios", target_os = "visionos", target_os = "tvos"))]
92+
audio_unit.initialize()?;
93+
6894
audio_unit.start()?;
95+
println!("Audio started - playing 440Hz sine wave");
6996

70-
std::thread::sleep(std::time::Duration::from_millis(3000));
97+
std::thread::sleep(std::time::Duration::from_millis(10_000));
7198

7299
Ok(())
73100
}

0 commit comments

Comments
 (0)