aboutsummaryrefslogtreecommitdiff
path: root/src/audio
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2019-02-17 23:02:16 -0500
committerNathan Perry <avaglir@gmail.com>2019-02-17 23:02:16 -0500
commit7cedad8e5b8e15a19951ed196b3e7e2f3c95ae8e (patch)
tree9686962d5daf9b3e7b3c523619fb933b710e90a7 /src/audio
parent3eecbaf1ff02122506ee8ee8c65e02ff1325aae0 (diff)
initial mp3 attempt
Diffstat (limited to 'src/audio')
-rw-r--r--src/audio/play_queue.rs65
-rw-r--r--src/audio/ytdl.rs28
2 files changed, 81 insertions, 12 deletions
diff --git a/src/audio/play_queue.rs b/src/audio/play_queue.rs
index 119b8c5..4bb6ade 100644
--- a/src/audio/play_queue.rs
+++ b/src/audio/play_queue.rs
@@ -1,13 +1,20 @@
use std::{
collections::VecDeque,
- io::Cursor,
+ io::{self, Cursor},
+ iter,
+ process::{
+ Command,
+ Stdio,
+ },
sync::{Arc, RwLock},
thread,
time::Duration,
};
+use byteorder::{ByteOrder, NativeEndian};
use either::{Left, Right};
-use flate2::bufread::DeflateDecoder;
+use itertools::Itertools;
+use lame_sys;
use serenity::{
prelude::*,
voice,
@@ -94,9 +101,9 @@ impl PlayQueue {
}
let mut queue = queue_lck.write().unwrap();
- let item = queue.queue.pop_front().unwrap();
+ let mut item = queue.queue.pop_front().unwrap();
- let src = match item.data {
+ let src = match &mut item.data {
Left(ref url) => {
match ytdl(url, item.start, item.end) {
Ok(src) => src,
@@ -107,8 +114,54 @@ impl PlayQueue {
}
}
},
- Right(ref vec) => {
- voice::pcm(true, DeflateDecoder::new(Cursor::new(vec.clone())))
+ Right(ref mut v) => {
+
+// let out = unsafe {
+// let hip_t = lame_sys::hip_decode_init();
+//
+// let mut pcm_l = vec![0; 5 * 1024 * 1024 / 2];
+// let mut pcm_r = vec![0; 5 * 1024 * 1024 / 2];
+//
+// let count = lame_sys::hip_decode(hip_t,
+// v.as_mut_ptr(),
+// v.len(),
+// pcm_l.as_mut_ptr(),
+// pcm_r.as_mut_ptr());
+//
+// lame_sys::hip_decode_exit(hip_t);
+//
+// pcm_l.into_iter()
+// .interleave(pcm_r.into_iter())
+// .flat_map(|x| {
+// let mut b = vec![0u8; 2];
+// NativeEndian::write_i16(&mut b, x);
+//
+// b.into_iter()
+// })
+// .collect::<Vec<u8>>()
+// };
+
+ let mut out = Command::new("lame")
+ .args(&[
+ "--decode", "-t",
+ "-", "-",
+ ])
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()
+ .unwrap();
+
+ io::copy(&mut Cursor::new(v), &mut out.stdin.as_mut().unwrap());
+ let result = voice::pcm(true, Cursor::new("abc"));
+
+// out.stdout.as_mut().unwrap()
+
+ let status = out.wait_with_output().unwrap();
+ println!("{}", status.status);
+ println!("{}", String::from_utf8(status.stderr).unwrap());
+
+ result
}
};
diff --git a/src/audio/ytdl.rs b/src/audio/ytdl.rs
index e88d8f8..7333c18 100644
--- a/src/audio/ytdl.rs
+++ b/src/audio/ytdl.rs
@@ -46,6 +46,7 @@ pub(crate) trait CodecInfo {
pub(crate) struct Pcm {}
pub(crate) struct Opus {}
+pub(crate) struct Mp3 {}
impl CodecInfo for Pcm {
#[inline]
@@ -79,6 +80,22 @@ impl CodecInfo for Opus {
}
}
+impl CodecInfo for Mp3 {
+ #[inline]
+ fn ffmpeg_opts() -> &'static[&'static str] {
+ lazy_static! {
+ static ref OPTS: Vec<&'static str> = vec! [
+ "-f", "aac",
+ "-acodec", "libfdk_aac",
+ "-b:a", "96k",
+ "-sample_fmt", "s16",
+ ];
+ }
+
+ &*OPTS
+ }
+}
+
pub fn ytdl_url(uri: &str) -> Result<String> {
let args = [
"-f",
@@ -113,7 +130,7 @@ pub fn ytdl_url(uri: &str) -> Result<String> {
}
}
-pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: Option<Duration>, size_limit: Option<usize>) -> Result<Box<dyn Read + Send>> {
+pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: Option<Duration>, size_limit: Option<usize>) -> Result<Child> {
let start = start.unwrap_or(Duration::zero());
let start_str = format!("{:02}:{:02}:{:02}", start.num_hours(), start.num_minutes() % 60, start.num_seconds() % 60);
@@ -146,20 +163,19 @@ pub(crate) fn ffmpeg_dl<T: CodecInfo>(uri: &str, start: Option<Duration>, end: O
debug!("ffmpeg -i \"{}\" {}", uri, opts.join(" "));
- let command = Command::new("ffmpeg")
+ Command::new("ffmpeg")
.arg("-i")
.arg(uri)
.args(opts)
.stderr(Stdio::piped())
.stdin(Stdio::null())
.stdout(Stdio::piped())
- .spawn()?;
-
- Ok(Box::new(ChildContainer(command)))
+ .spawn()
+ .map_err(|e| e.into())
}
pub fn ytdl(uri: &str, start: Option<Duration>, end: Option<Duration>) -> Result<Box<AudioSource>> {
let youtube_uri = ytdl_url(uri)?;
let command = ffmpeg_dl::<Pcm>(&youtube_uri, start, end, None)?;
- Ok(pcm(true, command))
+ Ok(pcm(true, command.stdout.unwrap()))
}