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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! Audio module, for playing sound effects and music

use hound;
use lewton;
use alto::{Alto, Context, DeviceObject, OutputDevice, Buffer, StaticSource, SourceState, Mono, Stereo};
use alto::Source as AltoSource;

use std::rc::Rc;
use std::cell::RefCell;
use std::sync::Arc;

use bit_set::BitSet;

use keeshond_datapack::{ReadSeek, DataId, DataStore, DataObject, DataError, DataPreparer, PreparedStore, PreparedStoreError, DataMultistore};
use keeshond_datapack::source::Source;

const NUM_SOURCES : usize = 32;

#[derive(Debug, Fail)]
pub enum AudioError
{
    #[fail(display = "OpenAL failed: {}", _0)]
    AlError(String),
    #[fail(display = "Failed to load resource: {}", _0)]
    LoadResourceFailed(String)
}

/// A resource representing a sound that can be played.
pub struct Sound
{
    frequency : u32,
    channels : u32,
    samples : Vec<i16>
}

impl Sound
{
    fn from_wav(reader: Box<dyn ReadSeek>) -> Result<Self, DataError>
    {
        let mut wav_reader = try_or_else!(hound::WavReader::new(reader),
            |error| { Err(DataError::BadData(format!("{}", error))) });

        let frequency = wav_reader.spec().sample_rate;
        let channels = wav_reader.spec().channels as u32;
        let mut samples = Vec::new();
        let mut sample_iter = wav_reader.samples::<i16>();

        loop
        {
            if let Some(packet_res) = sample_iter.next()
            {
                match packet_res
                {
                    Ok(packet) =>
                    {
                        samples.push(packet);
                    },
                    Err(error) =>
                    {
                        return Err(DataError::BadData(format!("{}", error)));
                    }
                }
            }
            else
            {
                break;
            }
        }

        Ok(Sound
        {
            frequency,
            channels,
            samples
        })
    }

    fn from_ogg(reader: Box<dyn ReadSeek>) -> Result<Self, DataError>
    {
        let mut ogg_reader = try_or_else!(lewton::inside_ogg::OggStreamReader::new(reader),
            |error| { Err(DataError::BadData(format!("{}", error))) });

        let frequency = ogg_reader.ident_hdr.audio_sample_rate;
        let channels = ogg_reader.ident_hdr.audio_channels as u32;
        let mut samples = Vec::new();

        loop
        {
            let packet_opt = try_or_else!(ogg_reader.read_dec_packet_itl(),
                |error| { Err(DataError::BadData(format!("{}", error))) });

            if let Some(mut packet) = packet_opt
            {
                samples.append(&mut packet);
            }
            else
            {
                break;
            }
        }

        Ok(Sound
        {
            frequency,
            channels,
            samples
        })
    }
}

impl DataObject for Sound
{
    fn folder_name() -> &'static str where Self : Sized
    {
        "sounds"
    }
    fn want_file(pathname : &str) -> bool where Self : Sized
    {
        pathname.ends_with(".wav") || pathname.ends_with(".ogg")
    }
    fn from_io(reader: Box<dyn ReadSeek>, full_pathname : &str, _source : &mut Box<dyn Source>) -> Result<Self, DataError> where Self : Sized
    {
        if full_pathname.ends_with(".wav")
        {
            return Sound::from_wav(reader);
        }
        else if full_pathname.ends_with(".ogg")
        {
            return Sound::from_ogg(reader);
        }

        Err(DataError::BadData("Unknown audio format".into()))
    }
}

struct AlSoundHandle
{
    buffer : Arc<Buffer>
}

struct AlSoundPreparer
{
    context : Rc<RefCell<Context>>
}

impl AlSoundPreparer
{
    fn new(context : Rc<RefCell<Context>>) -> AlSoundPreparer
    {
        AlSoundPreparer
        {
            context
        }
    }
}

impl DataPreparer<Sound, AlSoundHandle> for AlSoundPreparer
{
    fn prepare(&mut self, data : &mut Sound, _id : DataId) -> AlSoundHandle
    {
        let context_ref : &Context = &self.context.borrow();
        let buffer_raw;
        
        match data.channels
        {
            1 =>
            {
                buffer_raw = context_ref.new_buffer::<Mono<i16>, _>(&data.samples, data.frequency as i32).expect("OpenAL buffer creation failed");
            },
            2 =>
            {
                buffer_raw = context_ref.new_buffer::<Stereo<i16>, _>(&data.samples, data.frequency as i32).expect("OpenAL buffer creation failed");
            },
            _ =>
            {
                panic!("OpenAL sound with unexpected number of channels");
            }
        }
        
        let buffer = Arc::new(buffer_raw);
        
        data.samples = Vec::new();
        
        AlSoundHandle
        {
            buffer
        }
    }
    fn unprepare(&mut self, _prepared : &mut AlSoundHandle, _id : DataId)
    {
        
    }
}

/// Public interface to controlling audio in your game
pub trait Audio
{
    fn unit_scale(&self) -> f32;
    fn set_unit_scale(&mut self, scale : f32);
    fn listener_position(&self) -> (f32, f32);
    fn set_listener_position(&mut self, x : f32, y : f32);
    fn play_sound(&mut self, sound_id : DataId) -> Option<Voice>;
    fn play_sound_with(&mut self, sound_id : DataId, voice_info : &VoiceInfo) -> Option<Voice>;
    fn stop_voice(&mut self, voice : &Voice) -> bool;
    fn voice(&self, voice : &Voice) -> Option<VoiceInfo>;
    fn set_voice(&mut self, voice : &Voice, voice_info : &VoiceInfo) -> bool;
    fn max_voices(&self) -> usize;
    fn sync_sound_store(&mut self, sound_store : &mut DataStore<Sound>) -> Result<(), PreparedStoreError>;
}

/// A handle to a currently played sound
#[derive(Debug)]
pub struct Voice
{
    pub slot : usize,
    pub generation : u64
}

/// Information on how to play a sound
#[derive(Clone)]
pub struct VoiceInfo
{
    pub volume : f32,
    pub pitch : f32,
    pub position : (f32, f32),
    pub distance : f32,
    pub rolloff_start : f32,
    pub local : bool,
    pub looping : bool
}

impl Default for VoiceInfo
{
    fn default() -> VoiceInfo
    {
        VoiceInfo
        {
            volume : 1.0,
            pitch : 1.0,
            position : (0.0, 0.0),
            distance : 1.0,
            rolloff_start : 0.0,
            local : true,
            looping : false
        }
    }
}

/// (ADVANCED) Null audio backend, for automatic tests that don't need sound
pub struct NullAudio
{

}

impl NullAudio
{
    pub fn new() -> NullAudio
    {
        NullAudio {}
    }
}

impl Audio for NullAudio
{
    #[allow(unused_variables)]
    fn unit_scale(&self) -> f32 { 1.0 }
    #[allow(unused_variables)]
    fn set_unit_scale(&mut self, scale : f32) {}
    #[allow(unused_variables)]
    fn listener_position(&self) -> (f32, f32) { (0.0, 0.0) }
    #[allow(unused_variables)]
    fn set_listener_position(&mut self, x : f32, y : f32) {}
    #[allow(unused_variables)]
    fn play_sound(&mut self, sound_id : DataId) -> Option<Voice>
    {
        Some(Voice { slot : 0, generation : 0 })
    }
    #[allow(unused_variables)]
    fn play_sound_with(&mut self, sound_id : DataId, voice_info : &VoiceInfo) -> Option<Voice>
    {
        Some(Voice { slot : 0, generation : 0 })
    }
    #[allow(unused_variables)]
    fn stop_voice(&mut self, voice : &Voice) -> bool { true }
    #[allow(unused_variables)]
    fn voice(&self, voice : &Voice) -> Option<VoiceInfo> { None }
    #[allow(unused_variables)]
    fn set_voice(&mut self, voice : &Voice, voice_info : &VoiceInfo) -> bool { true }
    #[allow(unused_variables)]
    fn max_voices(&self) -> usize { 0 }
    #[allow(unused_variables)]
    fn sync_sound_store(&mut self, sound_store : &mut DataStore<Sound>) -> Result<(), PreparedStoreError>
    {
        Ok(())
    }
}

/// (ADVANCED) OpenAL audio backend
pub struct AlAudio
{
    #[allow(dead_code)] // In case it's needed
    alto : Alto,
    #[allow(dead_code)] // In case it's needed
    device : OutputDevice,
    #[allow(dead_code)] // In case it's needed
    context : Rc<RefCell<Context>>,
    unit_scale : f32,
    scale_multiplier : f32,
    listener_position : (f32, f32),
    sources : Vec<StaticSource>,
    voice_generations : Vec<u64>,
    voice_sounds : Vec<DataId>,
    voice_pending : BitSet<u32>,
    voice_info : Vec<VoiceInfo>,
    al_sound_store : PreparedStore<Sound, AlSoundHandle>
}

impl AlAudio
{
    pub fn new(resources : &mut DataMultistore) -> Result<AlAudio, AudioError>
    {
        let alto = try_or_else!(Alto::load_default(),
            |error| Err(AudioError::AlError(format!("{}", error))));
        
        let device = try_or_else!(alto.open(None),
            |error| Err(AudioError::AlError(format!("{}", error))));
        
        match device.specifier()
        {
            Some(specifier) =>
            {
                info!("OpenAL device: {}", specifier.to_str().unwrap_or("unknown"));
            },
            None => {}
        }
        
        let context_raw = try_or_else!(device.new_context(None),
            |error| Err(AudioError::AlError(format!("{}", error))));
        
        let mut sources = Vec::new();
        let mut voice_generations = Vec::new();
        let mut voice_sounds = Vec::new();
        let mut voice_info = Vec::new();
        
        for _ in 0..NUM_SOURCES
        {
            let source = try_or_else!(context_raw.new_static_source(),
                |error| Err(AudioError::AlError(format!("{}", error))));
            
            sources.push(source);
            voice_generations.push(0);
            voice_sounds.push(0);
            voice_info.push(VoiceInfo::default());
        }
        
        context_raw.set_distance_model(alto::DistanceModel::InverseClamped);
        
        let context = Rc::new(RefCell::new(context_raw));
        
        let sound_data_preparer = Box::new(AlSoundPreparer::new(context.clone()));
        let al_sound_store = try_or_else!(PreparedStore::new(&mut resources.store_mut::<Sound>(),
            sound_data_preparer),
            |error| Err(AudioError::LoadResourceFailed(format!("Failed to create OpenAL sound handle store: {}", error))));
        
        Ok(AlAudio
        {
            alto,
            device,
            context,
            unit_scale : 1.0,
            scale_multiplier : 1.0,
            listener_position : (0.0, 0.0),
            sources,
            voice_generations,
            voice_sounds,
            voice_pending : BitSet::with_capacity(NUM_SOURCES),
            voice_info,
            al_sound_store
        })
    }

    fn slot_playing(&self, slot : usize) -> bool
    {
        if slot >= self.sources.len()
        {
            return false;
        }

        let state = self.sources[slot].state();

        state == SourceState::Playing || state == SourceState::Paused || self.voice_pending.contains(slot)
    }
    
    fn find_voice(&mut self) -> Option<Voice>
    {
        for i in 0..self.sources.len()
        {
            if !self.slot_playing(i)
            {
                self.voice_generations[i] += 1;
                
                let voice = Voice
                {
                    slot : i,
                    generation : self.voice_generations[i]
                };
                
                return Some(voice);
            }
        }
        
        None
    }
    
    fn set_voice_internal(&mut self, source_id : usize, voice_info : &VoiceInfo)
    {
        let context_ref : &Context = &self.context.borrow();
        let _defer_lock = context_ref.defer_updates();
        let source = &mut self.sources[source_id];
        
        source.set_relative(voice_info.local);
        
        let (mut pos_x, mut pos_y) = voice_info.position;
        
        pos_x *= self.scale_multiplier;
        pos_y *= self.scale_multiplier;
        
        if let Err(error) = source.set_position([pos_x, pos_y, -1.0])
        {
            warn!("OpenAL source error: {}", error);
        }
        
        source.set_looping(voice_info.looping);
        if let Err(error) = source.set_gain(voice_info.volume)
        {
            warn!("OpenAL source error: {}", error);
        }
        if let Err(error) = source.set_pitch(voice_info.pitch)
        {
            warn!("OpenAL source error: {}", error);
        }
        if let Err(error) = source.set_reference_distance(voice_info.rolloff_start + 1.0)
        {
            warn!("OpenAL source error: {}", error);
        }
        
        let distance_multiplier;
        
        if voice_info.distance > 0.0 && voice_info.distance.is_normal()
        {
            distance_multiplier = 1.0 / voice_info.distance;
        }
        else
        {
            distance_multiplier = 1.0;
        }
        
        if let Err(error) = source.set_rolloff_factor(distance_multiplier)
        {
            warn!("OpenAL source error: {}", error);
        }
        
        self.voice_info[source_id] = voice_info.clone();
    }

    fn set_buffer_internal(&mut self, slot : usize)
    {
        let sound_id = self.voice_sounds[slot];

        if let Some(data) = self.al_sound_store.get_mut(sound_id)
        {
            let source = &mut self.sources[slot];

            source.stop();
            let buffer_result = source.set_buffer(data.buffer.clone());

            match buffer_result
            {
                Ok(_) =>
                {
                    self.sources[slot].play();
                },
                Err(error) =>
                {
                    warn!("OpenAL source bind failed: {}", error);
                }
            }
        }
    }
}

impl Audio for AlAudio
{
    fn unit_scale(&self) -> f32
    {
        self.unit_scale
    }
    
    fn set_unit_scale(&mut self, scale : f32)
    {
        if self.unit_scale == scale
        {
            return;
        }
        
        let context_ref : &Context = &self.context.borrow();
        let _defer_lock = context_ref.defer_updates();
        
        self.unit_scale = scale;
        
        if scale > 0.0 && scale.is_normal()
        {
            self.scale_multiplier = 1.0 / scale;
        }
        else
        {
            self.scale_multiplier = 1.0;
        }
        
        let mut warned_yet = false;
        
        for i in 0..self.voice_info.len()
        {
            let (mut pos_x, mut pos_y) = self.voice_info[i].position;
            
            pos_x *= self.scale_multiplier;
            pos_y *= self.scale_multiplier;
            
            let source = &mut self.sources[i];
            
            if let Err(error) = source.set_position([pos_x, pos_y, -1.0])
            {
                // Don't spam the log
                if !warned_yet
                {
                    warn!("OpenAL source error: {}", error);
                    warned_yet = true;
                }
            }
        }
        
        let (mut listener_x, mut listener_y) = self.listener_position;
        
        listener_x *= self.scale_multiplier;
        listener_y *= self.scale_multiplier;
        
        if let Err(error) = context_ref.set_position([listener_x, listener_y, 0.0])
        {
            warn!("OpenAL source error: {}", error);
        }
    }

    fn listener_position(&self) -> (f32, f32)
    {
        self.listener_position
    }
    
    fn set_listener_position(&mut self, x : f32, y : f32)
    {
        self.listener_position = (x, y);
        
        let listener_x = x * self.scale_multiplier;
        let listener_y = y * self.scale_multiplier;
        
        let context_ref : &Context = &self.context.borrow();
        
        if let Err(error) = context_ref.set_position([listener_x, listener_y, 0.0])
        {
            warn!("OpenAL source error: {}", error);
        }
    }
    
    fn play_sound(&mut self, sound_id : DataId) -> Option<Voice>
    {
        self.play_sound_with(sound_id, &VoiceInfo::default())
    }
    
    fn play_sound_with(&mut self, sound_id : DataId, voice_info : &VoiceInfo) -> Option<Voice>
    {
        let voice_result = self.find_voice();
        
        if let Some(voice) = &voice_result
        {
            self.voice_sounds[voice.slot] = sound_id;
            self.set_voice_internal(voice.slot, voice_info);

            if self.al_sound_store.get_mut(sound_id).is_some()
            {
                self.set_buffer_internal(voice.slot);
            }
            else
            {
                self.voice_pending.insert(voice.slot);
            }
        }
        
        voice_result
    }
    
    fn stop_voice(&mut self, voice : &Voice) -> bool
    {
        if voice.slot >= self.voice_generations.len()
        {
            return false;
        }
        
        if self.voice_generations[voice.slot] != voice.generation
        {
            return false;
        }
        
        if self.slot_playing(voice.slot)
        {
            let source = &mut self.sources[voice.slot];
            source.stop();
            self.voice_pending.remove(voice.slot);
            return true;
        }
        
        false
    }
    
    fn set_voice(&mut self, voice : &Voice, voice_info : &VoiceInfo) -> bool
    {
        if voice.slot >= self.voice_generations.len()
        {
            return false;
        }
        
        if self.voice_generations[voice.slot] != voice.generation
        {
            return false;
        }
        
        self.set_voice_internal(voice.slot, voice_info);
        
        true
    }
    
    fn voice(&self, voice : &Voice) -> Option<VoiceInfo>
    {
        if voice.slot >= self.voice_generations.len()
        {
            return None;
        }
        
        if self.voice_generations[voice.slot] != voice.generation
        {
            return None;
        }

        if self.slot_playing(voice.slot)
        {
            return Some(self.voice_info[voice.slot].clone());
        }
        
        None
    }
    
    fn max_voices(&self) -> usize
    {
        self.voice_info.len()
    }
    
    fn sync_sound_store(&mut self, sound_store : &mut DataStore<Sound>) -> Result<(), PreparedStoreError>
    {
        let result = self.al_sound_store.sync(sound_store);

        for slot in 0..self.voice_pending.len()
        {
            if self.voice_pending.contains(slot)
            {
                self.set_buffer_internal(slot);
            }
        }

        self.voice_pending.clear();

        result
    }
}