fmod_utils/
audition.rs

1//! FMOD System wrapper
2
3use log;
4use fmod::{self, ChannelControl};
5use vec_map::VecMap;
6
7use crate::{Sampler, Music, Voice, voice};
8
9/// A high-level interface to an FMOD System
10#[derive(Debug, PartialEq)]
11pub struct Audition {
12  pub system  : fmod::System,
13  pub sampler : Sampler,
14  pub music   : Music,
15  pub voices  : VecMap <Voice>
16}
17
18impl Audition {
19  /// Create with default FMOD system
20  pub fn new() -> Self {
21    Self::default()
22  }
23  /// Create with provided initialized FMOD system
24  #[inline]
25  pub fn with_system (system : fmod::System) -> Self {
26    let sampler = Sampler::new (&system);
27    let music   = Music::default();
28    let voices  = VecMap::new();
29    Audition { system, sampler, music, voices }
30  }
31  /// Sets the master volume on the master channel group
32  #[inline]
33  pub fn set_master_volume (&self, volume : f32) {
34    self.system.get_master_channel_group().unwrap().set_volume (volume).unwrap()
35  }
36  /// Load sound sample files with given filenames.
37  #[inline]
38  pub fn load_samples (&mut self, samples : VecMap <String>) {
39    self.sampler.load (samples);
40  }
41  /// Load sound sample files from memory.
42  #[inline]
43  pub fn load_samples_memory (&mut self, samples : VecMap <&[u8]>) {
44    self.sampler.load_memory (samples);
45  }
46  /// Load sound sample files from PCM data.
47  #[inline]
48  pub fn load_samples_pcm (&mut self, samples : VecMap <&[i16]>) {
49    self.sampler.load_pcm (samples);
50  }
51  /// Load standard MIDI files (.mid) with given soundfont file paths (.dls)
52  #[inline]
53  pub fn load_songs_midi (&mut self, songs : VecMap <(String, String)>) {
54    self.music = Music::load_midi (&mut self.system, songs);
55  }
56  /// Load standard MIDI files (.mid) from memory, with given soundfont file
57  /// paths (.dls)
58  #[inline]
59  pub fn load_songs_midi_memory (&mut self, songs : VecMap <(&[u8], String)>) {
60    self.music = Music::load_midi_memory (&mut self.system, songs);
61  }
62  pub fn new_voice (&mut self) -> voice::Id {
63    let z : voice::IndexType = 0;
64    for i in z.. {
65      if !self.voices.contains_key (i as usize) {
66        assert!(self.voices.insert (i as usize, Voice::default()).is_none());
67        return voice::Id (i)
68      }
69    }
70    unreachable!();
71  }
72  #[inline]
73  pub fn remove_voice (&mut self, voice_id : voice::Id) -> Option <Voice> {
74    self.voices.remove (voice_id.0 as usize)
75  }
76  /// Convennience method to call the `update()` method of the FMOD system
77  #[inline]
78  pub fn update (&mut self) {
79    self.system.update().unwrap();
80  }
81}
82
83impl Default for Audition {
84  fn default() -> Self {
85    let system = fmod::System::default().unwrap_or_else (|err|{
86      log::error!("failed to create FMOD system: {:?}", err);
87      panic!()
88    });
89    Audition::with_system (system)
90  }
91}