AudioDevice.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * AudioDevice.h - base-class for audio-devices, used by LMMS audio engine
  3. *
  4. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program 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 GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef AUDIO_DEVICE_H
  25. #define AUDIO_DEVICE_H
  26. #include <QtCore/QMutex>
  27. #include <samplerate.h>
  28. #include "lmms_basics.h"
  29. class AudioEngine;
  30. class AudioPort;
  31. class QThread;
  32. class AudioDevice
  33. {
  34. public:
  35. AudioDevice( const ch_cnt_t _channels, AudioEngine* audioEngine );
  36. virtual ~AudioDevice();
  37. inline void lock()
  38. {
  39. m_devMutex.lock();
  40. }
  41. inline void unlock()
  42. {
  43. m_devMutex.unlock();
  44. }
  45. // if audio-driver supports ports, classes inherting AudioPort
  46. // (e.g. channel-tracks) can register themselves for making
  47. // audio-driver able to collect their individual output and provide
  48. // them at a specific port - currently only supported by JACK
  49. virtual void registerPort( AudioPort * _port );
  50. virtual void unregisterPort( AudioPort * _port );
  51. virtual void renamePort( AudioPort * _port );
  52. inline bool supportsCapture() const
  53. {
  54. return m_supportsCapture;
  55. }
  56. inline sample_rate_t sampleRate() const
  57. {
  58. return m_sampleRate;
  59. }
  60. ch_cnt_t channels() const
  61. {
  62. return m_channels;
  63. }
  64. void processNextBuffer();
  65. virtual void startProcessing()
  66. {
  67. m_inProcess = true;
  68. }
  69. virtual void stopProcessing();
  70. virtual void applyQualitySettings();
  71. protected:
  72. // subclasses can re-implement this for being used in conjunction with
  73. // processNextBuffer()
  74. virtual void writeBuffer( const surroundSampleFrame * /* _buf*/,
  75. const fpp_t /*_frames*/,
  76. const float /*_master_gain*/ )
  77. {
  78. }
  79. // called by according driver for fetching new sound-data
  80. fpp_t getNextBuffer( surroundSampleFrame * _ab );
  81. // convert a given audio-buffer to a buffer in signed 16-bit samples
  82. // returns num of bytes in outbuf
  83. int convertToS16( const surroundSampleFrame * _ab,
  84. const fpp_t _frames,
  85. const float _master_gain,
  86. int_sample_t * _output_buffer,
  87. const bool _convert_endian = false );
  88. // clear given signed-int-16-buffer
  89. void clearS16Buffer( int_sample_t * _outbuf,
  90. const fpp_t _frames );
  91. // resample given buffer from samplerate _src_sr to samplerate _dst_sr
  92. fpp_t resample( const surroundSampleFrame * _src,
  93. const fpp_t _frames,
  94. surroundSampleFrame * _dst,
  95. const sample_rate_t _src_sr,
  96. const sample_rate_t _dst_sr );
  97. inline void setSampleRate( const sample_rate_t _new_sr )
  98. {
  99. m_sampleRate = _new_sr;
  100. }
  101. AudioEngine* audioEngine()
  102. {
  103. return m_audioEngine;
  104. }
  105. bool hqAudio() const;
  106. static void stopProcessingThread( QThread * thread );
  107. protected:
  108. bool m_supportsCapture;
  109. private:
  110. sample_rate_t m_sampleRate;
  111. ch_cnt_t m_channels;
  112. AudioEngine* m_audioEngine;
  113. bool m_inProcess;
  114. QMutex m_devMutex;
  115. SRC_DATA m_srcData;
  116. SRC_STATE * m_srcState;
  117. surroundSampleFrame * m_buffer;
  118. } ;
  119. #endif