-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: playing sound from remote source
- Loading branch information
Théo Gaillard
committed
Mar 31, 2020
1 parent
6e2c022
commit 84e45b0
Showing
12 changed files
with
232 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
fn main() { | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
|
||
let url = "https://github.com/RustAudio/rodio/raw/master/examples/music.flac"; | ||
let data = rodio::SeekableReqwest::get(url); | ||
let buffer = rodio::SeekableBufReader::new(data); | ||
let source = rodio::Decoder::new(buffer).unwrap(); | ||
|
||
sink.append(source); | ||
sink.sleep_until_end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
extern crate rodio; | ||
|
||
use std::io::BufReader; | ||
use std::{fs::File, io::BufReader}; | ||
|
||
fn main() { | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
|
||
let file = std::fs::File::open("examples/music.flac").unwrap(); | ||
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap()); | ||
let path = "examples/music.flac"; | ||
let file = File::open(path).unwrap(); | ||
let buffer = BufReader::new(file); | ||
let source = rodio::Decoder::new(buffer).unwrap(); | ||
|
||
sink.sleep_until_end(); | ||
sink.append(source); | ||
sink.sleep_until_end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
extern crate rodio; | ||
|
||
use std::io::BufReader; | ||
use std::{fs::File, io::BufReader}; | ||
|
||
fn main() { | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
|
||
let file = std::fs::File::open("examples/music.mp3").unwrap(); | ||
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap()); | ||
let path = "examples/music.mp3"; | ||
let file = File::open(path).unwrap(); | ||
let buffer = BufReader::new(file); | ||
let source = rodio::Decoder::new(buffer).unwrap(); | ||
|
||
sink.sleep_until_end(); | ||
sink.append(source); | ||
sink.sleep_until_end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
extern crate rodio; | ||
|
||
use std::io::BufReader; | ||
use std::{fs::File, io::BufReader}; | ||
|
||
fn main() { | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
|
||
let file = std::fs::File::open("examples/music.ogg").unwrap(); | ||
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap()); | ||
let path = "examples/music.ogg"; | ||
let file = File::open(path).unwrap(); | ||
let buffer = BufReader::new(file); | ||
let source = rodio::Decoder::new(buffer).unwrap(); | ||
|
||
sink.sleep_until_end(); | ||
sink.append(source); | ||
sink.sleep_until_end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
extern crate rodio; | ||
|
||
use std::io::BufReader; | ||
use std::{fs::File, io::BufReader}; | ||
|
||
fn main() { | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
let device = rodio::default_output_device().unwrap(); | ||
let sink = rodio::Sink::new(&device); | ||
|
||
let file = std::fs::File::open("examples/music.wav").unwrap(); | ||
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap()); | ||
let path = "examples/music.wav"; | ||
let file = File::open(path).unwrap(); | ||
let buffer = BufReader::new(file); | ||
let source = rodio::Decoder::new(buffer).unwrap(); | ||
|
||
sink.sleep_until_end(); | ||
sink.append(source); | ||
sink.sleep_until_end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "http")] | ||
pub mod seekable_bufreader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::io::{BufRead, Error, ErrorKind, Read, Seek, SeekFrom}; | ||
|
||
pub struct SeekableBufReader<R> { | ||
inner: R, | ||
position: usize, | ||
cache: Vec<u8>, | ||
} | ||
|
||
impl<R: Read + Seek> SeekableBufReader<R> { | ||
pub fn new(inner: R) -> SeekableBufReader<R> { | ||
SeekableBufReader { | ||
inner, | ||
position: 0, | ||
cache: Vec::default(), | ||
} | ||
} | ||
|
||
pub fn capacity(&self) -> usize { | ||
self.cache.capacity() | ||
} | ||
|
||
pub fn available(&self) -> usize { | ||
self.cache.len() | ||
} | ||
|
||
pub fn position(&self) -> usize { | ||
self.position | ||
} | ||
|
||
pub fn at(&mut self, index: usize) -> Option<&u8> { | ||
if self.available() <= index { | ||
self.cache_to(index + 1); | ||
} | ||
self.cache.get(index) | ||
} | ||
|
||
pub fn get(&mut self, from: usize, to: usize) -> &[u8] { | ||
let to = from + to; | ||
if self.available() <= to { | ||
self.cache_to(to + 1); | ||
} | ||
&self.cache[from..to] | ||
} | ||
|
||
fn cache_to(&mut self, _position: usize) { | ||
// TODO: lazy_download | ||
self.cache_to_end(); | ||
} | ||
|
||
#[allow(unused_must_use)] | ||
fn cache_to_end(&mut self) { | ||
let mut buffer = Vec::default(); | ||
self.inner.read_to_end(&mut buffer); | ||
self.cache.extend(buffer); | ||
} | ||
} | ||
|
||
impl<R: Read + Seek> Seek for SeekableBufReader<R> { | ||
fn seek(&mut self, seek: SeekFrom) -> Result<u64, Error> { | ||
let (position, offset) = match seek { | ||
SeekFrom::Start(position) => (0, position as i64), | ||
SeekFrom::Current(position) => (self.position, position), | ||
SeekFrom::End(position) => { | ||
self.cache_to_end(); | ||
(self.available(), position) | ||
} | ||
}; | ||
let position = if offset < 0 { | ||
position.checked_sub(offset.wrapping_neg() as usize) | ||
} else { | ||
position.checked_add(offset as usize) | ||
}; | ||
match position { | ||
Some(position) => { | ||
self.position = position; | ||
Ok(position as u64) | ||
} | ||
None => Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"invalid seek to a negative or overflowing position", | ||
)), | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read + Seek> Read for SeekableBufReader<R> { | ||
fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Error> { | ||
let size = match self.at(self.position + buffer.len()) { | ||
Some(_) => buffer.len(), | ||
None => self.available() - self.position, | ||
}; | ||
buffer[..size].clone_from_slice(self.get(self.position, size)); | ||
self.consume(size); | ||
Ok(size) | ||
} | ||
} | ||
|
||
impl<R: Read + Seek> BufRead for SeekableBufReader<R> { | ||
fn fill_buf(&mut self) -> Result<&[u8], Error> { | ||
Ok(self.get(self.position, self.available())) | ||
} | ||
|
||
fn consume(&mut self, amt: usize) { | ||
self.position += amt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod buffer; | ||
pub mod source; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::io::{Error, ErrorKind, Read, Seek, SeekFrom}; | ||
|
||
use reqwest::{get, Response}; | ||
|
||
pub struct SeekableReqwest {} | ||
|
||
impl SeekableReqwest { | ||
pub fn get(url: &str) -> SeekableResponse { | ||
SeekableResponse::from(get(url).unwrap()) | ||
} | ||
} | ||
|
||
pub struct SeekableResponse { | ||
inner: Response, | ||
position: usize, | ||
} | ||
|
||
impl From<Response> for SeekableResponse { | ||
fn from(inner: Response) -> SeekableResponse { | ||
SeekableResponse { | ||
inner, | ||
position: 0, | ||
} | ||
} | ||
} | ||
|
||
impl Seek for SeekableResponse { | ||
fn seek(&mut self, seek: SeekFrom) -> Result<u64, Error> { | ||
let (position, offset) = match seek { | ||
SeekFrom::Start(position) => (0, position as i64), | ||
SeekFrom::Current(position) => (self.position, position), | ||
SeekFrom::End(position) => (0, position), // TODO: real end index | ||
}; | ||
let position = if offset < 0 { | ||
position.checked_sub(offset.wrapping_neg() as usize) | ||
} else { | ||
position.checked_add(offset as usize) | ||
}; | ||
match position { | ||
Some(position) => { | ||
self.position = position; | ||
Ok(position as u64) | ||
} | ||
None => Err(Error::new( | ||
ErrorKind::InvalidInput, | ||
"invalid seek to a negative or overflowing position", | ||
)), | ||
} | ||
} | ||
} | ||
|
||
impl Read for SeekableResponse { | ||
fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Error> { | ||
self.inner.read(buffer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "http")] | ||
pub mod http; |