Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change notemap to concert pitch and add --transpose CLI flag #27

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 69 additions & 69 deletions notemap.json
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
{
"0": 59,
"1": 71,
"32": 60,
"33": 72,
"37": 73,
"45": 74,
"128": 57,
"129": 69,
"144": 56,
"145": 68,
"192": 58,
"193": 70,
"208": 58,
"209": 70,
"297": 75,
"301": 75,
"1024": 58,
"1025": 70,
"1152": 55,
"1153": 67,
"1168": 55,
"1169": 67,
"1664": 56,
"1665": 68,
"9344": 53,
"9345": 65,
"9360": 53,
"9361": 65,
"25728": 54,
"25729": 66,
"25744": 54,
"25745": 66,
"65664": 56,
"65665": 68,
"66051": 76,
"74880": 51,
"74881": 63,
"74896": 51,
"74897": 63,
"78976": 52,
"78977": 64,
"78992": 52,
"78993": 64,
"524416": 56,
"524417": 68,
"533632": 52,
"533633": 64,
"533648": 52,
"533649": 64,
"599168": 50,
"599169": 62,
"599184": 50,
"599185": 62,
"4793472": 48,
"4793473": 60,
"4793488": 48,
"4793489": 60,
"4826240": 49,
"4826241": 61,
"4826256": 49,
"4826257": 61,
"5055616": 46,
"5055632": 46,
"5186688": 47,
"5186704": 47,
"6104192": 45,
"6104208": 45,
"13444224": 44,
"13444240": 44
"0": 73,
"1": 85,
"32": 74,
"33": 86,
"37": 87,
"45": 88,
"128": 71,
"129": 83,
"144": 70,
"145": 82,
"192": 72,
"193": 84,
"208": 72,
"209": 84,
"297": 89,
"301": 89,
"1024": 72,
"1025": 84,
"1152": 69,
"1153": 81,
"1168": 69,
"1169": 81,
"1664": 70,
"1665": 82,
"9344": 67,
"9345": 79,
"9360": 67,
"9361": 79,
"25728": 68,
"25729": 80,
"25744": 68,
"25745": 80,
"65664": 70,
"65665": 82,
"66051": 90,
"74880": 65,
"74881": 77,
"74896": 65,
"74897": 77,
"78976": 66,
"78977": 78,
"78992": 66,
"78993": 78,
"524416": 70,
"524417": 82,
"533632": 66,
"533633": 78,
"533648": 66,
"533649": 78,
"599168": 64,
"599169": 76,
"599184": 64,
"599185": 76,
"4793472": 62,
"4793473": 74,
"4793488": 62,
"4793489": 74,
"4826240": 63,
"4826241": 75,
"4826256": 63,
"4826257": 75,
"5055616": 60,
"5055632": 60,
"5186688": 61,
"5186704": 61,
"6104192": 59,
"6104208": 59,
"13444224": 58,
"13444240": 58
}
23 changes: 14 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod pressure;
mod synth;

#[derive(Debug, StructOpt)]
#[structopt(name = "haxo", about = "Make music on a haxophone", version = env!("VERGEN_GIT_DESCRIBE"))]
#[structopt(name = "haxo", about = "Make music on a haxophone", version = env!("VERGEN_GIT_DESCRIBE"), settings = &[structopt::clap::AppSettings::AllowNegativeNumbers])]
struct Opt {
#[structopt(short, long)]
record: bool,
Expand All @@ -36,6 +36,8 @@ struct Opt {
prog_number: i32,
#[structopt(short, long, default_value = "./notemap.json")]
notemap_file: String,
#[structopt(short, long, default_value = "-14")]
transpose: i32,
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -86,12 +88,15 @@ fn main() -> Result<(), Box<dyn Error>> {
#[cfg(feature = "instrumentation")]
let mut noteon_pin = Gpio::new()?.get(GPIO_UART_TXD)?.into_output();

println!("Starting haxophone (version {})...", env!("VERGEN_GIT_DESCRIBE"));
println!(
"Starting haxophone (version {})...",
env!("VERGEN_GIT_DESCRIBE")
);

keyscan::init_io().expect("Failed to initialize scan GPIO");
let mut sensor = pressure::Pressure::init().expect("Failed to initialize pressure sensor");

let mut notemap = notemap::NoteMap::generate(&opt.notemap_file);
let mut notemap = notemap::NoteMap::generate(&opt.notemap_file, opt.transpose);
if opt.record {
notemap.start_recording();
}
Expand Down Expand Up @@ -130,11 +135,11 @@ fn main() -> Result<(), Box<dyn Error>> {
}

if let Some(note) = notemap.get(&keys) {
if last_note != *note {
if last_note != note {
if log_enabled!(Level::Debug) {
debug!(
"Note: {} Pressure: {} Key {:032b}: {}",
midinotes::get_name(note).unwrap_or("Unknown?"),
notemap.get_name(&note).unwrap_or("Unknown?"),
pressure,
keys,
keys
Expand All @@ -154,12 +159,12 @@ fn main() -> Result<(), Box<dyn Error>> {
#[cfg(feature = "midi")]
midi_out.cc(MIDI_CC_VOLUME, vol);
}
synth.noteon(0, *note, 127);
synth.noteon(0, note, 127);
#[cfg(feature = "midi")]
midi_out.noteon(*note, 127);
midi_out.noteon(note, 127);
#[cfg(feature = "instrumentation")]
noteon_pin.set_high();
last_note = *note;
last_note = note;
debug!("last_note changed to {}", last_note);
}
}
Expand All @@ -181,7 +186,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// Enter Control Mode
if neg_pressure_countdown == 0 {
match midinotes::get_name(note) {
match notemap.get_name(&note) {
Some("Low Bb") => {
mode = Mode::Control;
beep(&synth, 71, 50);
Expand Down
73 changes: 36 additions & 37 deletions src/midinotes.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
// Map notes in the instrument to midi notes. The note names are used for debug
// messages and when recording notemaps. This assumes a Bb instrument
// TODO: Make this a command line flag, e.g. --instrument-key=Bb
// messages and when recording notemaps. Notes are in concert pitch.
pub const NOTES: &[(&str, i32)] = &[
("Low Bb", 44),
("Low B", 45),
("Low C", 46),
("Low C#", 47),
("Low D", 48),
("Low D#", 49),
("Low E", 50),
("Low F", 51),
("Low F#", 52),
("Low G", 53),
("Low Ab", 54),
("Low A", 55),
("Mid Bb", 56),
("Mid B", 57),
("Mid C", 58),
("Mid C#", 59),
("Mid D", 60),
("Mid D#", 61),
("Mid E", 62),
("Mid F", 63),
("Mid F#", 64),
("Mid G", 65),
("Mid Ab", 66),
("Mid A", 67),
("High Bb", 68),
("High B", 69),
("High C", 70),
("High C#", 71),
("High D", 72),
("High D#", 73),
("High E", 74),
("High F", 75),
("High F#", 76),
("Low Bb", 58),
("Low B", 59),
("Low C", 60),
("Low C#", 61),
("Low D", 62),
("Low D#", 63),
("Low E", 64),
("Low F", 65),
("Low F#", 66),
("Low G", 67),
("Low Ab", 68),
("Low A", 69),
("Mid Bb", 70),
("Mid B", 71),
("Mid C", 72),
("Mid C#", 73),
("Mid D", 74),
("Mid D#", 75),
("Mid E", 76),
("Mid F", 77),
("Mid F#", 78),
("Mid G", 79),
("Mid Ab", 80),
("Mid A", 81),
("High Bb", 82),
("High B", 83),
("High C", 84),
("High C#", 85),
("High D", 86),
("High D#", 87),
("High E", 88),
("High F", 89),
("High F#", 90),
];

pub fn get_name(value: &i32) -> Option<&str> {
pub fn get_name(value: i32) -> Option<&'static str> {
for &n in NOTES {
if *value == n.1 {
if value == n.1 {
return Some(n.0);
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/notemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ pub struct NoteMap {
record_next: bool,
filename: String,
notemap: BTreeMap<u32, i32>,
transpose: i32,
}

impl NoteMap {
pub fn generate(notemapfile: &str) -> Self {
pub fn generate(notemapfile: &str, transpose: i32) -> Self {
let mapfile = fs::read_to_string(notemapfile);
let mapfile = match mapfile {
Ok(contents) => contents,
Expand All @@ -37,7 +38,8 @@ impl NoteMap {
last_recorded: 0,
record_next: false,
filename: String::from(notemapfile),
notemap: notemap,
notemap,
transpose,
}
}

Expand All @@ -46,8 +48,14 @@ impl NoteMap {
fs::write(&self.filename, notemap_json).expect("Unable to write file");
}

pub fn get(&self, key: &u32) -> std::option::Option<&i32> {
self.notemap.get(key)
pub fn get(&self, key: &u32) -> std::option::Option<i32> {
// Notemap is concert pitch, so add transpose to get midi value.
self.notemap.get(key).map(|v| v + self.transpose)
}

pub fn get_name(&self, note: &i32) -> std::option::Option<&'static str> {
// Subtract transpose to convert from midi note to concert pitch.
midinotes::get_name(note - self.transpose)
}

pub fn start_recording(&mut self) {
Expand Down