diff --git a/src/main.rs b/src/main.rs index 5677db6..8bf6546 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate colour; mod reskit; use std::error::Error; use clap::{App, Arg, SubCommand}; -use reskit::soundtrack::DmfModule; +use reskit::soundtrack::dmf::DmfModule; use reskit::utility; use reskit::tileset; diff --git a/src/reskit/soundtrack.rs b/src/reskit/soundtrack/dmf.rs similarity index 84% rename from src/reskit/soundtrack.rs rename to src/reskit/soundtrack/dmf.rs index f3e1005..1b57bb4 100644 --- a/src/reskit/soundtrack.rs +++ b/src/reskit/soundtrack/dmf.rs @@ -1,6 +1,8 @@ use std::{error::Error, fs::File, io::Read, convert::TryInto, collections::HashMap}; use flate2::read::ZlibDecoder; -use crate::reskit::utility::{Either, get_string, get_u8, skip, get_u32, get_i8, get_i32, get_u16, get_i16}; +use crate::reskit::{utility::{Either, get_string, get_u8, skip, get_u32, get_i8, get_i32, get_u16, get_i16}, soundtrack::types::{SampleRate, SampleFormat, PsgEnvelope}}; + +use super::types::{Sample, Note, PsgSettings}; const DMF_MAGIC_NUMBER: &'static str = ".DelekDefleMask."; const DMF_SUPPORTED_VERSION: u8 = 0x18; @@ -39,21 +41,6 @@ pub struct FmSettings { operators: [FmOperator; 4] } -#[derive(Debug)] -pub struct PsgEnvelope { - envelope: Either, Vec>, - loop_at: Option, - settings: HashMap<&'static str, bool> -} - -#[derive(Debug)] -pub struct PsgSettings { - volume: PsgEnvelope, - arpeggio: PsgEnvelope, - noise: PsgEnvelope, - wavetable: PsgEnvelope -} - #[derive(Debug)] pub enum InstrumentType { Fm( FmSettings ), @@ -66,22 +53,6 @@ pub struct Instrument { instrument_type: InstrumentType } -#[derive(Debug)] -pub enum Note { - NoteOff, - CSharp( u16 ), - D( u16 ), - DSharp( u16 ), - E( u16 ), - F( u16 ), - FSharp( u16 ), - G( u16 ), - GSharp( u16 ), - A( u16 ), - ASharp( u16 ), - B( u16 ), - C( u16 ) -} #[derive(Debug)] pub struct Effect { @@ -97,31 +68,6 @@ pub struct PatternRow { instrument_index: Option } -#[derive(Debug)] -pub enum SampleRate { - Hz8000, - Hz11025, - Hz16000, - Hz22050, - Hz32000 -} - -#[derive(Debug)] -pub enum SampleFormat { - Bits8, - Bits16 -} - -#[derive(Debug)] -pub struct Sample { - name: String, - rate: SampleRate, - pitch: i8, - amp: u8, - bitrate: SampleFormat, - data: Vec -} - pub struct DmfModule { platform: u8, version: u8, @@ -651,75 +597,4 @@ impl DmfModule { todo!() } -} - - -impl Note { - - pub fn get_freq( &self ) -> Option { - match self { - Note::NoteOff => None, - Note::CSharp( octave ) => match octave { - 0 => Some( 17.32 ), 1 => Some( 34.65 ), 2 => Some( 69.30 ), 3 => Some( 138.59 ), - 4 => Some( 277.18 ), 5 => Some( 554.37 ), 6 => Some( 1108.73 ), 7 => Some( 2217.46 ), - _ => None - }, - Note::D( octave ) => match octave { - 0 => Some( 18.35 ), 1 => Some( 36.71 ), 2 => Some( 73.42 ), 3 => Some( 146.83 ), - 4 => Some( 293.66 ), 5 => Some( 587.33 ), 6 => Some( 1174.66 ), 7 => Some( 2349.32 ), - _ => None - }, - Note::DSharp( octave ) => match octave { - 0 => Some( 19.45 ), 1 => Some( 38.89 ), 2 => Some( 77.78 ), 3 => Some( 155.56 ), - 4 => Some( 311.13 ), 5 => Some( 622.25 ), 6 => Some( 1244.51 ), 7 => Some( 2489.02 ), - _ => None - }, - Note::E( octave ) => match octave { - 0 => Some( 20.60 ), 1 => Some( 41.20 ), 2 => Some( 82.41 ), 3 => Some( 164.81 ), - 4 => Some( 329.63 ), 5 => Some( 659.25 ), 6 => Some( 1318.51 ), 7 => Some( 2637.02 ), - _ => None - }, - Note::F( octave ) => match octave { - 0 => Some( 21.83 ), 1 => Some( 43.65 ), 2 => Some( 87.31 ), 3 => Some( 174.61 ), - 4 => Some( 349.23 ), 5 => Some( 698.46 ), 6 => Some( 1396.91 ), 7 => Some( 2793.83 ), - _ => None - }, - Note::FSharp( octave ) => match octave { - 0 => Some( 23.12 ), 1 => Some( 46.25 ), 2 => Some( 92.50 ), 3 => Some( 185.00 ), - 4 => Some( 369.99 ), 5 => Some( 739.99 ), 6 => Some( 1479.98 ), 7 => Some( 2959.96 ), - _ => None - }, - Note::G( octave ) => match octave { - 0 => Some( 24.50 ), 1 => Some( 49.00 ), 2 => Some( 98.00 ), 3 => Some( 196.00 ), - 4 => Some( 392.00 ), 5 => Some( 783.99 ), 6 => Some( 1567.98 ), 7 => Some( 3135.96 ), - _ => None - }, - Note::GSharp( octave ) => match octave { - 0 => Some( 25.96 ), 1 => Some( 51.91 ), 2 => Some( 103.83 ), 3 => Some( 207.65 ), - 4 => Some( 415.30 ), 5 => Some( 830.61 ), 6 => Some( 1661.22 ), 7 => Some( 3322.44 ), - _ => None - }, - Note::A( octave ) => match octave { - 0 => Some( 27.50 ), 1 => Some( 55.00 ), 2 => Some( 110.00 ), 3 => Some( 220.00 ), - 4 => Some( 440.00 ), 5 => Some( 880.00 ), 6 => Some( 1760.00 ), 7 => Some( 3520.00 ), - _ => None - }, - Note::ASharp( octave ) => match octave { - 0 => Some( 29.14 ), 1 => Some( 58.27 ), 2 => Some( 116.54 ), 3 => Some( 233.08 ), - 4 => Some( 466.16 ), 5 => Some( 932.33 ), 6 => Some( 1864.66 ), 7 => Some( 3729.31 ), - _ => None - }, - Note::B( octave ) => match octave { - 0 => Some( 30.87 ), 1 => Some( 61.74 ), 2 => Some( 123.47 ), 3 => Some( 246.94 ), - 4 => Some( 493.88 ), 5 => Some( 987.77 ), 6 => Some( 1975.53 ), 7 => Some( 3951.07 ), - _ => None - }, - Note::C( octave ) => match octave { - 0 => Some( 16.35 ), 1 => Some( 32.70 ), 2 => Some( 65.41 ), 3 => Some( 130.81 ), - 4 => Some( 261.63 ), 5 => Some( 523.25 ), 6 => Some( 1046.50 ), 7 => Some( 2093.00 ), - _ => None - } - } - } - -} +} \ No newline at end of file diff --git a/src/reskit/soundtrack/mod.rs b/src/reskit/soundtrack/mod.rs new file mode 100644 index 0000000..3b4ac3e --- /dev/null +++ b/src/reskit/soundtrack/mod.rs @@ -0,0 +1,2 @@ +pub mod dmf; +pub mod types; \ No newline at end of file diff --git a/src/reskit/soundtrack/types.rs b/src/reskit/soundtrack/types.rs new file mode 100644 index 0000000..8df4728 --- /dev/null +++ b/src/reskit/soundtrack/types.rs @@ -0,0 +1,131 @@ +use std::collections::HashMap; +use crate::reskit::utility::Either; + +#[derive(Debug)] +pub struct PsgEnvelope { + pub envelope: Either, Vec>, + pub loop_at: Option, + pub settings: HashMap<&'static str, bool> +} + +#[derive(Debug)] +pub struct PsgSettings { + pub volume: PsgEnvelope, + pub arpeggio: PsgEnvelope, + pub noise: PsgEnvelope, + pub wavetable: PsgEnvelope +} + + +#[derive(Debug)] +pub enum SampleRate { + Hz8000, + Hz11025, + Hz16000, + Hz22050, + Hz32000 +} + +#[derive(Debug)] +pub enum SampleFormat { + Bits8, + Bits16 +} + +#[derive(Debug)] +pub struct Sample { + pub name: String, + pub rate: SampleRate, + pub pitch: i8, + pub amp: u8, + pub bitrate: SampleFormat, + pub data: Vec +} + +#[derive(Debug)] +pub enum Note { + NoteOff, + CSharp( u16 ), + D( u16 ), + DSharp( u16 ), + E( u16 ), + F( u16 ), + FSharp( u16 ), + G( u16 ), + GSharp( u16 ), + A( u16 ), + ASharp( u16 ), + B( u16 ), + C( u16 ) +} + + +impl Note { + + pub fn get_freq( &self ) -> Option { + match self { + Note::NoteOff => None, + Note::CSharp( octave ) => match octave { + 0 => Some( 17.32 ), 1 => Some( 34.65 ), 2 => Some( 69.30 ), 3 => Some( 138.59 ), + 4 => Some( 277.18 ), 5 => Some( 554.37 ), 6 => Some( 1108.73 ), 7 => Some( 2217.46 ), + _ => None + }, + Note::D( octave ) => match octave { + 0 => Some( 18.35 ), 1 => Some( 36.71 ), 2 => Some( 73.42 ), 3 => Some( 146.83 ), + 4 => Some( 293.66 ), 5 => Some( 587.33 ), 6 => Some( 1174.66 ), 7 => Some( 2349.32 ), + _ => None + }, + Note::DSharp( octave ) => match octave { + 0 => Some( 19.45 ), 1 => Some( 38.89 ), 2 => Some( 77.78 ), 3 => Some( 155.56 ), + 4 => Some( 311.13 ), 5 => Some( 622.25 ), 6 => Some( 1244.51 ), 7 => Some( 2489.02 ), + _ => None + }, + Note::E( octave ) => match octave { + 0 => Some( 20.60 ), 1 => Some( 41.20 ), 2 => Some( 82.41 ), 3 => Some( 164.81 ), + 4 => Some( 329.63 ), 5 => Some( 659.25 ), 6 => Some( 1318.51 ), 7 => Some( 2637.02 ), + _ => None + }, + Note::F( octave ) => match octave { + 0 => Some( 21.83 ), 1 => Some( 43.65 ), 2 => Some( 87.31 ), 3 => Some( 174.61 ), + 4 => Some( 349.23 ), 5 => Some( 698.46 ), 6 => Some( 1396.91 ), 7 => Some( 2793.83 ), + _ => None + }, + Note::FSharp( octave ) => match octave { + 0 => Some( 23.12 ), 1 => Some( 46.25 ), 2 => Some( 92.50 ), 3 => Some( 185.00 ), + 4 => Some( 369.99 ), 5 => Some( 739.99 ), 6 => Some( 1479.98 ), 7 => Some( 2959.96 ), + _ => None + }, + Note::G( octave ) => match octave { + 0 => Some( 24.50 ), 1 => Some( 49.00 ), 2 => Some( 98.00 ), 3 => Some( 196.00 ), + 4 => Some( 392.00 ), 5 => Some( 783.99 ), 6 => Some( 1567.98 ), 7 => Some( 3135.96 ), + _ => None + }, + Note::GSharp( octave ) => match octave { + 0 => Some( 25.96 ), 1 => Some( 51.91 ), 2 => Some( 103.83 ), 3 => Some( 207.65 ), + 4 => Some( 415.30 ), 5 => Some( 830.61 ), 6 => Some( 1661.22 ), 7 => Some( 3322.44 ), + _ => None + }, + Note::A( octave ) => match octave { + 0 => Some( 27.50 ), 1 => Some( 55.00 ), 2 => Some( 110.00 ), 3 => Some( 220.00 ), + 4 => Some( 440.00 ), 5 => Some( 880.00 ), 6 => Some( 1760.00 ), 7 => Some( 3520.00 ), + _ => None + }, + Note::ASharp( octave ) => match octave { + 0 => Some( 29.14 ), 1 => Some( 58.27 ), 2 => Some( 116.54 ), 3 => Some( 233.08 ), + 4 => Some( 466.16 ), 5 => Some( 932.33 ), 6 => Some( 1864.66 ), 7 => Some( 3729.31 ), + _ => None + }, + Note::B( octave ) => match octave { + 0 => Some( 30.87 ), 1 => Some( 61.74 ), 2 => Some( 123.47 ), 3 => Some( 246.94 ), + 4 => Some( 493.88 ), 5 => Some( 987.77 ), 6 => Some( 1975.53 ), 7 => Some( 3951.07 ), + _ => None + }, + Note::C( octave ) => match octave { + 0 => Some( 16.35 ), 1 => Some( 32.70 ), 2 => Some( 65.41 ), 3 => Some( 130.81 ), + 4 => Some( 261.63 ), 5 => Some( 523.25 ), 6 => Some( 1046.50 ), 7 => Some( 2093.00 ), + _ => None + } + } + } + +} \ No newline at end of file