From 71c95cb205f563dfa88ba965cffbea94a405fa7d Mon Sep 17 00:00:00 2001 From: ashley Date: Fri, 25 Aug 2023 21:25:25 -0400 Subject: [PATCH] Refactor instruments into generic types list --- src/reskit/soundtrack/formats/dmf.rs | 59 ++++++---------------------- src/reskit/soundtrack/types.rs | 38 ++++++++++++++++++ 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/reskit/soundtrack/formats/dmf.rs b/src/reskit/soundtrack/formats/dmf.rs index abc23b2..cc21ee2 100644 --- a/src/reskit/soundtrack/formats/dmf.rs +++ b/src/reskit/soundtrack/formats/dmf.rs @@ -3,7 +3,7 @@ use flate2::read::ZlibDecoder; use linked_hash_set::LinkedHashSet; use samplerate::convert; use uuid::Uuid; -use crate::reskit::{utility::{get_string, get_u8, skip, get_u32, get_i8, get_i32, get_u16, get_i16, Ring, print_warning, print_info}, soundtrack::{types::{SampleFormat, PsgEnvelope, Note, Sample, PsgSettings, PatternRow, Effect, DcsgChannelMode, NoiseType, Channel}, engines::echo::{EchoFormat, EchoEvent, ESF_FM_1, ESF_FM_2, ESF_FM_3, ESF_FM_4, ESF_FM_5, ESF_PSG_1, ESF_FM_6, ESF_PSG_2, ESF_PSG_3, ESF_PSG_4, get_events_for_row, compact_delays}}}; +use crate::reskit::{utility::{get_string, get_u8, skip, get_u32, get_i8, get_i32, get_u16, get_i16, Ring, print_warning, print_info}, soundtrack::{types::{SampleFormat, PsgEnvelope, Note, Sample, PsgSettings, PatternRow, Effect, DcsgChannelMode, NoiseType, Channel, Instrument, InstrumentType, Fm2612Operator, Fm2612Settings}, engines::echo::{EchoFormat, EchoEvent, ESF_FM_1, ESF_FM_2, ESF_FM_3, ESF_FM_4, ESF_FM_5, ESF_PSG_1, ESF_FM_6, ESF_PSG_2, ESF_PSG_3, ESF_PSG_4, get_events_for_row, compact_delays}}}; const DMF_MAGIC_NUMBER: &'static str = ".DelekDefleMask."; const DMF_SUPPORTED_VERSION: u8 = 27; @@ -29,43 +29,6 @@ pub enum FrameMode { Custom( u8, u8, u8 ) } -#[derive(Debug, Default)] -pub struct FmOperator { - am: u8, - ar: u8, - dr: u8, - mult: u8, - rr: u8, - sl: u8, - tl: u8, - dt2: u8, - rs: u8, - dt: i8, - d2r: u8, - ssg_mode: u8 -} - -#[derive(Debug)] -pub struct FmSettings { - alg: u8, - fb: u8, - fms: u8, - ams: u8, - operators: [FmOperator; 4] -} - -#[derive(Debug)] -pub enum InstrumentType { - Fm( FmSettings ), - Psg( PsgSettings ) -} - -#[derive(Debug)] -pub struct Instrument { - name: String, - instrument_type: InstrumentType -} - pub struct DmfModule { platform: u8, version: u8, @@ -327,7 +290,7 @@ impl DmfModule { } }; - InstrumentType::Psg( PsgSettings { volume, arpeggio, noise, wavetable } ) + InstrumentType::PsgDcsg( PsgSettings { volume, arpeggio, noise, wavetable } ) }, 1 => { print_info( "Fm Instrument" ); @@ -342,11 +305,11 @@ impl DmfModule { print_info( &format!( "FMS:\t{}", fms ) ); print_info( &format!( "AMS:\t{}", ams ) ); - let mut operators: [FmOperator; 4] = [ - FmOperator::default(), - FmOperator::default(), - FmOperator::default(), - FmOperator::default() + let mut operators: [Fm2612Operator; 4] = [ + Fm2612Operator::default(), + Fm2612Operator::default(), + Fm2612Operator::default(), + Fm2612Operator::default() ]; // Operator 1 @@ -465,12 +428,12 @@ impl DmfModule { print_info( &format!( "Op 4 SSG_MODE:\t{}\n", operators[ 3 ].ssg_mode ) ); } - InstrumentType::Fm( FmSettings { alg, fb, fms, ams, operators } ) + InstrumentType::Fm2612( Fm2612Settings { alg, fb, fms, ams, operators } ) }, _ => return Err( format!( "invalid file: invalid instrument mode {}", mode ) )? }; - instruments.push( Instrument { name, instrument_type } ); + instruments.push( Instrument { index: i as usize, name, instrument_type } ); } // Version 27 of DMF specifies "1 wavetable of 0 bytes" for sega megadrive @@ -672,7 +635,7 @@ impl EchoFormat for DmfModule { let mut eefs: HashMap> = HashMap::new(); for instrument in &self.instruments { - if let InstrumentType::Psg( settings ) = &instrument.instrument_type { + if let InstrumentType::PsgDcsg( settings ) = &instrument.instrument_type { // Echo uses volume and arpeggio envelopes // Noise envelopes are usable but cannot be articulated in an instrument. // For use of the noise envelope, use ESF $0Bnn/$3Bnn and $3Ann in the stream. @@ -801,7 +764,7 @@ impl EchoFormat for DmfModule { let mut eifs: HashMap> = HashMap::new(); for instrument in &self.instruments { - if let InstrumentType::Fm( settings ) = &instrument.instrument_type { + if let InstrumentType::Fm2612( settings ) = &instrument.instrument_type { // Create feedback + algorithm byte let alg_fb: u8 = ( settings.fb << 3 ) | settings.alg; diff --git a/src/reskit/soundtrack/types.rs b/src/reskit/soundtrack/types.rs index 4034e12..518c3fc 100644 --- a/src/reskit/soundtrack/types.rs +++ b/src/reskit/soundtrack/types.rs @@ -17,6 +17,44 @@ pub struct PsgSettings { pub wavetable: PsgEnvelope< u32 > } +#[derive(Debug, Default)] +pub struct Fm2612Operator { + pub am: u8, + pub ar: u8, + pub dr: u8, + pub mult: u8, + pub rr: u8, + pub sl: u8, + pub tl: u8, + pub dt2: u8, + pub rs: u8, + pub dt: i8, + pub d2r: u8, + pub ssg_mode: u8 +} + +#[derive(Debug)] +pub struct Fm2612Settings { + pub alg: u8, + pub fb: u8, + pub fms: u8, + pub ams: u8, + pub operators: [Fm2612Operator; 4] +} + +#[derive(Debug)] +pub enum InstrumentType { + Fm2612( Fm2612Settings ), + PsgDcsg( PsgSettings ) +} + +#[derive(Debug)] +pub struct Instrument { + pub index: usize, + pub name: String, + pub instrument_type: InstrumentType +} + #[derive(Debug)] pub enum SampleFormat { Bits8,