mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add volume control functions to the codec
This commit is contained in:
parent
8648a342fc
commit
beed5c395a
4 changed files with 163 additions and 0 deletions
|
|
@ -36,10 +36,27 @@ pub(crate) enum Opcode {
|
|||
/// send a frame ready notification
|
||||
AnotherFrame,
|
||||
|
||||
/// set speaker volume
|
||||
SetSpeakerVolume,
|
||||
|
||||
/// set headphone volume -- L&R channels are ganged together in this API, but codec can do separately
|
||||
SetHeadphoneVolume,
|
||||
|
||||
/// Suspend/resume callback
|
||||
SuspendResume,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub enum VolumeOps {
|
||||
UpOne,
|
||||
DownOne,
|
||||
Set,
|
||||
Mute,
|
||||
RestoreDefault,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub(crate) enum EventCallback {
|
||||
Event,
|
||||
|
|
|
|||
|
|
@ -30,6 +30,23 @@ pub struct Codec {
|
|||
|
||||
static SILENCE: [u32; FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; FIFO_DEPTH];
|
||||
|
||||
/// gain is specified in dB, and has a useful range from 0 to -80dB
|
||||
fn analog_volume_db_to_code(g: f32) -> u8 {
|
||||
if g >= 0.0 {
|
||||
0
|
||||
} else if g >= -17.5 {
|
||||
(-g * 2.0) as u8
|
||||
} else if g >= -34.6 {
|
||||
(-g * 2.0 - 0.4) as u8
|
||||
} else if g >= -49.3 {
|
||||
(100.0 - (50.0 + g) * 0.7202) as u8
|
||||
} else if g >= -72.2 {
|
||||
127
|
||||
} else {
|
||||
127
|
||||
}
|
||||
}
|
||||
|
||||
fn audio_handler(_irq_no: usize, arg: *mut usize) {
|
||||
let codec = unsafe { &mut *(arg as *mut Codec) };
|
||||
let volatile_audio = codec.fifo.as_mut_ptr() as *mut u32;
|
||||
|
|
@ -204,6 +221,37 @@ impl Codec {
|
|||
self.live
|
||||
}
|
||||
|
||||
pub fn set_speaker_gain_db(&mut self, gain_db: f32) {
|
||||
if gain_db <= -79.0 {
|
||||
// mute
|
||||
self.w(0, &[1]); // select page 1
|
||||
self.w(32, &[0b0_0_00011_0]); // class D amp powered off
|
||||
} else {
|
||||
let code = analog_volume_db_to_code(gain_db);
|
||||
self.w(0, &[1]); // select page 1
|
||||
self.w(32, &[0b1_0_00011_0]); // class D amp powered on
|
||||
self.w(38, &[
|
||||
0b1_000_0000 | code,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_headphone_gain_db(&mut self, gain_db: f32) {
|
||||
if gain_db <= -79.0 {
|
||||
// mute
|
||||
self.w(0, &[1]); // select page 1
|
||||
self.w(31, &[0b0_0_0_10_1_0_0]); // headphones powered down
|
||||
} else {
|
||||
let code = analog_volume_db_to_code(gain_db);
|
||||
self.w(0, &[1]); // select page 1
|
||||
self.w(31, &[0b1_1_00011_0]); // headphones powered up
|
||||
self.w(36, &[
|
||||
0b1_000_0000 | code, // HPL
|
||||
0b1_000_0000 | code, // HPR
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
fn w(&mut self, adr: u8, data: &[u8]) -> bool {
|
||||
//log::trace!("writing to 0x{:x}, {:x?}", adr, data);
|
||||
match self.llio.i2c_write(TLV320AIC3100_I2C_ADR, adr, data, None) {
|
||||
|
|
|
|||
|
|
@ -80,6 +80,48 @@ impl Codec {
|
|||
Message::new_scalar(Opcode::PauseStream.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).map(|_| ())
|
||||
}
|
||||
|
||||
/// gain goes from 0dB down to -80dB
|
||||
pub fn set_speaker_volume(&self, op: VolumeOps, gain: Option<f32>) -> Result<(), xous::Error> {
|
||||
let code = if let Some(g) = gain {
|
||||
if g > 0.0 {
|
||||
0 as usize
|
||||
} else {
|
||||
(-g * 10.0) as usize
|
||||
}
|
||||
} else {
|
||||
800 // -80.0 dB => mute
|
||||
};
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::SetSpeakerVolume.to_usize().unwrap(),
|
||||
op.to_usize().unwrap(),
|
||||
code, // gain as -dB * 10 as usize
|
||||
0,
|
||||
0
|
||||
)
|
||||
).map(|_| ())
|
||||
}
|
||||
/// gain goes from 0dB down to -80dB
|
||||
pub fn set_headphone_volume(&self, op: VolumeOps, gain: Option<f32>) -> Result<(), xous::Error> {
|
||||
let code = if let Some(g) = gain {
|
||||
if g > 0.0 {
|
||||
0 as usize
|
||||
} else {
|
||||
(-g * 10.0) as usize
|
||||
}
|
||||
} else {
|
||||
800 // -80.0 dB => mute
|
||||
};
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::SetHeadphoneVolume.to_usize().unwrap(),
|
||||
op.to_usize().unwrap(),
|
||||
code, // gain as -dB * 10 as usize
|
||||
0,
|
||||
0
|
||||
)
|
||||
).map(|_| ())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
|
|
|
|||
|
|
@ -48,6 +48,8 @@ fn xmain() -> ! {
|
|||
}
|
||||
*/
|
||||
|
||||
let mut speaker_analog_gain_db: f32 = -6.0;
|
||||
let mut headphone_analog_gain_db: f32 = -15.0;
|
||||
let mut audio_cb_conns: [Option<ScalarCallback>; 32] = [None; 32];
|
||||
loop {
|
||||
let mut msg = xous::receive_message(codec_sid).unwrap();
|
||||
|
|
@ -152,6 +154,60 @@ fn xmain() -> ! {
|
|||
//log::trace!("A rd {} wr {}", rdcount, wrcount);
|
||||
send_event(&audio_cb_conns, codec.free_play_frames(), codec.available_rec_frames());
|
||||
}),
|
||||
Some(api::Opcode::SetSpeakerVolume) => xous::msg_scalar_unpack!(msg, op, gain_code, _, _, {
|
||||
match FromPrimitive::from_usize(op) {
|
||||
Some(VolumeOps::Set) => {
|
||||
speaker_analog_gain_db = -(gain_code as f32) / 10.0;
|
||||
},
|
||||
Some(VolumeOps::Mute) => {
|
||||
speaker_analog_gain_db = -80.0;
|
||||
},
|
||||
Some(VolumeOps::RestoreDefault) => {
|
||||
speaker_analog_gain_db = -6.0;
|
||||
},
|
||||
Some(VolumeOps::UpOne) => {
|
||||
speaker_analog_gain_db += 3.0;
|
||||
if speaker_analog_gain_db >= 0.0 {
|
||||
speaker_analog_gain_db = 0.0;
|
||||
}
|
||||
},
|
||||
Some(VolumeOps::DownOne) => {
|
||||
speaker_analog_gain_db -= 3.0;
|
||||
if speaker_analog_gain_db <= -80.0 {
|
||||
speaker_analog_gain_db = -80.0;
|
||||
}
|
||||
},
|
||||
_ => log::error!("got speaker volume primitive that we don't recognize, ignoring!"),
|
||||
};
|
||||
codec.set_speaker_gain_db(speaker_analog_gain_db);
|
||||
}),
|
||||
Some(api::Opcode::SetHeadphoneVolume) => xous::msg_scalar_unpack!(msg, op, gain_code, _, _, {
|
||||
match FromPrimitive::from_usize(op) {
|
||||
Some(VolumeOps::Set) => {
|
||||
headphone_analog_gain_db = -(gain_code as f32) / 10.0;
|
||||
},
|
||||
Some(VolumeOps::Mute) => {
|
||||
headphone_analog_gain_db = -80.0;
|
||||
},
|
||||
Some(VolumeOps::RestoreDefault) => {
|
||||
headphone_analog_gain_db = -6.0;
|
||||
},
|
||||
Some(VolumeOps::UpOne) => {
|
||||
headphone_analog_gain_db += 3.0;
|
||||
if headphone_analog_gain_db >= 0.0 {
|
||||
headphone_analog_gain_db = 0.0;
|
||||
}
|
||||
},
|
||||
Some(VolumeOps::DownOne) => {
|
||||
headphone_analog_gain_db -= 3.0;
|
||||
if headphone_analog_gain_db <= -80.0 {
|
||||
headphone_analog_gain_db = -80.0;
|
||||
}
|
||||
},
|
||||
_ => log::error!("got speaker volume primitive that we don't recognize, ignoring!"),
|
||||
};
|
||||
codec.set_headphone_gain_db(headphone_analog_gain_db);
|
||||
}),
|
||||
None => {
|
||||
log::error!("couldn't convert opcode");
|
||||
break
|
||||
|
|
|
|||
Loading…
Reference in a new issue