libvorbis_decoder.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #[cfg(not(feature = "with-tremor"))] extern crate vorbis;
  2. #[cfg(feature = "with-tremor")] extern crate tremor as vorbis;
  3. use std::io::{Read, Seek};
  4. use std::fmt;
  5. use std::error;
  6. pub struct VorbisDecoder<R: Read + Seek>(vorbis::Decoder<R>);
  7. pub struct VorbisPacket(vorbis::Packet);
  8. pub struct VorbisError(vorbis::VorbisError);
  9. impl <R> VorbisDecoder<R>
  10. where R: Read + Seek
  11. {
  12. pub fn new(input: R) -> Result<VorbisDecoder<R>, VorbisError> {
  13. Ok(VorbisDecoder(vorbis::Decoder::new(input)?))
  14. }
  15. #[cfg(not(feature = "with-tremor"))]
  16. pub fn seek(&mut self, ms: i64) -> Result<(), VorbisError> {
  17. self.0.time_seek(ms as f64 / 1000f64)?;
  18. Ok(())
  19. }
  20. #[cfg(feature = "with-tremor")]
  21. pub fn seek(&mut self, ms: i64) -> Result<(), VorbisError> {
  22. self.0.time_seek(ms)?;
  23. Ok(())
  24. }
  25. pub fn next_packet(&mut self) -> Result<Option<VorbisPacket>, VorbisError> {
  26. loop {
  27. match self.0.packets().next() {
  28. Some(Ok(packet)) => return Ok(Some(VorbisPacket(packet))),
  29. None => return Ok(None),
  30. Some(Err(vorbis::VorbisError::Hole)) => (),
  31. Some(Err(err)) => return Err(err.into()),
  32. }
  33. }
  34. }
  35. }
  36. impl VorbisPacket {
  37. pub fn data(&self) -> &[i16] {
  38. &self.0.data
  39. }
  40. pub fn data_mut(&mut self) -> &mut [i16] {
  41. &mut self.0.data
  42. }
  43. }
  44. impl From<vorbis::VorbisError> for VorbisError {
  45. fn from(err: vorbis::VorbisError) -> VorbisError {
  46. VorbisError(err)
  47. }
  48. }
  49. impl fmt::Debug for VorbisError {
  50. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  51. fmt::Debug::fmt(&self.0, f)
  52. }
  53. }
  54. impl fmt::Display for VorbisError {
  55. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  56. fmt::Display::fmt(&self.0, f)
  57. }
  58. }
  59. impl error::Error for VorbisError {
  60. fn description(&self) -> &str {
  61. error::Error::description(&self.0)
  62. }
  63. fn cause(&self) -> Option<&error::Error> {
  64. error::Error::cause(&self.0)
  65. }
  66. }