libvorbis_decoder.rs 2.0 KB

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