write_wav/write_wav.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_resource::io::FsResourceIo;
22use fyrox_sound::buffer::SoundBufferResourceExtension;
23use fyrox_sound::engine::State;
24use fyrox_sound::{
25 buffer::{DataSource, SoundBufferResource},
26 context::SoundContext,
27 engine::SoundEngine,
28 pool::Handle,
29 source::{SoundSource, SoundSourceBuilder, Status},
30};
31
32fn main() {
33 // Initialize sound engine without output device.
34 let engine = SoundEngine::without_device();
35
36 // Create new context.
37 let context = SoundContext::new();
38
39 // Register context in the engine.
40 engine.state().add_context(context.clone());
41
42 // Load sound buffer.
43 let door_open_buffer = SoundBufferResource::new_generic(
44 fyrox_sound::futures::executor::block_on(DataSource::from_file(
45 "examples/data/door_open.wav",
46 // Load from the default resource io (File system)
47 &FsResourceIo,
48 ))
49 .unwrap(),
50 )
51 .unwrap();
52
53 // Create generic source (without spatial effects) using that buffer.
54 let source = SoundSourceBuilder::new()
55 .with_buffer(door_open_buffer)
56 .with_status(Status::Playing)
57 .build()
58 .unwrap();
59
60 // Each sound sound must be added to context, context takes ownership on source
61 // and returns pool handle to it by which it can be accessed later on if needed.
62 let _source_handle: Handle<SoundSource> = context.state().add_source(source);
63
64 // Create output wav file. The sample rate is currently fixed.
65 let wav_spec = hound::WavSpec {
66 channels: 2,
67 sample_rate: fyrox_sound::context::SAMPLE_RATE,
68 bits_per_sample: 32,
69 sample_format: hound::SampleFormat::Float,
70 };
71 let mut wav_writer = hound::WavWriter::create("output.wav", wav_spec).unwrap();
72
73 // Create an output buffer.
74 let buf_len = State::render_buffer_len();
75 let mut buf = vec![(0.0f32, 0.0f32); buf_len];
76 let mut samples_written = 0;
77
78 // Wait until sound will play completely.
79 while samples_written < 3 * fyrox_sound::context::SAMPLE_RATE {
80 engine.state().render(&mut buf);
81 for &(l, r) in buf.iter() {
82 wav_writer.write_sample(l).unwrap();
83 wav_writer.write_sample(r).unwrap();
84 }
85 samples_written += buf_len as u32;
86 }
87
88 wav_writer.finalize().unwrap();
89}