2014-11-24 21:25:14 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// $Id$
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-02-05 11:33:03 +03:00
|
|
|
// Copyright (C) 2011-2017 The Bochs Project
|
2014-11-24 21:25:14 +03:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
// Common code for sound lowlevel modules
|
|
|
|
|
2015-05-09 11:31:49 +03:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
#if BX_HAVE_LIBSAMPLERATE
|
|
|
|
#include <samplerate.h>
|
|
|
|
#elif BX_HAVE_SOXR_LSR
|
|
|
|
#include <soxr-lsr.h>
|
|
|
|
#endif
|
|
|
|
|
2015-01-22 23:28:14 +03:00
|
|
|
// This is the maximum size of a wave data packet.
|
|
|
|
// It should be large enough for 0.1 seconds of playback or recording.
|
|
|
|
#define BX_SOUNDLOW_WAVEPACKETSIZE 19200
|
2014-11-24 21:25:14 +03:00
|
|
|
|
|
|
|
// Definitions for the output functions
|
|
|
|
#define BX_SOUNDLOW_OK 0
|
|
|
|
#define BX_SOUNDLOW_ERR 1
|
|
|
|
|
2015-01-11 23:13:50 +03:00
|
|
|
#define BX_MAX_WAVE_CALLBACKS 3
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Bit16u samplerate;
|
|
|
|
Bit8u bits;
|
2015-01-27 00:10:51 +03:00
|
|
|
Bit8u channels;
|
2015-01-11 23:13:50 +03:00
|
|
|
Bit8u format;
|
2015-01-27 22:33:28 +03:00
|
|
|
Bit16u volume;
|
2015-01-11 23:13:50 +03:00
|
|
|
} bx_pcm_param_t;
|
2015-01-08 22:12:01 +03:00
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
const bx_pcm_param_t default_pcm_param = { 44100, 16, 2, 1, 0xffff };
|
2015-01-27 00:10:51 +03:00
|
|
|
|
2014-11-24 21:25:14 +03:00
|
|
|
typedef Bit32u (*sound_record_handler_t)(void *arg, Bit32u len);
|
2015-01-08 22:12:01 +03:00
|
|
|
typedef Bit32u (*get_wave_cb_t)(void *arg, Bit16u rate, Bit8u *buffer, Bit32u len);
|
2014-11-24 21:25:14 +03:00
|
|
|
|
2015-01-31 01:52:56 +03:00
|
|
|
// audio buffer support
|
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
#define BUFTYPE_FLOAT 0
|
|
|
|
#define BUFTYPE_UCHAR 1
|
|
|
|
|
2015-01-31 01:52:56 +03:00
|
|
|
typedef struct _audio_buffer_t
|
|
|
|
{
|
|
|
|
Bit32u size, pos;
|
2017-02-05 11:33:03 +03:00
|
|
|
union {
|
|
|
|
Bit8u *data;
|
|
|
|
float *fdata;
|
|
|
|
};
|
2015-06-04 11:58:02 +03:00
|
|
|
bx_pcm_param_t param;
|
2015-01-31 01:52:56 +03:00
|
|
|
struct _audio_buffer_t *next;
|
|
|
|
} audio_buffer_t;
|
|
|
|
|
2015-05-15 23:18:47 +03:00
|
|
|
class bx_audio_buffer_c {
|
|
|
|
public:
|
2017-02-05 11:33:03 +03:00
|
|
|
bx_audio_buffer_c(Bit8u format);
|
2015-05-15 23:18:47 +03:00
|
|
|
~bx_audio_buffer_c();
|
|
|
|
|
|
|
|
audio_buffer_t *new_buffer(Bit32u size);
|
|
|
|
audio_buffer_t *get_buffer();
|
|
|
|
void delete_buffer();
|
|
|
|
private:
|
2017-02-05 11:33:03 +03:00
|
|
|
Bit8u format;
|
2015-05-15 23:18:47 +03:00
|
|
|
audio_buffer_t *root;
|
|
|
|
};
|
2015-01-31 01:52:56 +03:00
|
|
|
|
2015-06-04 11:58:02 +03:00
|
|
|
extern bx_audio_buffer_c *audio_buffers[2];
|
2017-02-05 11:33:03 +03:00
|
|
|
void convert_float_to_s16le(float *src, unsigned srcsize, Bit8u *dst);
|
2015-01-31 01:52:56 +03:00
|
|
|
Bit32u pcm_callback(void *dev, Bit16u rate, Bit8u *buffer, Bit32u len);
|
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
extern int resampler_control;
|
2015-02-18 19:34:53 +03:00
|
|
|
extern int mixer_control;
|
2017-02-05 11:33:03 +03:00
|
|
|
extern BX_MUTEX(resampler_mutex);
|
2016-08-12 20:06:14 +03:00
|
|
|
#ifndef ANDROID
|
2015-02-01 23:50:20 +03:00
|
|
|
extern BX_MUTEX(mixer_mutex);
|
2016-08-12 20:06:14 +03:00
|
|
|
#endif
|
2015-02-01 23:50:20 +03:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
// the waveout class
|
|
|
|
|
|
|
|
class bx_soundlow_waveout_c : public logfunctions {
|
|
|
|
public:
|
|
|
|
bx_soundlow_waveout_c();
|
|
|
|
virtual ~bx_soundlow_waveout_c();
|
|
|
|
|
|
|
|
virtual int openwaveoutput(const char *wavedev);
|
|
|
|
virtual int set_pcm_params(bx_pcm_param_t *param);
|
|
|
|
virtual int sendwavepacket(int length, Bit8u data[], bx_pcm_param_t *src_param);
|
|
|
|
virtual int get_packetsize();
|
|
|
|
virtual int output(int length, Bit8u data[]);
|
2015-02-18 19:34:53 +03:00
|
|
|
virtual int closewaveoutput();
|
2015-02-13 14:30:46 +03:00
|
|
|
|
|
|
|
virtual int register_wave_callback(void *, get_wave_cb_t wd_cb);
|
|
|
|
virtual void unregister_wave_callback(int callback_id);
|
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
virtual void resampler(audio_buffer_t *inbuffer, audio_buffer_t *outbuffer);
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
virtual bx_bool mixer_common(Bit8u *buffer, int len);
|
2017-02-05 11:33:03 +03:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
protected:
|
2017-02-05 11:33:03 +03:00
|
|
|
void start_resampler_thread(void);
|
2015-02-13 14:30:46 +03:00
|
|
|
void start_mixer_thread(void);
|
2017-02-05 11:33:03 +03:00
|
|
|
Bit32u resampler_common(audio_buffer_t *inbuffer, float **fbuffer);
|
2015-02-13 14:30:46 +03:00
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
bx_pcm_param_t real_pcm_param;
|
|
|
|
#if BX_HAVE_LIBSAMPLERATE || BX_HAVE_SOXR_LSR
|
|
|
|
SRC_STATE *src_state;
|
|
|
|
#endif
|
2015-02-13 14:30:46 +03:00
|
|
|
|
|
|
|
int cb_count;
|
|
|
|
struct {
|
|
|
|
void *device;
|
|
|
|
get_wave_cb_t cb;
|
|
|
|
} get_wave[BX_MAX_WAVE_CALLBACKS];
|
|
|
|
int pcm_callback_id;
|
|
|
|
};
|
|
|
|
|
2015-02-14 20:25:39 +03:00
|
|
|
// the wavein class
|
|
|
|
|
|
|
|
class bx_soundlow_wavein_c : public logfunctions {
|
|
|
|
public:
|
|
|
|
bx_soundlow_wavein_c();
|
|
|
|
virtual ~bx_soundlow_wavein_c();
|
|
|
|
|
|
|
|
virtual int openwaveinput(const char *wavedev, sound_record_handler_t rh);
|
|
|
|
virtual int startwaverecord(bx_pcm_param_t *param);
|
|
|
|
virtual int getwavepacket(int length, Bit8u data[]);
|
|
|
|
virtual int stopwaverecord();
|
|
|
|
|
|
|
|
static void record_timer_handler(void *);
|
|
|
|
void record_timer(void);
|
|
|
|
protected:
|
|
|
|
int record_timer_index;
|
|
|
|
int record_packet_size;
|
|
|
|
sound_record_handler_t record_handler;
|
|
|
|
};
|
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
// the midiout class
|
|
|
|
|
|
|
|
class bx_soundlow_midiout_c : public logfunctions {
|
2014-11-24 21:25:14 +03:00
|
|
|
public:
|
2015-02-15 21:32:36 +03:00
|
|
|
bx_soundlow_midiout_c();
|
|
|
|
virtual ~bx_soundlow_midiout_c();
|
|
|
|
|
|
|
|
virtual int openmidioutput(const char *mididev);
|
|
|
|
virtual int midiready();
|
|
|
|
virtual int sendmidicommand(int delta, int command, int length, Bit8u data[]);
|
2015-02-17 21:28:25 +03:00
|
|
|
virtual int closemidioutput();
|
2015-02-15 21:32:36 +03:00
|
|
|
};
|
2014-11-24 21:25:14 +03:00
|
|
|
|
2015-03-10 23:56:44 +03:00
|
|
|
// This is the base class of the sound lowlevel support.
|
2015-02-15 21:32:36 +03:00
|
|
|
// the lowlevel sound driver class returns pointers to the child objects
|
2014-11-24 21:25:14 +03:00
|
|
|
|
2015-02-15 21:32:36 +03:00
|
|
|
class bx_sound_lowlevel_c : public logfunctions {
|
|
|
|
public:
|
2014-11-24 21:25:14 +03:00
|
|
|
bx_sound_lowlevel_c();
|
|
|
|
virtual ~bx_sound_lowlevel_c();
|
|
|
|
|
2015-03-10 23:56:44 +03:00
|
|
|
virtual bx_soundlow_waveout_c* get_waveout() {return NULL;}
|
|
|
|
virtual bx_soundlow_wavein_c* get_wavein() {return NULL;}
|
|
|
|
virtual bx_soundlow_midiout_c* get_midiout() {return NULL;}
|
2014-11-24 21:25:14 +03:00
|
|
|
|
|
|
|
protected:
|
2015-02-13 14:30:46 +03:00
|
|
|
bx_soundlow_waveout_c *waveout;
|
2015-02-14 20:25:39 +03:00
|
|
|
bx_soundlow_wavein_c *wavein;
|
2015-02-15 21:32:36 +03:00
|
|
|
bx_soundlow_midiout_c *midiout;
|
2014-11-24 21:25:14 +03:00
|
|
|
};
|
2015-03-10 23:56:44 +03:00
|
|
|
|
|
|
|
// the dummy sound driver contains stubs for all features
|
|
|
|
|
|
|
|
class bx_sound_dummy_c : public bx_sound_lowlevel_c {
|
|
|
|
public:
|
|
|
|
bx_sound_dummy_c() {}
|
|
|
|
virtual ~bx_sound_dummy_c() {}
|
|
|
|
|
|
|
|
virtual bx_soundlow_waveout_c* get_waveout();
|
|
|
|
virtual bx_soundlow_wavein_c* get_wavein();
|
|
|
|
virtual bx_soundlow_midiout_c* get_midiout();
|
|
|
|
};
|