Make some static variables in audioapi functions fields of audioapi class
[lsnes.git] / include / core / audioapi.hpp
blob107ba83b3349a9dd3d800cd12eb7370e8e5c604e
1 #ifndef _audioapi__hpp__included__
2 #define _audioapi__hpp__included__
4 #include "library/threads.hpp"
6 #include <map>
7 #include <cstdint>
8 #include <cstdlib>
9 #include <string>
10 #include <stdexcept>
12 class audioapi_instance
14 public:
15 /**
16 * Audio API music buffer.
18 struct buffer
20 /**
21 * The samples.
23 * Note: May be NULL if no samples are available..
25 int16_t* samples;
26 /**
27 * Playback pointer in samples structure.
29 size_t pointer;
30 /**
31 * Total number of samples in this buffer.
33 size_t total;
34 /**
35 * True if buffer is stereo, false if mono.
37 bool stereo;
38 /**
39 * The rate in samples per second the buffer is supposed to be played at.
41 double rate;
44 /**
45 * Audio API VU calculator.
47 struct vumeter
49 /**
50 * Initialize.
52 vumeter();
53 /**
54 * Submit samples.
56 * Parameter samples: The samples to submit. If NULL, reads all samples as 0.
57 * Parameter count: Number of samples.
58 * Parameter stereo: If true, read only every other sample (but still read count samples).
59 * Parameter rate: Sound sampling rate.
60 * Parameter scale: Value to scale the samples by.
62 void operator()(float* samples, size_t count, bool stereo, double rate, double scale);
63 /**
64 * Get VU value in dB.
66 operator float() const throw() { return vu; }
67 private:
68 double accumulator;
69 size_t samples;
70 float vu;
71 void update_vu();
73 //Resampler.
74 class resampler
76 public:
77 resampler();
78 //After call, either insize or outsize is zero.
79 void resample(float*& in, size_t& insize, float*& out, size_t& outsize, double ratio, bool stereo);
80 private:
81 double position;
82 double vAl, vBl, vCl, vDl, vAr, vBr, vCr, vDr;
84 /**
85 * Ctor.
87 audioapi_instance();
88 ~audioapi_instance();
89 //The following are intended to be used by the emulator core.
90 /**
91 * Submit a buffer for playback on music channel.
93 * Parameter samples: The samples in the buffer. If stereo is true, each sample takes two elements (L, R).
94 * Parameter count: The number of samples in buffer.
95 * Parameter stereo: If true, the signal is stereo. If false, mono.
96 * Parameter rate: Rate of buffer in samples per second.
98 void submit_buffer(int16_t* samples, size_t count, bool stereo, double rate);
99 /**
100 * Get the voice channel playback/record rate.
102 * Returns: The rate in samples per second (first for recording, then for playback).
104 std::pair<unsigned,unsigned> voice_rate();
106 * Get the voice channel nominal playback/record rate.
108 * Returns: The rate in samples per second.
110 unsigned orig_voice_rate();
112 * Get the voice channel playback status register.
114 * Returns: The number of samples free for playback.
116 unsigned voice_p_status();
118 * Get the voice channel playback status register2.
120 * Returns: The number of samples in playback buffer.
122 unsigned voice_p_status2();
124 * Get the voice channel record status register.
126 * Returns: The number of samples in capture buffer.
128 unsigned voice_r_status();
130 * Play sound on voice channel.
132 * Parameter samples: The samples to play.
133 * Parameter count: Number of samples to play. Must be less than number of samples free for playback.
135 void play_voice(float* samples, size_t count);
137 * Capture sound on voice channel.
139 * Parameter samples: The buffer to store captured samples to.
140 * Parameter count: Number of samples to capture. Must be less than number of samples used for capture.
142 void record_voice(float* samples, size_t count);
144 * Init the audio. Call on emulator startup.
146 void init();
148 * Quit the audio. Call on emulator shutdown.
150 void quit();
152 * Set music volume.
154 * Parameter volume: The volume (0-1).
156 void music_volume(float volume);
158 * Get music volume.
160 * Returns: The music volume.
162 float music_volume();
164 * Set voice playback volume.
166 * Parameter volume: The volume (0-1).
168 void voicep_volume(float volume);
170 * Get voice playback volume.
172 * Returns: The voice playback volume.
174 float voicep_volume();
176 * Set voice capture volume.
178 * Parameter volume: The volume (0-1).
180 void voicer_volume(float volume);
182 * Get voice capture volume.
184 * Returns: The voice capture volume.
186 float voicer_volume();
188 //The following are intended to be used by the driver from the callback
190 * Get mixed music + voice buffer to play (at voice rate).
192 * Parameter samples: Buffer to store the samples to.
193 * Parameter count: Number of samples to generate.
194 * Parameter stereo: If true, return stereo buffer, else mono.
196 void get_mixed(int16_t* samples, size_t count, bool stereo);
198 * Get music channel buffer to play.
200 * Parameter played: Number of samples to ACK as played.
201 * Returns: Buffer to play.
203 * Note: This should only be called from the sound driver.
205 struct buffer get_music(size_t played);
207 * Get voice channel buffer to play.
209 * Parameter samples: The place to store the samples.
210 * Parameter count: Number of samples to fill.
212 * Note: This should only be called from the sound driver.
214 void get_voice(float* samples, size_t count);
216 * Put recorded voice channel buffer.
218 * Parameter samples: The samples to send. Can be NULL to send silence.
219 * Parameter count: Number of samples to send.
221 * Note: Even if audio driver does not support capture, one should send in silence.
222 * Note: This should only be called from the sound driver.
224 void put_voice(float* samples, size_t count);
226 * Set the voice channel playback/record rate.
228 * Parameter rate_r: The recording rate in samples per second.
229 * Parameter rate_p: The playback rate in samples per second.
231 * Note: This should only be called from the sound driver.
232 * Note: Setting rate to 0 enables dummy callbacks.
234 void voice_rate(unsigned rate_r, unsigned rate_p);
235 //Vu values.
236 vumeter vu_mleft;
237 vumeter vu_mright;
238 vumeter vu_vout;
239 vumeter vu_vin;
240 private:
241 struct dummy_cb_proc
243 dummy_cb_proc(audioapi_instance& _parent);
244 int operator()();
245 audioapi_instance& parent;
247 dummy_cb_proc dummyproc;
248 threads::thread* dummythread;
249 //3 music buffers is not enough due to huge blocksizes used by SDL.
250 const static unsigned MUSIC_BUFFERS = 8;
251 const static unsigned voicep_bufsize = 65536;
252 const static unsigned voicer_bufsize = 65536;
253 const static unsigned music_bufsize = 8192;
254 float voicep_buffer[voicep_bufsize];
255 float voicer_buffer[voicer_bufsize];
256 int16_t music_buffer[MUSIC_BUFFERS * music_bufsize];
257 volatile bool music_stereo[MUSIC_BUFFERS];
258 volatile double music_rate[MUSIC_BUFFERS];
259 volatile size_t music_size[MUSIC_BUFFERS];
260 unsigned music_ptr;
261 unsigned last_complete_music_seen;
262 volatile unsigned last_complete_music;
263 volatile unsigned voicep_get;
264 volatile unsigned voicep_put;
265 volatile unsigned voicer_get;
266 volatile unsigned voicer_put;
267 volatile unsigned voice_rate_play;
268 volatile unsigned orig_voice_rate_play;
269 volatile unsigned voice_rate_rec;
270 volatile bool dummy_cb_active_record;
271 volatile bool dummy_cb_active_play;
272 volatile bool dummy_cb_quit;
273 volatile float _music_volume;
274 volatile float _voicep_volume;
275 volatile float _voicer_volume;
276 resampler music_resampler;
277 bool last_adjust; //Adjusting consequtively is too hard.
282 //All the following need to be implemented by the sound driver itself
283 struct _audioapi_driver
285 //These correspond to various audioapi_driver_* functions.
286 void (*init)() throw();
287 void (*quit)() throw();
288 void (*enable)(bool enable);
289 bool (*initialized)();
290 void (*set_device)(const std::string& pdev, const std::string& rdev);
291 std::string (*get_device)(bool rec);
292 std::map<std::string, std::string> (*get_devices)(bool rec);
293 const char* (*name)();
296 struct audioapi_driver
298 audioapi_driver(struct _audioapi_driver driver);
303 * Initialize the driver.
305 void audioapi_driver_init() throw();
308 * Deinitialize the driver.
310 void audioapi_driver_quit() throw();
313 * Panic notification.
315 void audioapi_panicing() throw();
318 * Enable or disable sound.
320 * parameter enable: Enable sounds if true, otherwise disable sounds.
322 void audioapi_driver_enable(bool enable) throw();
325 * Has the sound system been successfully initialized?
327 * Returns: True if sound system has successfully initialized, false otherwise.
329 bool audioapi_driver_initialized();
332 * Set sound device (playback).
334 * - If new sound device is invalid, the sound device is not changed.
336 * Parameter pdev: The new sound device (playback).
337 * Parameter rdev: The new sound device (recording)
339 void audioapi_driver_set_device(const std::string& pdev, const std::string& rdev) throw(std::bad_alloc,
340 std::runtime_error);
343 * Get current sound device (playback).
345 * Returns: The current sound device.
347 std::string audioapi_driver_get_device(bool rec) throw(std::bad_alloc);
350 * Get available sound devices (playback).
352 * Returns: The map of devices. Keyed by name of the device, values are human-readable names for devices.
354 std::map<std::string, std::string> audioapi_driver_get_devices(bool rec) throw(std::bad_alloc);
357 * Identification for sound plugin.
359 const char* audioapi_driver_name() throw();
361 #endif