1 module armos.audio.recorder;
2 
3 import derelict.openal.al;
4 /++
5 +/
6 class Recorder {
7     public{
8         ///
9         this(uint rate = 44100, uint size = 1024){
10             DerelictAL.load();
11             _rate = rate;
12             _size = size;
13             _device = alcCaptureOpenDevice(null, _rate, AL_FORMAT_STEREO16, _size);
14         }
15 
16         ~this(){
17             stop;
18             alcCaptureCloseDevice(_device);
19         }
20 
21         ///
22         string usableDevice(){
23             import std.conv;
24             return alcGetString(null, ALC_CAPTURE_DEVICE_SPECIFIER).to!string;
25         }
26 
27         ///
28         string currentDevice(){
29             import std.conv;
30             assert(_device, "No capture device selected.");
31             return alcGetString(_device, ALC_CAPTURE_DEVICE_SPECIFIER).to!string;
32         }
33 
34         ///
35         const(byte[]) buffer()const{
36             return _buffer;
37         }
38 
39         ///
40         int samples()const{
41             return _samples;
42         }
43 
44         ///
45         Recorder update(){
46             alcGetIntegerv(_device, ALC_CAPTURE_SAMPLES, cast(ALCsizei)(ALint.sizeof), &_samples);
47             if(_samples > 0){
48                 _buffer = new byte[_samples];
49                 alcCaptureSamples(_device, _buffer.ptr, _samples/2);
50             }else{
51                 _buffer = [];
52             }
53             return this;
54         }
55 
56         ///
57         Recorder start(){
58             alcCaptureStart(_device);
59             return this;
60         }
61 
62         ///
63         Recorder stop(){
64             alcCaptureStop(_device);
65             return this;
66         }
67     }//public
68 
69     private{
70         ALCdevice*  _device;
71         int _samples;
72         byte[] _buffer;
73         uint _rate;
74         uint _size;
75     }//private
76 }//class Recorder