1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// The MIT License (MIT)
//
// Copyright (c) 2013 Jeremy Letang (letang.jeremy@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! The datas extracted from a sound file.

use std::mem;
use libc::c_void;
use std::vec::Vec;

use openal::{ffi, al};
use sndfile::{SndFile, SndInfo};
use sndfile::OpenMode::Read;
use internal::OpenAlData;
use audio_tags::{Tags, AudioTags, get_sound_tags};

/**
 * Samples extracted from a file.
 *
 * SoundDatas are made to be shared between several Sound and played in the same
 * time.
 *
 * # Example
 * ```no_run
 * extern crate ears;
 * use ears::{Sound, SoundData, AudioController};
 * use std::cell::RefCell;
 * use std::rc::Rc;
 *
 * fn main() -> () {
 *   // Create a SoundData
 *   let snd_data = Rc::new(RefCell::new(SoundData::new("path/to/my/sound.wav")
 *                                       .unwrap()));
 *
 *   // Create two Sound with the same SoundData
 *   let mut snd1 = Sound::new_with_data(snd_data.clone()).unwrap();
 *   let mut snd2 = Sound::new_with_data(snd_data.clone()).unwrap();
 *
 *   // Play the sounds
 *   snd1.play();
 *   snd2.play();
 *
 *   // Wait until snd2 is playing
 *   while snd2.is_playing() {}
 * }
 * ```
 */
pub struct SoundData {
    /// The SoundTags who contains all the information of the sound
    sound_tags: Tags,
    /// The sndfile samples information
    snd_info: SndInfo,
    /// The total samples count of the Sound
    nb_sample: i64,
    /// The OpenAl internal identifier for the buffer
    al_buffer: u32
}

impl SoundData {
    /**
     * Create a new SoundData.
     *
     * The SoundData contains all the information extracted from the
     * file: samples and tags.
     * It's an easy way to share the same samples between man Sounds objects.
     *
     * # Arguments
     * * `path` - The path of the file to load
     *
     * # Return
     * An Option with Some(SoundData) if the SoundData is create, or None if
     * an error has occured.
     */
    pub fn new(path: &str) -> Option<SoundData> {
        check_openal_context!(None);

        let mut file;

        match SndFile::new(path, Read) {
            Ok(file_) => file = file_,
            Err(err) => { println!("{}", err); return None; }
        };

        let infos = file.get_sndinfo();

        let nb_sample = infos.channels as i64 * infos.frames;

        let mut samples = vec![0i16; nb_sample as usize];
        file.read_i16(&mut samples[..], nb_sample as i64);

        let mut buffer_id = 0;
        let len = mem::size_of::<i16>() * (samples.len());

        // Retrieve format informations
        let format =  match al::get_channels_format(infos.channels) {
            Some(fmt) => fmt,
            None => {
                println!("internal error : unrecognized format.");
                return None;
            }
        };

        al::alGenBuffers(1, &mut buffer_id);
        al::alBufferData(buffer_id,
                         format,
                         samples.as_ptr() as *mut c_void,
                         len as i32,
                         infos.samplerate);

        match al::openal_has_error() {
            Some(err)   => { println!("{}", err); return None; },
            None        => {}
        };

        let sound_data = SoundData {
            sound_tags  : get_sound_tags(&file),
            snd_info    : infos,
            nb_sample   : nb_sample,
            al_buffer   : buffer_id
        };
        file.close();

        Some(sound_data)
    }
}


/**
 * Get the sound file infos.
 *
 * # Return
 * The struct SndInfo.
 */
pub fn get_sndinfo<'r>(s_data: &'r SoundData) -> &'r SndInfo {
    &s_data.snd_info
}

/**
 * Get the OpenAL identifier of the samples buffer.
 *
 * # Return
 * The OpenAL internal identifier for the samples buffer of the sound.
 */
 #[doc(hidden)]
pub fn get_buffer(s_data: &SoundData) -> u32 {
    s_data.al_buffer
}

impl AudioTags for SoundData {
    /**
     * Get the tags of a Sound.
     *
     * # Return
     * A borrowed pointer to the internal struct SoundTags
     */
    fn get_tags(&self) -> Tags {
        self.sound_tags.clone()
    }
}

impl Drop for SoundData {
    /// Destroy all the resources attached to the SoundData
    fn drop(&mut self) -> () {
        unsafe {
            ffi::alDeleteBuffers(1, &mut self.al_buffer);
        }
    }
}

#[cfg(test)]
mod test {
    #![allow(non_snake_case)]

    #[allow(unused_variables)]
    use sound_data::SoundData;

    #[test]
    #[ignore]
    fn sounddata_create_OK() -> () {
        #![allow(unused_variables)]
        let snd_data = SoundData::new("res/shot.wav").unwrap();

    }

    #[test]
    #[ignore]
    #[should_panic]
    fn sounddata_create_FAIL() -> () {
        #![allow(unused_variables)]
        let snd_data = SoundData::new("toto.wav").unwrap();
    }
}