AudioResampler.cxx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file AudioResampler.cxx
  23. * @brief Audio resampler
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango.org>
  26. */
  27. #include "AudioResampler.h"
  28. #include "tinymedia/tmedia_resampler.h"
  29. #include "tsk_debug.h"
  30. AudioResampler::AudioResampler(uint32_t nInFreq, uint32_t nOutFreq, uint32_t nFrameDuration, uint32_t nChannels, uint32_t nQuality):
  31. m_nOutFreq(nOutFreq),
  32. m_nInFreq(nInFreq),
  33. m_nFrameDuration(nFrameDuration),
  34. m_nChannels(nChannels),
  35. m_nQuality(nQuality)
  36. {
  37. if ((m_pWrappedResampler = tmedia_resampler_create())) {
  38. int ret;
  39. if ((ret = tmedia_resampler_open(m_pWrappedResampler, nInFreq, nOutFreq, nFrameDuration, nChannels, nChannels, m_nQuality, 16))) {
  40. TSK_DEBUG_ERROR("Failed to open audio resampler (%d)", ret);
  41. TSK_OBJECT_SAFE_FREE(m_pWrappedResampler);
  42. }
  43. }
  44. else {
  45. TSK_DEBUG_ERROR("No audio resampler could be found. Did you forget to call tdav_init()?");
  46. }
  47. }
  48. AudioResampler::~AudioResampler()
  49. {
  50. TSK_OBJECT_SAFE_FREE(m_pWrappedResampler);
  51. }
  52. uint32_t AudioResampler::process(const void* pInData, uint32_t nInSizeInBytes, void* pOutData, uint32_t nOutSizeInBytes)
  53. {
  54. if(!m_pWrappedResampler) {
  55. TSK_DEBUG_ERROR("Embedded resampler is invalid");
  56. return 0;
  57. }
  58. if(nInSizeInBytes < getInputRequiredSizeInShort()/2) {
  59. TSK_DEBUG_ERROR("Input buffer is too short");
  60. return 0;
  61. }
  62. if(nOutSizeInBytes < getOutputRequiredSizeInShort()/2) {
  63. TSK_DEBUG_ERROR("Output buffer is too short");
  64. return 0;
  65. }
  66. return 2*tmedia_resampler_process(m_pWrappedResampler, (uint16_t*)pInData, nInSizeInBytes/2, (uint16_t*)pOutData, nOutSizeInBytes/2);
  67. }