Permalink
Browse files

turn microphone into an object

  • Loading branch information...
1 parent a9a1f8a commit 0fa6d55f2ba1da7a2369efc86b599d8556d0e960 @Queatz committed Apr 27, 2012
Showing with 85 additions and 85 deletions.
  1. +0 −78 engine/audio.cpp
  2. +1 −5 engine/engine.cpp
  3. +73 −0 engine/microphone.cpp
  4. +11 −2 engine/scge.parseme.hpp
View
@@ -102,81 +102,3 @@ void audio(const char* a) {
void audio_soundfont(const char* a) {
setenv("FLUID_SOUNDFONT", a, 1);
}
-
-/* Microphone */
-
-#define CAPTURE_FREQ 44100
-#define CAPTURE_BUF_SIZE 22050
-
-void microphone_on() {
- if(alure_state == 0)
- audio_on();
-
- if(!capture_device) {
- alGetError();
-
- capture_device = alcCaptureOpenDevice(NULL, CAPTURE_FREQ, AL_FORMAT_MONO16, CAPTURE_BUF_SIZE);
-
- if(!capture_device) {
- err("microphone", alErrorString(alGetError()));
- return;
- }
-
- ALenum e;
- e = alcGetError(capture_device);
- if(e != AL_NO_ERROR)
- err("microphone", alcErrorString(e));
- }
-
- capture_soundbyte.data = NULL;
- capture_soundbyte.frequency = CAPTURE_FREQ;
-
- alcCaptureStart(capture_device);
-}
-
-void microphone_off() {
- if(!capture_device)
- return;
-
- alcCaptureStop(capture_device);
- alcCaptureCloseDevice(capture_device);
-
- capture_device = NULL;
-}
-
-void microphone(bool a) {
- if(a)
- microphone_on();
- else
- microphone_off();
-}
-
-soundbyte* microphone() {
- if(!capture_device)
- return NULL;
-
- ALCint samps;
- alcGetIntegerv(capture_device, ALC_CAPTURE_SAMPLES, 1, &samps);
-
- ALenum e = alcGetError(capture_device);
- if(e) {
- err("microphone", alcErrorString(e));
- return NULL;
- }
-
- // Resize if needed
- if(samps > capture_samples_available) {
- if(capture_soundbyte.data)
- delete capture_soundbyte.data;
- capture_soundbyte.data = new ALshort[samps];
- capture_samples_available = samps;
- }
-
- capture_samples_length = samps;
-
- alcCaptureSamples(capture_device, capture_soundbyte.data, samps);
-
- capture_soundbyte.length = capture_samples_length;
- capture_soundbyte.frequency = CAPTURE_FREQ;
- return &capture_soundbyte;
-}
View
@@ -27,18 +27,14 @@ std::vector<ALuint> loaded_sounds;
std::vector<ALuint> loaded_buffers;
int alure_state = 0;
-ALCdevice* capture_device = NULL;
-unsigned int capture_samples_available = 0; // Samples in cache
-unsigned int capture_samples_length = 0; // Last number of captured samples
-soundbyte capture_soundbyte;
-
int midi_status = 0;
fluid_settings_t* midi_settings = NULL;
fluid_synth_t* midi_synth = NULL;
fluid_audio_driver_t* midi_device = NULL;
#include "audio.cpp"
+#include "microphone.cpp"
#include "soundbyte.cpp"
#include "buffer.cpp"
#include "sound.cpp"
View
@@ -0,0 +1,73 @@
+#define CAPTURE_FREQ 44100
+#define CAPTURE_BUF_SIZE 22050
+
+microphone::microphone() : _device(NULL), _samples_available(0), _samples_length(0) {
+ if(alure_state == 0)
+ audio_on();
+
+ if(!_device) {
+ alGetError();
+
+ _device = alcCaptureOpenDevice(NULL, CAPTURE_FREQ, AL_FORMAT_MONO16, CAPTURE_BUF_SIZE);
+
+ if(!_device) {
+ err("microphone", alErrorString(alGetError()));
+ return;
+ }
+
+ ALenum e;
+ e = alcGetError(_device);
+ if(e != AL_NO_ERROR)
+ err("microphone", alcErrorString(e));
+ }
+
+ _soundbyte.data = NULL;
+ _soundbyte.frequency = CAPTURE_FREQ;
+}
+
+microphone::~microphone() {
+ if(!_device)
+ return;
+
+ alcCaptureStop(_device);
+ alcCaptureCloseDevice(_device);
+
+ _device = NULL;
+}
+
+void microphone::enable(bool a) {
+ if(a)
+ alcCaptureStart(_device);
+ else
+ alcCaptureStop(_device);
+}
+
+soundbyte* microphone::step() {
+ if(!_device)
+ return NULL;
+
+ ALCint samps;
+ alcGetIntegerv(_device, ALC_CAPTURE_SAMPLES, 1, &samps);
+
+ ALenum e = alcGetError(_device);
+ if(e) {
+ err("microphone", alcErrorString(e));
+ return NULL;
+ }
+
+ // Resize if needed
+ if(samps > _samples_available) {
+ if(_soundbyte.data)
+ delete _soundbyte.data;
+ _soundbyte.data = new ALshort[samps];
+ _samples_available = samps;
+ }
+
+ _samples_length = samps;
+
+ alcCaptureSamples(_device, _soundbyte.data, samps);
+
+ _soundbyte.length = _samples_length;
+ _soundbyte.frequency = CAPTURE_FREQ;
+ return &_soundbyte;
+}
@@ -395,8 +395,17 @@ struct midi {
int _channel;//x
};
-void microphone(bool);
-soundbyte* microphone();
+struct microphone {
+ microphone();
+ ~microphone();
+ void enable(bool = true);
+ soundbyte* step();
+
+ ALCdevice* _device;//x
+ unsigned int _samples_available;//x
+ unsigned int _samples_length;//x
+ soundbyte _soundbyte;//x
+};
#endif

0 comments on commit 0fa6d55

Please sign in to comment.