Skip to main content

raw_streaming/
raw_streaming.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use fyrox_sound::buffer::SoundBufferResourceExtension;
22use fyrox_sound::{
23    buffer::{DataSource, RawStreamingDataSource, SoundBufferResource},
24    context::SoundContext,
25    engine::SoundEngine,
26    source::{SoundSourceBuilder, Status},
27};
28use std::{thread, time::Duration};
29
30#[derive(Debug)]
31struct SamplesGenerator {
32    sample_rate: usize,
33    frequency: f32,
34    amplitude: f32,
35    index: usize,
36}
37
38impl SamplesGenerator {
39    pub fn new() -> Self {
40        Self {
41            sample_rate: 44100,
42            frequency: 440.0,
43            amplitude: 0.75,
44            index: 0,
45        }
46    }
47}
48
49impl Iterator for SamplesGenerator {
50    type Item = f32;
51
52    fn next(&mut self) -> Option<Self::Item> {
53        let sample = self.amplitude
54            * ((2.0 * std::f32::consts::PI * self.index as f32 * self.frequency)
55                / self.sample_rate as f32)
56                .sin();
57
58        self.index += 1;
59
60        Some(sample)
61    }
62}
63
64impl RawStreamingDataSource for SamplesGenerator {
65    fn sample_rate(&self) -> usize {
66        self.sample_rate
67    }
68
69    fn channel_count(&self) -> usize {
70        1
71    }
72}
73
74fn main() {
75    // Initialize sound engine with default output device.
76    let engine = SoundEngine::new().unwrap();
77
78    // Initialize new sound context.
79    let context = SoundContext::new();
80
81    engine.state().add_context(context.clone());
82
83    // Create sine wave generator
84    let sine_wave = DataSource::RawStreaming(Box::new(SamplesGenerator::new()));
85
86    let sine_wave_buffer = SoundBufferResource::new_streaming(sine_wave).unwrap();
87
88    // Create generic source (without spatial effects) using that buffer.
89    let source = SoundSourceBuilder::new()
90        .with_buffer(sine_wave_buffer)
91        .with_status(Status::Playing)
92        .build()
93        .unwrap();
94
95    context.state().add_source(source);
96
97    // Play sound for some time.
98    thread::sleep(Duration::from_secs(10));
99}