@@ -346,31 +346,59 @@ where Self::Channel: AudioFormatNum + 'static
346
346
/// All format types are returned as native-endian.
347
347
pub trait AudioFormatNum {
348
348
fn audio_format ( ) -> AudioFormat ;
349
+
350
+ /// The appropriately typed silence value for the audio format used.
351
+ ///
352
+ /// # Examples
353
+ ///
354
+ /// ```
355
+ /// // The AudioFormatNum trait has to be imported for the Channel::SILENCE part to work.
356
+ /// use sdl2::audio::{AudioCallback, AudioFormatNum};
357
+ ///
358
+ /// struct Silence;
359
+ ///
360
+ /// impl AudioCallback for Silence {
361
+ /// type Channel = u16;
362
+ ///
363
+ /// fn callback(&mut self, out: &mut [u16]) {
364
+ /// for dst in out.iter_mut() {
365
+ /// *dst = Self::Channel::SILENCE;
366
+ /// }
367
+ /// }
368
+ /// }
369
+ /// ```
370
+ const SILENCE : Self ;
349
371
}
350
372
351
373
/// `AUDIO_S8`
352
374
impl AudioFormatNum for i8 {
353
375
fn audio_format ( ) -> AudioFormat { AudioFormat :: S8 }
376
+ const SILENCE : i8 = 0 ;
354
377
}
355
378
/// `AUDIO_U8`
356
379
impl AudioFormatNum for u8 {
357
380
fn audio_format ( ) -> AudioFormat { AudioFormat :: U8 }
381
+ const SILENCE : u8 = 0x80 ;
358
382
}
359
383
/// `AUDIO_S16`
360
384
impl AudioFormatNum for i16 {
361
385
fn audio_format ( ) -> AudioFormat { AudioFormat :: s16_sys ( ) }
386
+ const SILENCE : i16 = 0 ;
362
387
}
363
388
/// `AUDIO_U16`
364
389
impl AudioFormatNum for u16 {
365
390
fn audio_format ( ) -> AudioFormat { AudioFormat :: u16_sys ( ) }
391
+ const SILENCE : u16 = 0x8000 ;
366
392
}
367
393
/// `AUDIO_S32`
368
394
impl AudioFormatNum for i32 {
369
395
fn audio_format ( ) -> AudioFormat { AudioFormat :: s32_sys ( ) }
396
+ const SILENCE : i32 = 0 ;
370
397
}
371
398
/// `AUDIO_F32`
372
399
impl AudioFormatNum for f32 {
373
400
fn audio_format ( ) -> AudioFormat { AudioFormat :: f32_sys ( ) }
401
+ const SILENCE : f32 = 0.0 ;
374
402
}
375
403
376
404
extern "C" fn audio_callback_marshall < CB : AudioCallback >
@@ -473,6 +501,10 @@ pub struct AudioSpec {
473
501
pub freq : i32 ,
474
502
pub format : AudioFormat ,
475
503
pub channels : u8 ,
504
+ /// The silence value calculated by SDL2. Note that it's inconvenient to use if your channel
505
+ /// type is not u8 and [incorrect in case of u16](https://bugzilla.libsdl.org/show_bug.cgi?id=4805).
506
+ /// You're likely to find [the `AudioFormatNum.SILENCE` associated constant](
507
+ /// trait.AudioFormatNum.html#associatedconstant.SILENCE) more useful.
476
508
pub silence : u8 ,
477
509
pub samples : u16 ,
478
510
pub size : u32
0 commit comments