RecordingDevice.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Copyright (c) 2006-2016 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented = 0; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "RecordingDevice.h"
  21. #include "Audio.h"
  22. #include "sound/Sound.h"
  23. namespace love
  24. {
  25. namespace audio
  26. {
  27. namespace openal
  28. {
  29. #define soundInstance() (Module::getInstance<love::sound::Sound>(Module::M_SOUND))
  30. class InvalidFormatException : public love::Exception
  31. {
  32. public:
  33. InvalidFormatException(int channels, int bitdepth)
  34. : Exception("Recording %d channels with %d bits per sample is not supported.", channels, bitdepth)
  35. {
  36. }
  37. };
  38. RecordingDevice::RecordingDevice(const char *name, int id)
  39. : name(name)
  40. , id(id)
  41. {
  42. }
  43. RecordingDevice::~RecordingDevice()
  44. {
  45. if (!isRecording())
  46. return;
  47. alcCaptureStop(device);
  48. alcCaptureCloseDevice(device);
  49. }
  50. bool RecordingDevice::startRecording()
  51. {
  52. return startRecording(samples, sampleRate, bitDepth, channels);
  53. }
  54. bool RecordingDevice::startRecording(int samples, int sampleRate, int bitDepth, int channels)
  55. {
  56. if (isRecording())
  57. {
  58. alcCaptureStop(device);
  59. alcCaptureCloseDevice(device);
  60. }
  61. ALenum format = Audio::getFormat(bitDepth, channels);
  62. if (format == AL_NONE)
  63. throw InvalidFormatException(channels, bitDepth);
  64. if (samples <= 0)
  65. throw love::Exception("Invalid number of samples.");
  66. if (sampleRate <= 0)
  67. throw love::Exception("Invalid sample rate.");
  68. device = alcCaptureOpenDevice(name.c_str(), sampleRate, format, samples);
  69. if (device == nullptr)
  70. return false;
  71. alcCaptureStart(device);
  72. this->samples = samples;
  73. this->sampleRate = sampleRate;
  74. this->bitDepth = bitDepth;
  75. this->channels = channels;
  76. return true;
  77. }
  78. void RecordingDevice::stopRecording()
  79. {
  80. if (!isRecording())
  81. return;
  82. alcCaptureStop(device);
  83. alcCaptureCloseDevice(device);
  84. device = nullptr;
  85. }
  86. love::sound::SoundData *RecordingDevice::getData()
  87. {
  88. if (!isRecording())
  89. return nullptr;
  90. int samples = getSampleCount();
  91. if (samples == 0)
  92. return nullptr;
  93. love::sound::SoundData *soundData = soundInstance()->newSoundData(samples, sampleRate, bitDepth, channels);
  94. alcCaptureSamples(device, soundData->getData(), samples);
  95. return soundData;
  96. }
  97. int RecordingDevice::getSampleCount() const
  98. {
  99. if (!isRecording())
  100. return 0;
  101. ALCint samples;
  102. alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, sizeof(ALCint), &samples);
  103. return (int)samples;
  104. }
  105. int RecordingDevice::getSampleRate() const
  106. {
  107. return sampleRate;
  108. }
  109. int RecordingDevice::getBitDepth() const
  110. {
  111. return bitDepth;
  112. }
  113. int RecordingDevice::getChannels() const
  114. {
  115. return channels;
  116. }
  117. const char *RecordingDevice::getName() const
  118. {
  119. return name.c_str();
  120. }
  121. bool RecordingDevice::isRecording() const
  122. {
  123. return device != nullptr;
  124. }
  125. } //openal
  126. } //audio
  127. } //love