Skip to content

Instantly share code, notes, and snippets.

@Ralith
Ralith / opus.rs
Created October 17, 2021 03:21
Decoding opus files
fn decode_opus(data: &[u8]) -> Result<Arc<oddio::Frames<f32>>> {
let _span = tracy_client::span!("opus decode");
let mut ogg = ogg::PacketReader::new(std::io::Cursor::new(data));
let mut decoder = opus::Decoder::new(SAMPLE_RATE, opus::Channels::Mono).unwrap();
let mut buffer = Vec::new();
let mut serial = None;
let mut pre_skip = 0;
let mut scale = 1.0;
let mut seen_comment = false;
while let Some(packet) = ogg.read_packet().context("reading ogg packet")? {
struct WordWrapper(Vec<capnp::Word>);
impl AsMut<[u8]> for WordWrapper {
fn as_mut(&mut self) -> &mut [u8] {
capnp::Word::words_to_bytes_mut(&mut self.0)
}
}
fn read_capnp_frame<T: io::Read>(stream: T, mut segment_sizes: Vec<u8>, mut segment_data: Vec<capnp::Word>) -> impl Future<Item=(T, Vec<u8>, Vec<capnp::Word>), Error=io::Error> {
tokio::io::read_exact(stream, [0; 4]).and_then(move |(stream, segment_count)| {
#0 tokio_core::reactor::{{impl}}::notify_handle (self=0x7fffffffbf70, handle=Task = {...}) at /home/ralith/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-core-0.1.0/src/reactor/mod.rs:377
#1 0x00005555555b952e in tokio_core::reactor::{{impl}}::notify (self=0x7fffffffbf70, msg=Schedule = {...}) at /home/ralith/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-core-0.1.0/src/reactor/mod.rs:394
#2 0x00005555555be92d in tokio_core::reactor::{{impl}}::send::{{closure}} (lp=Some = {...}) at /home/ralith/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-core-0.1.0/src/reactor/mod.rs:508
#3 0x00005555555be702 in tokio_core::reactor::{{impl}}::with_loop::{{closure}}<closure,()> (lp=0x7fffffffbf70) at /home/ralith/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-core-0.1.0/src/reactor/mod.rs:533
#4 0x00005555555be424 in scoped_tls::{{impl}}::with<tokio_core::reactor::Core,closure,()> (self=0x5555559820b8 <tokio_core::reactor::CURRENT_LOOP::hb69805ae44045245>, f=closure = {...})
at /home/ral
@Ralith
Ralith / gist:3057830
Created July 6, 2012 03:05
Working dependent maybeZip
module main
list1 : List Int
list1 = [1, 2, 3]
list2 : List Int
list2 = [4,5,6]
total equalLen : (xs : List a) -> (ys : List b) -> Maybe (length xs = length ys)
equalLen [] [] = Just refl
(defmacro with-double-ptr ((inner outer c-type) &body body)
"Creates a pointer INNER (ostensibly of type C-TYPE) which is addressed by OUTER. Only OUTER is deallocated.
This is intended for use with functions which set take a pointer to a pointer and set the inner pointer's value to the address of some C-TYPE. Note that no typechecking is done."
`(let ((,inner (make-pointer 0)))
(with-foreign-object (,outer '(:pointer ,c-type))
(setf (mem-ref ,outer :pointer) ,inner)
,@body)))
(defmacro with-double-ptr-wfo ((inner outer c-type) &body body)
"Creates a pointer INNER (ostensibly of type C-TYPE) which is addressed by OUTER."