Delay committing fullscreen until seeing window size change
[lsnes.git] / include / core / audioapi.hpp
blob849156d9104b889ef3cd836451febb8375f8369d
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);
236 * Suppress all future VU updates.
238 static void disable_vu_updates();
239 //Vu values.
240 vumeter vu_mleft;
241 vumeter vu_mright;
242 vumeter vu_vout;
243 vumeter vu_vin;
244 private:
245 struct dummy_cb_proc
247 dummy_cb_proc(audioapi_instance& _parent);
248 int operator()();
249 audioapi_instance& parent;
251 dummy_cb_proc dummyproc;
252 threads::thread* dummythread;
253 //3 music buffers is not enough due to huge blocksizes used by SDL.
254 const static unsigned MUSIC_BUFFERS = 8;
255 const static unsigned voicep_bufsize = 65536;
256 const static unsigned voicer_bufsize = 65536;
257 const static unsigned music_bufsize = 8192;
258 float voicep_buffer[voicep_bufsize];
259 float voicer_buffer[voicer_bufsize];
260 int16_t music_buffer[MUSIC_BUFFERS * music_bufsize];
261 volatile bool music_stereo[MUSIC_BUFFERS];
262 volatile double music_rate[MUSIC_BUFFERS];
263 volatile size_t music_size[MUSIC_BUFFERS];
264 unsigned music_ptr;
265 unsigned last_complete_music_seen;
266 volatile unsigned last_complete_music;
267 volatile unsigned voicep_get;
268 volatile unsigned voicep_put;
269 volatile unsigned voicer_get;
270 volatile unsigned voicer_put;
271 volatile unsigned voice_rate_play;
272 volatile unsigned orig_voice_rate_play;
273 volatile unsigned voice_rate_rec;
274 volatile bool dummy_cb_active_record;
275 volatile bool dummy_cb_active_play;
276 volatile bool dummy_cb_quit;
277 volatile float _music_volume;
278 volatile float _voicep_volume;
279 volatile float _voicer_volume;
280 resampler music_resampler;
281 bool last_adjust; //Adjusting consequtively is too hard.
282 static bool vu_disabled;
286 #endif