Refactor instruments into generic types list

master
Ashley N. 2023-08-25 21:25:25 -04:00
parent bf9343686a
commit 71c95cb205
2 changed files with 49 additions and 48 deletions

View File

@ -3,7 +3,7 @@ use flate2::read::ZlibDecoder;
use linked_hash_set::LinkedHashSet; use linked_hash_set::LinkedHashSet;
use samplerate::convert; use samplerate::convert;
use uuid::Uuid; 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_MAGIC_NUMBER: &'static str = ".DelekDefleMask.";
const DMF_SUPPORTED_VERSION: u8 = 27; const DMF_SUPPORTED_VERSION: u8 = 27;
@ -29,43 +29,6 @@ pub enum FrameMode {
Custom( u8, u8, u8 ) 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 { pub struct DmfModule {
platform: u8, platform: u8,
version: u8, version: u8,
@ -327,7 +290,7 @@ impl DmfModule {
} }
}; };
InstrumentType::Psg( PsgSettings { volume, arpeggio, noise, wavetable } ) InstrumentType::PsgDcsg( PsgSettings { volume, arpeggio, noise, wavetable } )
}, },
1 => { 1 => {
print_info( "Fm Instrument" ); print_info( "Fm Instrument" );
@ -342,11 +305,11 @@ impl DmfModule {
print_info( &format!( "FMS:\t{}", fms ) ); print_info( &format!( "FMS:\t{}", fms ) );
print_info( &format!( "AMS:\t{}", ams ) ); print_info( &format!( "AMS:\t{}", ams ) );
let mut operators: [FmOperator; 4] = [ let mut operators: [Fm2612Operator; 4] = [
FmOperator::default(), Fm2612Operator::default(),
FmOperator::default(), Fm2612Operator::default(),
FmOperator::default(), Fm2612Operator::default(),
FmOperator::default() Fm2612Operator::default()
]; ];
// Operator 1 // Operator 1
@ -465,12 +428,12 @@ impl DmfModule {
print_info( &format!( "Op 4 SSG_MODE:\t{}\n", operators[ 3 ].ssg_mode ) ); 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 ) )? _ => 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 // 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<String, Vec<u8>> = HashMap::new(); let mut eefs: HashMap<String, Vec<u8>> = HashMap::new();
for instrument in &self.instruments { 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 // Echo uses volume and arpeggio envelopes
// Noise envelopes are usable but cannot be articulated in an instrument. // 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. // 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<String, Vec<u8>> = HashMap::new(); let mut eifs: HashMap<String, Vec<u8>> = HashMap::new();
for instrument in &self.instruments { 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 // Create feedback + algorithm byte
let alg_fb: u8 = ( settings.fb << 3 ) | settings.alg; let alg_fb: u8 = ( settings.fb << 3 ) | settings.alg;

View File

@ -17,6 +17,44 @@ pub struct PsgSettings {
pub wavetable: PsgEnvelope< u32 > 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)] #[derive(Debug)]
pub enum SampleFormat { pub enum SampleFormat {
Bits8, Bits8,