2021-02-05 22:47:13 +00:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use byteorder::{LE, ReadBytesExt};
|
|
|
|
|
|
|
|
use crate::framework::error::{GameError, GameResult};
|
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
#[repr(u8)]
|
2021-02-05 22:47:13 +00:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
2020-09-02 22:58:11 +00:00
|
|
|
pub enum Version {
|
|
|
|
// Can't find any files with this signature,
|
|
|
|
// But apparently these files had no Pi flag.
|
|
|
|
Beta = b'1',
|
|
|
|
Main = b'2',
|
|
|
|
// OrgMaker 2.05 Extended Drums
|
2021-02-05 22:47:13 +00:00
|
|
|
Extended = b'3',
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct LoopRange {
|
|
|
|
// inclusive
|
|
|
|
pub start: i32,
|
|
|
|
// exclusive
|
2021-02-05 22:47:13 +00:00
|
|
|
pub end: i32,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct Display {
|
|
|
|
pub beats: u8,
|
2021-02-05 22:47:13 +00:00
|
|
|
pub steps: u8,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct Timing {
|
|
|
|
pub wait: u16,
|
2021-02-05 22:47:13 +00:00
|
|
|
pub loop_range: LoopRange,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Instrument {
|
|
|
|
pub freq: u16,
|
|
|
|
pub inst: u8,
|
|
|
|
pub pipi: u8,
|
2021-02-05 22:47:13 +00:00
|
|
|
pub notes: u16,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Track {
|
|
|
|
pub inst: Instrument,
|
2021-02-05 22:47:13 +00:00
|
|
|
pub notes: Vec<Note>,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Track {
|
|
|
|
fn clone(&self) -> Track {
|
|
|
|
Track {
|
|
|
|
inst: self.inst,
|
|
|
|
notes: self.notes.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Track {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
self.inst.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct Note {
|
|
|
|
pub pos: i32,
|
|
|
|
pub key: u8,
|
|
|
|
pub len: u8,
|
|
|
|
pub vol: u8,
|
2021-02-05 22:47:13 +00:00
|
|
|
pub pan: u8,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Song {
|
|
|
|
pub version: Version,
|
|
|
|
pub time: Timing,
|
2021-02-05 22:47:13 +00:00
|
|
|
pub tracks: [Track; 16],
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Song {
|
|
|
|
fn clone(&self) -> Song {
|
|
|
|
Song {
|
|
|
|
version: self.version,
|
|
|
|
time: self.time,
|
|
|
|
tracks: self.tracks.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Song {
|
|
|
|
pub fn empty() -> Song {
|
|
|
|
Song {
|
|
|
|
version: Version::Main,
|
|
|
|
time: Timing { wait: 8, loop_range: LoopRange { start: 0, end: 1 } },
|
|
|
|
tracks: [
|
2021-02-05 22:47:13 +00:00
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
Track { inst: Instrument { freq: 1000, inst: 0, pipi: 0, notes: 0 }, notes: vec![] },
|
|
|
|
],
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:47:13 +00:00
|
|
|
pub fn load_from<R: io::Read>(mut f: R) -> GameResult<Song> {
|
2020-09-02 22:58:11 +00:00
|
|
|
let mut magic = [0; 6];
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
f.read_exact(&mut magic)?;
|
2021-02-05 22:47:13 +00:00
|
|
|
|
|
|
|
let version =
|
2020-09-02 22:58:11 +00:00
|
|
|
match &magic {
|
|
|
|
b"Org-01" => Version::Beta,
|
|
|
|
b"Org-02" => Version::Main,
|
|
|
|
b"Org-03" => Version::Extended,
|
2021-02-05 22:47:13 +00:00
|
|
|
_ => return Err(GameError::ResourceLoadError("Invalid magic number".to_string()))
|
2020-09-02 22:58:11 +00:00
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
|
|
|
let wait = f.read_u16::<LE>()?;
|
|
|
|
let _bpm = f.read_u8()?;
|
|
|
|
let _spb = f.read_u8()?;
|
2020-09-02 22:58:11 +00:00
|
|
|
let start = f.read_i32::<LE>()?;
|
2021-02-05 22:47:13 +00:00
|
|
|
let end = f.read_i32::<LE>()?;
|
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
use std::mem::MaybeUninit as Mu;
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
let mut insts: [Mu<Instrument>; 16] = unsafe {
|
|
|
|
Mu::uninit().assume_init()
|
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for i in insts.iter_mut() {
|
2021-02-05 22:47:13 +00:00
|
|
|
let freq = f.read_u16::<LE>()?;
|
|
|
|
let inst = f.read_u8()?;
|
|
|
|
let pipi = f.read_u8()?;
|
2020-09-02 22:58:11 +00:00
|
|
|
let notes = f.read_u16::<LE>()?;
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
*i = Mu::new(Instrument {
|
|
|
|
freq,
|
|
|
|
inst,
|
|
|
|
pipi,
|
2021-02-05 22:47:13 +00:00
|
|
|
notes,
|
2020-09-02 22:58:11 +00:00
|
|
|
});
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
let insts: [Instrument; 16] = unsafe {
|
|
|
|
std::mem::transmute(insts)
|
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
let mut tracks: [Mu<Track>; 16] = unsafe {
|
|
|
|
Mu::uninit().assume_init()
|
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for (i, t) in tracks.iter_mut().enumerate() {
|
|
|
|
let count = insts[i].notes as usize;
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct UninitNote {
|
|
|
|
pos: Mu<i32>,
|
|
|
|
key: Mu<u8>,
|
|
|
|
len: Mu<u8>,
|
|
|
|
vol: Mu<u8>,
|
2021-02-05 22:47:13 +00:00
|
|
|
pan: Mu<u8>,
|
2020-09-02 22:58:11 +00:00
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
let mut notes: Vec<UninitNote> = unsafe {
|
|
|
|
vec![Mu::uninit().assume_init(); count]
|
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for note in notes.iter_mut() {
|
|
|
|
note.pos = Mu::new(f.read_i32::<LE>()?);
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for note in notes.iter_mut() {
|
|
|
|
note.key = Mu::new(f.read_u8()?);
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for note in notes.iter_mut() {
|
|
|
|
note.len = Mu::new(f.read_u8()?);
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for note in notes.iter_mut() {
|
|
|
|
note.vol = Mu::new(f.read_u8()?);
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
for note in notes.iter_mut() {
|
|
|
|
note.pan = Mu::new(f.read_u8()?);
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
*t = Mu::new(Track {
|
|
|
|
inst: insts[i],
|
2021-02-05 22:47:13 +00:00
|
|
|
notes: unsafe { std::mem::transmute(notes) },
|
2020-09-02 22:58:11 +00:00
|
|
|
});
|
|
|
|
}
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
let tracks = unsafe {
|
|
|
|
std::mem::transmute(tracks)
|
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
let song = Song {
|
|
|
|
version,
|
|
|
|
time: Timing {
|
|
|
|
wait,
|
|
|
|
loop_range: LoopRange {
|
|
|
|
start,
|
2021-02-05 22:47:13 +00:00
|
|
|
end,
|
|
|
|
},
|
2020-09-02 22:58:11 +00:00
|
|
|
},
|
2021-02-05 22:47:13 +00:00
|
|
|
tracks,
|
2020-09-02 22:58:11 +00:00
|
|
|
};
|
2021-02-05 22:47:13 +00:00
|
|
|
|
2020-09-02 22:58:11 +00:00
|
|
|
Ok(song)
|
|
|
|
}
|
|
|
|
}
|