Skip to content
Permalink
Browse files

chore: Add rustfmt.toml and rustfmt pass

  • Loading branch information
Herschel committed Aug 26, 2019
1 parent 06d9f39 commit 6a5c5ab1df88ef08c80d251e48e345f19399eb78

Large diffs are not rendered by default.

@@ -1,2 +1,2 @@
pub mod audio;
pub mod render;
pub mod audio;
pub mod render;
@@ -1,73 +1,73 @@
use generational_arena::{Arena, Index};

pub mod decoders;
pub mod swf {
pub use swf::{read, AudioCompression, CharacterId, Sound, SoundFormat, SoundStreamHead};
}

pub type AudioStreamHandle = Index;
pub type SoundHandle = Index;

type Error = Box<dyn std::error::Error>;

pub trait AudioBackend {
fn prime_audio(&mut self) {}
fn register_sound(&mut self, swf_sound: &swf::Sound) -> Result<SoundHandle, Error>;
fn preload_sound_stream_head(
&mut self,
_clip_id: swf::CharacterId,
_stream_info: &swf::SoundStreamHead,
) {
}
fn preload_sound_stream_block(&mut self, _clip_id: swf::CharacterId, _audio_data: &[u8]) {}
fn preload_sound_stream_end(&mut self, _clip_id: swf::CharacterId) {}
fn play_sound(&mut self, sound: SoundHandle);
fn start_stream(
&mut self,
clip_id: crate::prelude::CharacterId,
clip_data: crate::tag_utils::SwfSlice,
handle: &swf::SoundStreamHead,
) -> AudioStreamHandle;
// TODO: Eventually remove this/move it to library.
fn is_loading_complete(&self) -> bool {
true
}
fn tick(&mut self) {}
}

pub struct NullAudioBackend {
sounds: Arena<()>,
streams: Arena<()>,
}

impl NullAudioBackend {
pub fn new() -> NullAudioBackend {
NullAudioBackend {
streams: Arena::new(),
sounds: Arena::new(),
}
}
}

impl AudioBackend for NullAudioBackend {
fn register_sound(&mut self, _sound: &swf::Sound) -> Result<SoundHandle, Error> {
Ok(self.sounds.insert(()))
}

fn play_sound(&mut self, _sound: SoundHandle) {}

fn start_stream(
&mut self,
_clip_id: crate::prelude::CharacterId,
_clip_data: crate::tag_utils::SwfSlice,
_handle: &swf::SoundStreamHead,
) -> AudioStreamHandle {
self.streams.insert(())
}
}

impl Default for NullAudioBackend {
fn default() -> Self {
NullAudioBackend::new()
}
}
use generational_arena::{Arena, Index};

pub mod decoders;
pub mod swf {
pub use swf::{read, AudioCompression, CharacterId, Sound, SoundFormat, SoundStreamHead};
}

pub type AudioStreamHandle = Index;
pub type SoundHandle = Index;

type Error = Box<dyn std::error::Error>;

pub trait AudioBackend {
fn prime_audio(&mut self) {}
fn register_sound(&mut self, swf_sound: &swf::Sound) -> Result<SoundHandle, Error>;
fn preload_sound_stream_head(
&mut self,
_clip_id: swf::CharacterId,
_stream_info: &swf::SoundStreamHead,
) {
}
fn preload_sound_stream_block(&mut self, _clip_id: swf::CharacterId, _audio_data: &[u8]) {}
fn preload_sound_stream_end(&mut self, _clip_id: swf::CharacterId) {}
fn play_sound(&mut self, sound: SoundHandle);
fn start_stream(
&mut self,
clip_id: crate::prelude::CharacterId,
clip_data: crate::tag_utils::SwfSlice,
handle: &swf::SoundStreamHead,
) -> AudioStreamHandle;
// TODO: Eventually remove this/move it to library.
fn is_loading_complete(&self) -> bool {
true
}
fn tick(&mut self) {}
}

pub struct NullAudioBackend {
sounds: Arena<()>,
streams: Arena<()>,
}

impl NullAudioBackend {
pub fn new() -> NullAudioBackend {
NullAudioBackend {
streams: Arena::new(),
sounds: Arena::new(),
}
}
}

impl AudioBackend for NullAudioBackend {
fn register_sound(&mut self, _sound: &swf::Sound) -> Result<SoundHandle, Error> {
Ok(self.sounds.insert(()))
}

fn play_sound(&mut self, _sound: SoundHandle) {}

fn start_stream(
&mut self,
_clip_id: crate::prelude::CharacterId,
_clip_data: crate::tag_utils::SwfSlice,
_handle: &swf::SoundStreamHead,
) -> AudioStreamHandle {
self.streams.insert(())
}
}

impl Default for NullAudioBackend {
fn default() -> Self {
NullAudioBackend::new()
}
}
@@ -1,76 +1,76 @@
mod adpcm;
mod mp3;

pub use adpcm::AdpcmDecoder;
pub use mp3::Mp3Decoder;

pub trait Decoder: Iterator<Item = i16> {
fn num_channels(&self) -> u8;
fn sample_rate(&self) -> u16;
}

pub fn stream_tag_reader(
swf_data: crate::tag_utils::SwfSlice,
) -> IterRead<impl Iterator<Item = u8>> {
use std::io::{Cursor, Read};
use swf::TagCode;

let mut reader = swf::read::Reader::new(Cursor::new(swf_data), 8);
let mut audio_data = vec![];
let mut cur_byte = 0;
let mut frame = 1;
let iter = std::iter::from_fn(move || {
if cur_byte >= audio_data.len() {
cur_byte = 0;
let tag_callback =
|reader: &mut swf::read::Reader<Cursor<crate::tag_utils::SwfSlice>>,
tag_code,
tag_len| match tag_code {
TagCode::ShowFrame => {
frame += 1;
Ok(())
}
TagCode::SoundStreamBlock => {
audio_data.clear();
let mut data = vec![];
reader
.get_mut()
.take(tag_len as u64)
.read_to_end(&mut data)?;
audio_data.extend(data[4..].iter());
Ok(())
}
_ => Ok(()),
};

let _ =
crate::tag_utils::decode_tags(&mut reader, tag_callback, TagCode::SoundStreamBlock);
}

if cur_byte < audio_data.len() {
let byte = audio_data[cur_byte];
cur_byte += 1;
Some(byte)
} else {
None
}
});
IterRead(iter)
}

pub struct IterRead<I: Iterator<Item = u8>>(I);

impl<I: Iterator<Item = u8>> std::io::Read for IterRead<I> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let mut n = 0;
for out in buf {
if let Some(v) = self.0.next() {
*out = v;
n += 1;
} else {
break;
}
}
Ok(n)
}
}
mod adpcm;
mod mp3;

pub use adpcm::AdpcmDecoder;
pub use mp3::Mp3Decoder;

pub trait Decoder: Iterator<Item = i16> {
fn num_channels(&self) -> u8;
fn sample_rate(&self) -> u16;
}

pub fn stream_tag_reader(
swf_data: crate::tag_utils::SwfSlice,
) -> IterRead<impl Iterator<Item = u8>> {
use std::io::{Cursor, Read};
use swf::TagCode;

let mut reader = swf::read::Reader::new(Cursor::new(swf_data), 8);
let mut audio_data = vec![];
let mut cur_byte = 0;
let mut frame = 1;
let iter = std::iter::from_fn(move || {
if cur_byte >= audio_data.len() {
cur_byte = 0;
let tag_callback =
|reader: &mut swf::read::Reader<Cursor<crate::tag_utils::SwfSlice>>,
tag_code,
tag_len| match tag_code {
TagCode::ShowFrame => {
frame += 1;
Ok(())
}
TagCode::SoundStreamBlock => {
audio_data.clear();
let mut data = vec![];
reader
.get_mut()
.take(tag_len as u64)
.read_to_end(&mut data)?;
audio_data.extend(data[4..].iter());
Ok(())
}
_ => Ok(()),
};

let _ =
crate::tag_utils::decode_tags(&mut reader, tag_callback, TagCode::SoundStreamBlock);
}

if cur_byte < audio_data.len() {
let byte = audio_data[cur_byte];
cur_byte += 1;
Some(byte)
} else {
None
}
});
IterRead(iter)
}

pub struct IterRead<I: Iterator<Item = u8>>(I);

impl<I: Iterator<Item = u8>> std::io::Read for IterRead<I> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let mut n = 0;
for out in buf {
if let Some(v) = self.0.next() {
*out = v;
n += 1;
} else {
break;
}
}
Ok(n)
}
}

0 comments on commit 6a5c5ab

Please sign in to comment.
You can’t perform that action at this time.