aym/lib.rs
1#![no_std]
2mod backends;
3
4pub use backends::AymPrecise;
5
6use core::fmt::Debug;
7use num_traits::Num;
8
9/// AY/YM Sound chip register count
10pub const AY_REGISTER_COUNT: usize = 14;
11
12/// Samples, returned by `aym` implement this trait; It allows to correctly convert
13/// the sound sample between different types. e.g.
14/// [to_f32](AySample::to_f32)/[to_f64](AySample::to_f64) will always return sample in the
15/// correct range `[-1.0; 1.0]`, while for example [to_i8](AySample::to_i8)
16/// will return the sound sample in range `[-128; 127]`
17pub trait AySample: Num + Copy {
18 // Returns sound sample in range `[i8::MIN; i8::MAX]`
19 fn to_i8(self) -> i8;
20 // Returns sound sample in range `[i16::MIN; i16::MAX]`
21 fn to_i16(self) -> i16;
22 // Returns sound sample in range `[i32::MIN; i32::MAX]`
23 fn to_i32(self) -> i32;
24 // Returns sound sample in range `[-1.0; 1.0]`
25 fn to_f32(self) -> f32;
26 // Returns sound sample in range `[-1.0; 1.0]`
27 fn to_f64(self) -> f64;
28}
29
30impl AySample for f64 {
31 fn to_i8(self) -> i8 {
32 (i8::MAX as f64 * self).clamp(i8::MIN as f64, i8::MAX as f64) as i8
33 }
34
35 fn to_i16(self) -> i16 {
36 (i16::MAX as f64 * self).clamp(i16::MIN as f64, i16::MAX as f64) as i16
37 }
38
39 fn to_i32(self) -> i32 {
40 (i32::MAX as f64 * self).clamp(i32::MIN as f64, i32::MAX as f64) as i32
41 }
42
43 fn to_f32(self) -> f32 {
44 self as f32
45 }
46
47 fn to_f64(self) -> f64 {
48 self
49 }
50}
51
52/// Represents AY stereo sample with `left` and `right` channel samples
53#[derive(Debug)]
54pub struct StereoSample<S>
55where
56 S: AySample + Debug,
57{
58 pub left: S,
59 pub right: S,
60}
61
62/// Sound chip type
63#[derive(Debug)]
64pub enum SoundChip {
65 /// AY-3-8910 sound chip
66 AY,
67 /// YM2149 sound chip
68 YM,
69}
70
71/// Stereo configuration
72///
73/// `Both` - Played on both channels
74/// `Left` - Played only on left channel
75/// `Right` - Played only on right channel
76///
77/// | Mode | A | B | C |
78/// | ---- | ----- | ----- | ----- |
79/// | Mono | Both | Both | Both |
80/// | ABC | Left | Both | Right |
81/// | ACB | Left | Right | Both |
82/// | BAC | Both | Left | Right |
83/// | BCA | Right | Left | Both |
84/// | CAB | Both | Right | Left |
85/// | CBA | Right | Both | Left |
86#[derive(Debug)]
87#[allow(clippy::upper_case_acronyms)]
88pub enum AyMode {
89 Mono,
90 ABC,
91 ACB,
92 BAC,
93 BCA,
94 CAB,
95 CBA,
96}
97
98/// Sound library generation backend.
99///
100/// Currently is only one backend - [AymPrecise],
101pub trait AymBackend: Sized {
102 /// Resulting sample type
103 type SoundSample: AySample + Debug;
104
105 /// Creates new aym instance.
106 ///
107 /// `frequency` - frequency of the sound chip in Hz
108 /// `sample_rate` - target device sound sample rate
109 fn new(chip: SoundChip, mode: AyMode, frequency: usize, sample_rate: usize) -> Self;
110 /// Write value to the sound chip register. `address` should be in `[0..AY_REGISTER_COUNT]`
111 fn write_register(&mut self, address: u8, value: u8);
112 /// Generates next sound sample
113 fn next_sample(&mut self) -> StereoSample<Self::SoundSample>;
114}