This commit is contained in:
Pedro de Oliveira 2022-11-13 03:12:51 +00:00
parent d55935c54b
commit 8e7c90a770
1 changed files with 324 additions and 326 deletions

View File

@ -1,9 +1,9 @@
use std::fmt;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::fmt;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
#[derive(Clone, Copy, Debug)]
pub enum Codec {
#[derive(Clone, Copy, Debug)]
pub enum Codec {
Pcm8BitUnsigned,
Adpcm4to8,
Adpcm3to8,
@ -12,10 +12,10 @@
Alaw,
Ulaw,
Adpcm4to16,
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BlockType {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BlockType {
Terminator,
SoundData,
SoundDataContinuation,
@ -24,15 +24,15 @@
Text,
Repeat,
EndRepeat,
}
}
#[derive(Debug)]
pub struct VocFile {
#[derive(Debug)]
pub struct VocFile {
pub version: (u8, u8),
pub blocks: Vec<BlockT>
}
pub blocks: Vec<BlockT>,
}
impl VocFile {
impl VocFile {
pub(crate) fn from_file(file_name: &str) -> Self {
println!("Parsing {} ...", file_name);
let mut fp = File::open(file_name).unwrap();
@ -68,7 +68,7 @@
panic!("Bad file. Expected {:02X?} got {:02X?}", checksum, checksum_buffer);
}
let mut voc = VocFile{ version, blocks: Vec::new() };
let mut voc = VocFile { version, blocks: Vec::new() };
loop {
let mut block_type_buffer: [u8; 1] = [0];
@ -128,14 +128,14 @@
let block = SoundData::new(block_size, sample_rate, codec, data);
voc.blocks.push(Box::new(block));
},
}
BlockType::SoundDataContinuation => {
let mut data: Vec<u8> = vec![0; (next - address) as usize];
drop(fp.read(&mut data));
let block = SoundDataContinuation::new(block_size, data);
voc.blocks.push(Box::new(block));
},
}
BlockType::Silence => {
let mut length_buffer: [u8; 2] = [0; 2];
drop(fp.read(&mut length_buffer));
@ -145,7 +145,7 @@
drop(fp.read(&mut frequency_divisor_buffer));
let sample_rate: i32 = 1000000i32 / (256i32 - frequency_divisor_buffer[0] as i32);
let block = Silence { block_type, size: block_size, sample_rate, length};
let block = Silence { block_type, size: block_size, sample_rate, length };
voc.blocks.push(Box::new(block));
}
BlockType::Text => {
@ -154,29 +154,27 @@
let block = Text { block_type, size: block_size, data };
voc.blocks.push(Box::new(block));
},
}
_ => panic!("block Type {:?} not implemented", block_type),
}
}
voc
}
}
}
pub trait Block: std::fmt::Debug {
pub trait Block: std::fmt::Debug {
fn to_bytes(&self) -> Vec<u8>;
}
}
impl Block for Terminator {
impl Block for Terminator {
fn to_bytes(&self) -> Vec<u8> {
vec![self.block_type as u8]
}
}
}
impl Block for SoundData {
impl Block for SoundData {
fn to_bytes(&self) -> Vec<u8> {
let size_bytes : [u8; 4] = self.size.to_le_bytes();
let size_bytes: [u8; 4] = self.size.to_le_bytes();
let frequency_divisor: u8 = (256i32 - 1000000i32 / self.sample_rate as i32) as u8;
let mut result: Vec<u8> = vec![
@ -185,40 +183,40 @@
size_bytes[1],
size_bytes[2],
frequency_divisor,
self.codec as u8
self.codec as u8,
];
result.extend_from_slice(&self.data);
result
}
}
}
impl Block for SoundDataContinuation {
impl Block for SoundDataContinuation {
fn to_bytes(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![self.block_type as u8];
result.extend_from_slice(&self.data);
result
}
}
}
impl Block for Silence {
impl Block for Silence {
fn to_bytes(&self) -> Vec<u8> {
panic!("Not implemented");
vec![self.block_type as u8]
}
}
}
impl Block for Marker {
impl Block for Marker {
fn to_bytes(&self) -> Vec<u8> {
panic!("Not implemented");
vec![self.block_type as u8]
}
}
}
impl Block for Text {
impl Block for Text {
fn to_bytes(&self) -> Vec<u8> {
let size_bytes : [u8; 4] = self.size.to_le_bytes();
let size_bytes: [u8; 4] = self.size.to_le_bytes();
let mut result: Vec<u8> = vec![
self.block_type as u8,
@ -231,50 +229,50 @@
result
}
}
}
impl Block for RepeatStart {
impl Block for RepeatStart {
fn to_bytes(&self) -> Vec<u8> {
panic!("Not implemented");
vec![self.block_type as u8]
}
}
}
impl Block for RepeatEnd {
impl Block for RepeatEnd {
fn to_bytes(&self) -> Vec<u8> {
panic!("Not implemented");
vec![self.block_type as u8]
}
}
}
type BlockT = Box<dyn Block>;
type BlockT = Box<dyn Block>;
pub struct Terminator {
pub struct Terminator {
block_type: BlockType,
}
}
impl fmt::Debug for Terminator {
impl fmt::Debug for Terminator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Terminator")
.finish()
}
}
}
impl Terminator {
impl Terminator {
pub fn new() -> Self {
Self { block_type: BlockType::Terminator }
}
}
}
pub struct SoundData {
pub struct SoundData {
block_type: BlockType,
pub size: i32,
pub sample_rate: i32,
pub codec: Codec,
pub data: Vec<u8>,
}
}
impl fmt::Debug for SoundData {
impl fmt::Debug for SoundData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SoundData")
.field("size", &self.size)
@ -282,82 +280,82 @@
.field("codec", &self.codec)
.finish()
}
}
}
impl SoundData {
impl SoundData {
pub fn new(size: i32, sample_rate: i32, codec: Codec, data: Vec<u8>) -> Self {
Self {
block_type: BlockType::SoundData,
size,
sample_rate,
codec,
data
}
data,
}
}
}
pub struct SoundDataContinuation {
pub struct SoundDataContinuation {
pub block_type: BlockType,
pub size: i32,
pub data: Vec<u8>,
}
}
impl fmt::Debug for SoundDataContinuation {
impl fmt::Debug for SoundDataContinuation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SoundDataContinuation")
.field("size", &self.size)
.finish()
}
}
}
impl SoundDataContinuation {
impl SoundDataContinuation {
pub fn new(size: i32, data: Vec<u8>) -> Self {
Self {
block_type: BlockType::SoundDataContinuation,
size,
data
}
data,
}
}
}
#[derive(Debug)]
pub struct Silence {
#[derive(Debug)]
pub struct Silence {
pub block_type: BlockType,
pub size: i32,
pub sample_rate: i32,
pub length: i16,
}
}
#[derive(Debug)]
pub struct Marker {
#[derive(Debug)]
pub struct Marker {
block_type: BlockType,
size: i32,
value: i16,
}
}
pub struct Text {
pub struct Text {
pub block_type: BlockType,
pub size: i32,
pub data: Vec<u8>,
}
}
impl fmt::Debug for Text {
impl fmt::Debug for Text {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Text")
.field("size", &self.size)
.finish()
}
}
}
#[derive(Debug)]
pub struct RepeatStart {
#[derive(Debug)]
pub struct RepeatStart {
block_type: BlockType,
size: i32,
repeat: i16,
}
}
#[derive(Debug)]
pub struct RepeatEnd {
#[derive(Debug)]
pub struct RepeatEnd {
block_type: BlockType,
size: i32,
}
}