RecordingDevice.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright (c) 2006-2022 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; 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)
  39. : name(name)
  40. {
  41. }
  42. RecordingDevice::~RecordingDevice()
  43. {
  44. stop();
  45. }
  46. bool RecordingDevice::start(int samples, int sampleRate, int bitDepth, int channels)
  47. {
  48. ALenum format = Audio::getFormat(bitDepth, channels);
  49. if (format == AL_NONE)
  50. throw InvalidFormatException(channels, bitDepth);
  51. if (samples <= 0)
  52. throw love::Exception("Invalid number of samples.");
  53. if (sampleRate <= 0)
  54. throw love::Exception("Invalid sample rate.");
  55. if (isRecording())
  56. stop();
  57. device = alcCaptureOpenDevice(name.c_str(), sampleRate, format, samples);
  58. if (device == nullptr)
  59. return false;
  60. alcCaptureStart(device);
  61. this->samples = samples;
  62. this->sampleRate = sampleRate;
  63. this->bitDepth = bitDepth;
  64. this->channels = channels;
  65. return true;
  66. }
  67. void RecordingDevice::stop()
  68. {
  69. if (!isRecording())
  70. return;
  71. alcCaptureStop(device);
  72. alcCaptureCloseDevice(device);
  73. device = nullptr;
  74. }
  75. love::sound::SoundData *RecordingDevice::getData()
  76. {
  77. if (!isRecording())
  78. return nullptr;
  79. int samples = getSampleCount();
  80. if (samples == 0)
  81. return nullptr;
  82. love::sound::SoundData *soundData = soundInstance()->newSoundData(samples, sampleRate, bitDepth, channels);
  83. alcCaptureSamples(device, soundData->getData(), samples);
  84. return soundData;
  85. }
  86. int RecordingDevice::getSampleCount() const
  87. {
  88. if (!isRecording())
  89. return 0;
  90. ALCint samples;
  91. alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, sizeof(ALCint), &samples);
  92. return (int)samples;
  93. }
  94. int RecordingDevice::getMaxSamples() const
  95. {
  96. return samples;
  97. }
  98. int RecordingDevice::getSampleRate() const
  99. {
  100. return sampleRate;
  101. }
  102. int RecordingDevice::getBitDepth() const
  103. {
  104. return bitDepth;
  105. }
  106. int RecordingDevice::getChannelCount() const
  107. {
  108. return channels;
  109. }
  110. const char *RecordingDevice::getName() const
  111. {
  112. return name.c_str();
  113. }
  114. bool RecordingDevice::isRecording() const
  115. {
  116. return device != nullptr;
  117. }
  118. } //openal
  119. } //audio
  120. } //love