Commit fb065187 authored by bellard's avatar bellard

audio clean up (initial patch by malc)


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1131 c046a42c-6fe2-441c-8c8c-71466251a162
parent bf71c9d9
include config.mak
#After enabling Adlib and/or FMOD rebuild QEMU from scratch
#Uncomment following for adlib support
#USE_ADLIB=1
#Uncomment following and specify proper paths/names for FMOD support
#USE_FMOD=1
#FMOD_INCLUDE=/net/include/fmod
......@@ -278,11 +274,18 @@ VL_OBJS=vl.o osdep.o block.o readline.o monitor.o pci.o console.o
VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
SOUND_HW = sb16.o
AUDIODRV = audio.o ossaudio.o sdlaudio.o wavaudio.o
AUDIODRV = audio.o wavaudio.o
ifdef CONFIG_SDL
AUDIODRV += sdlaudio.o
endif
ifdef CONFIG_OSS
AUDIODRV += ossaudio.o
endif
pc.o: DEFINES := -DUSE_SB16 $(DEFINES)
ifeq ($(USE_ADLIB),1)
ifdef CONFIG_ADLIB
SOUND_HW += fmopl.o adlib.o
audio.o: DEFINES := -DUSE_ADLIB $(DEFINES)
endif
ifeq ($(USE_FMOD),1)
......
......@@ -22,62 +22,26 @@
* THE SOFTWARE.
*/
#include <assert.h>
#include <limits.h>
#include "vl.h"
#define AUDIO_CAP "audio"
#include "audio/audio.h"
#define USE_SDL_AUDIO
#define USE_WAV_AUDIO
#if defined __linux__ || (defined _BSD && !defined __APPLE__)
#define USE_OSS_AUDIO
#endif
#ifdef USE_OSS_AUDIO
#include "audio/ossaudio.h"
#endif
#ifdef USE_SDL_AUDIO
#include "audio/sdlaudio.h"
#endif
#ifdef USE_WAV_AUDIO
#include "audio/wavaudio.h"
#endif
#include "audio/audio_int.h"
#ifdef USE_FMOD_AUDIO
#include "audio/fmodaudio.h"
#define dolog(...) AUD_log ("audio", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
#define QC_AUDIO_DRV "QEMU_AUDIO_DRV"
#define QC_VOICES "QEMU_VOICES"
#define QC_VOICES "QEMU_VOICES"
#define QC_FIXED_FORMAT "QEMU_FIXED_FORMAT"
#define QC_FIXED_FREQ "QEMU_FIXED_FREQ"
extern void SB16_init (void);
#ifdef USE_ADLIB
extern void Adlib_init (void);
#endif
#ifdef USE_GUS
extern void GUS_init (void);
#endif
static void (*hw_ctors[]) (void) = {
SB16_init,
#ifdef USE_ADLIB
Adlib_init,
#endif
#ifdef USE_GUS
GUS_init,
#endif
NULL
};
static HWVoice *hw_voice;
static HWVoice *hw_voices;
AudioState audio_state = {
1, /* use fixed settings */
......@@ -127,9 +91,10 @@ const char *audio_get_conf_str (const char *key, const char *defval)
return val;
}
void audio_log (const char *fmt, ...)
void AUD_log (const char *cap, const char *fmt, ...)
{
va_list ap;
fprintf (stderr, "%s: ", cap);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
......@@ -403,7 +368,7 @@ int pcm_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
static int dist (void *hw)
{
if (hw) {
return (((uint8_t *) hw - (uint8_t *) hw_voice)
return (((uint8_t *) hw - (uint8_t *) hw_voices)
/ audio_state.voice_size) + 1;
}
else {
......@@ -411,7 +376,7 @@ static int dist (void *hw)
}
}
#define ADVANCE(hw) hw ? advance (hw, audio_state.voice_size) : hw_voice
#define ADVANCE(hw) hw ? advance (hw, audio_state.voice_size) : hw_voices
HWVoice *pcm_hw_find_any (HWVoice *hw)
{
......@@ -648,6 +613,21 @@ SWVoice *AUD_open (SWVoice *sw, const char *name,
return sw;
}
void AUD_close (SWVoice *sw)
{
if (!sw)
return;
pcm_sw_fini (sw);
pcm_hw_del_sw (sw->hw, sw);
pcm_hw_gc (sw->hw);
if (sw->name) {
qemu_free (sw->name);
sw->name = NULL;
}
qemu_free (sw);
}
int AUD_write (SWVoice *sw, void *buf, int size)
{
int bytes;
......@@ -797,13 +777,13 @@ void AUD_enable (SWVoice *sw, int on)
}
static struct audio_output_driver *drvtab[] = {
#ifdef USE_OSS_AUDIO
#ifdef CONFIG_OSS
&oss_output_driver,
#endif
#ifdef USE_FMOD_AUDIO
&fmod_output_driver,
#endif
#ifdef USE_SDL_AUDIO
#ifdef CONFIG_SDL
&sdl_output_driver,
#endif
#ifdef USE_WAV_AUDIO
......@@ -821,8 +801,8 @@ static int voice_init (struct audio_output_driver *drv)
drv->name, audio_state.nb_hw_voices, drv->max_voices);
audio_state.nb_hw_voices = drv->max_voices;
}
hw_voice = qemu_mallocz (audio_state.nb_hw_voices * drv->voice_size);
if (hw_voice) {
hw_voices = qemu_mallocz (audio_state.nb_hw_voices * drv->voice_size);
if (hw_voices) {
audio_state.drv = drv;
return 1;
}
......@@ -928,8 +908,4 @@ void AUD_init (void)
dolog ("Can not initialize audio subsystem\n");
return;
}
for (i = 0; hw_ctors[i]; i++) {
hw_ctors[i] ();
}
}
......@@ -26,13 +26,6 @@
#include "mixeng.h"
#define dolog(...) fprintf (stderr, AUDIO_CAP ": " __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
typedef enum {
AUD_FMT_U8,
AUD_FMT_S8,
......@@ -40,130 +33,14 @@ typedef enum {
AUD_FMT_S16
} audfmt_e;
typedef struct HWVoice HWVoice;
struct audio_output_driver;
typedef struct AudioState {
int fixed_format;
int fixed_freq;
int fixed_channels;
int fixed_fmt;
int nb_hw_voices;
int voice_size;
int64_t ticks_threshold;
int freq_threshold;
void *opaque;
struct audio_output_driver *drv;
} AudioState;
extern AudioState audio_state;
typedef struct SWVoice {
int freq;
audfmt_e fmt;
int nchannels;
int shift;
int align;
t_sample *conv;
int left;
int pos;
int bytes_per_second;
int64_t ratio;
st_sample_t *buf;
void *rate;
int wpos;
int live;
int active;
int64_t old_ticks;
HWVoice *hw;
char *name;
} SWVoice;
#define VOICE_ENABLE 1
#define VOICE_DISABLE 2
struct pcm_ops {
int (*init) (HWVoice *hw, int freq, int nchannels, audfmt_e fmt);
void (*fini) (HWVoice *hw);
void (*run) (HWVoice *hw);
int (*write) (SWVoice *sw, void *buf, int size);
int (*ctl) (HWVoice *hw, int cmd, ...);
};
struct audio_output_driver {
const char *name;
void *(*init) (void);
void (*fini) (void *);
struct pcm_ops *pcm_ops;
int can_be_default;
int max_voices;
int voice_size;
};
struct HWVoice {
int active;
int enabled;
int pending_disable;
int valid;
int freq;
f_sample *clip;
audfmt_e fmt;
int nchannels;
int align;
int shift;
int rpos;
int bufsize;
int bytes_per_second;
st_sample_t *mix_buf;
int samples;
int64_t old_ticks;
int nb_voices;
struct SWVoice **pvoice;
struct pcm_ops *pcm_ops;
};
void audio_log (const char *fmt, ...);
void pcm_sw_free_resources (SWVoice *sw);
int pcm_sw_alloc_resources (SWVoice *sw);
void pcm_sw_fini (SWVoice *sw);
int pcm_sw_init (SWVoice *sw, HWVoice *hw, int freq,
int nchannels, audfmt_e fmt);
void pcm_hw_clear (HWVoice *hw, void *buf, int len);
HWVoice * pcm_hw_find_any (HWVoice *hw);
HWVoice * pcm_hw_find_any_active (HWVoice *hw);
HWVoice * pcm_hw_find_any_passive (HWVoice *hw);
HWVoice * pcm_hw_find_specific (HWVoice *hw, int freq,
int nchannels, audfmt_e fmt);
HWVoice * pcm_hw_add (int freq, int nchannels, audfmt_e fmt);
int pcm_hw_add_sw (HWVoice *hw, SWVoice *sw);
int pcm_hw_del_sw (HWVoice *hw, SWVoice *sw);
SWVoice * pcm_create_voice_pair (int freq, int nchannels, audfmt_e fmt);
void pcm_hw_free_resources (HWVoice *hw);
int pcm_hw_alloc_resources (HWVoice *hw);
void pcm_hw_fini (HWVoice *hw);
void pcm_hw_gc (HWVoice *hw);
int pcm_hw_get_live (HWVoice *hw);
int pcm_hw_get_live2 (HWVoice *hw, int *nb_active);
void pcm_hw_dec_live (HWVoice *hw, int decr);
int pcm_hw_write (SWVoice *sw, void *buf, int len);
int audio_get_conf_int (const char *key, int defval);
const char *audio_get_conf_str (const char *key, const char *defval);
typedef struct SWVoice SWVoice;
/* Public API */
SWVoice * AUD_open (SWVoice *sw, const char *name, int freq,
int nchannels, audfmt_e fmt);
void AUD_init (void);
void AUD_log (const char *cap, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));;
void AUD_close (SWVoice *sw);
int AUD_write (SWVoice *sw, void *pcm_buf, int size);
void AUD_adjust (SWVoice *sw, int leftover);
void AUD_reset (SWVoice *sw);
......
/*
* QEMU Audio subsystem header
*
* Copyright (c) 2003-2004 Vassili Karpov (malc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef QEMU_AUDIO_INT_H
#define QEMU_AUDIO_INT_H
#include "vl.h"
struct pcm_ops;
typedef struct HWVoice {
int active;
int enabled;
int pending_disable;
int valid;
int freq;
f_sample *clip;
audfmt_e fmt;
int nchannels;
int align;
int shift;
int rpos;
int bufsize;
int bytes_per_second;
st_sample_t *mix_buf;
int samples;
int64_t old_ticks;
int nb_voices;
struct SWVoice **pvoice;
struct pcm_ops *pcm_ops;
} HWVoice;
extern struct pcm_ops oss_pcm_ops;
extern struct audio_output_driver oss_output_driver;
extern struct pcm_ops sdl_pcm_ops;
extern struct audio_output_driver sdl_output_driver;
extern struct pcm_ops wav_pcm_ops;
extern struct audio_output_driver wav_output_driver;
extern struct pcm_ops fmod_pcm_ops;
extern struct audio_output_driver fmod_output_driver;
struct audio_output_driver {
const char *name;
void *(*init) (void);
void (*fini) (void *);
struct pcm_ops *pcm_ops;
int can_be_default;
int max_voices;
int voice_size;
};
typedef struct AudioState {
int fixed_format;
int fixed_freq;
int fixed_channels;
int fixed_fmt;
int nb_hw_voices;
int voice_size;
int64_t ticks_threshold;
int freq_threshold;
void *opaque;
struct audio_output_driver *drv;
} AudioState;
extern AudioState audio_state;
struct SWVoice {
int freq;
audfmt_e fmt;
int nchannels;
int shift;
int align;
t_sample *conv;
int left;
int pos;
int bytes_per_second;
int64_t ratio;
st_sample_t *buf;
void *rate;
int wpos;
int live;
int active;
int64_t old_ticks;
HWVoice *hw;
char *name;
};
struct pcm_ops {
int (*init) (HWVoice *hw, int freq, int nchannels, audfmt_e fmt);
void (*fini) (HWVoice *hw);
void (*run) (HWVoice *hw);
int (*write) (SWVoice *sw, void *buf, int size);
int (*ctl) (HWVoice *hw, int cmd, ...);
};
void pcm_sw_free_resources (SWVoice *sw);
int pcm_sw_alloc_resources (SWVoice *sw);
void pcm_sw_fini (SWVoice *sw);
int pcm_sw_init (SWVoice *sw, HWVoice *hw, int freq,
int nchannels, audfmt_e fmt);
void pcm_hw_clear (HWVoice *hw, void *buf, int len);
HWVoice * pcm_hw_find_any (HWVoice *hw);
HWVoice * pcm_hw_find_any_active (HWVoice *hw);
HWVoice * pcm_hw_find_any_passive (HWVoice *hw);
HWVoice * pcm_hw_find_specific (HWVoice *hw, int freq,
int nchannels, audfmt_e fmt);
HWVoice * pcm_hw_add (int freq, int nchannels, audfmt_e fmt);
int pcm_hw_add_sw (HWVoice *hw, SWVoice *sw);
int pcm_hw_del_sw (HWVoice *hw, SWVoice *sw);
SWVoice * pcm_create_voice_pair (int freq, int nchannels, audfmt_e fmt);
void pcm_hw_free_resources (HWVoice *hw);
int pcm_hw_alloc_resources (HWVoice *hw);
void pcm_hw_fini (HWVoice *hw);
void pcm_hw_gc (HWVoice *hw);
int pcm_hw_get_live (HWVoice *hw);
int pcm_hw_get_live2 (HWVoice *hw, int *nb_active);
void pcm_hw_dec_live (HWVoice *hw, int decr);
int pcm_hw_write (SWVoice *sw, void *buf, int len);
int audio_get_conf_int (const char *key, int defval);
const char *audio_get_conf_str (const char *key, const char *defval);
struct audio_output_driver;
#define VOICE_ENABLE 1
#define VOICE_DISABLE 2
#endif /* audio_int.h */
......@@ -25,9 +25,22 @@
#include <fmod_errors.h>
#include "vl.h"
#define AUDIO_CAP "fmod"
#include "audio/audio.h"
#include "audio/fmodaudio.h"
#include "audio/audio_int.h"
typedef struct FMODVoice {
HWVoice hw;
unsigned int old_pos;
FSOUND_SAMPLE *fmod_sample;
int channel;
} FMODVoice;
#define dolog(...) AUD_log ("fmod", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
#define QC_FMOD_DRV "QEMU_FMOD_DRV"
#define QC_FMOD_FREQ "QEMU_FMOD_FREQ"
......
/*
* QEMU FMOD audio output driver header
*
* Copyright (c) 2004 Vassili Karpov (malc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef QEMU_FMODAUDIO_H
#define QEMU_FMODAUDIO_H
#include <fmod.h>
typedef struct FMODVoice {
struct HWVoice hw;
unsigned int old_pos;
FSOUND_SAMPLE *fmod_sample;
int channel;
} FMODVoice;
extern struct pcm_ops fmod_pcm_ops;
extern struct audio_output_driver fmod_output_driver;
#endif /* fmodaudio.h */
......@@ -21,20 +21,32 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* Temporary kludge */
#if defined __linux__ || (defined _BSD && !defined __APPLE__)
#include <assert.h>
#include "vl.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <assert.h>
#include "vl.h"
#include "audio/audio_int.h"
typedef struct OSSVoice {
HWVoice hw;
void *pcm_buf;
int fd;
int nfrags;
int fragsize;
int mmapped;
int old_optr;
} OSSVoice;
#define AUDIO_CAP "oss"
#include "audio/audio.h"
#include "audio/ossaudio.h"
#define dolog(...) AUD_log ("oss", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
#define QC_OSS_FRAGSIZE "QEMU_OSS_FRAGSIZE"
#define QC_OSS_NFRAGS "QEMU_OSS_NFRAGS"
......@@ -463,4 +475,3 @@ struct audio_output_driver oss_output_driver = {
INT_MAX,
sizeof (OSSVoice)
};
#endif
......@@ -25,9 +25,18 @@
#include <SDL/SDL_thread.h>
#include "vl.h"
#define AUDIO_CAP "sdl"
#include "audio/audio.h"
#include "audio/sdlaudio.h"
#include "audio/audio_int.h"
typedef struct SDLVoice {
HWVoice hw;
} SDLVoice;
#define dolog(...) AUD_log ("sdl", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
#define QC_SDL_SAMPLES "QEMU_SDL_SAMPLES"
......
/*
* QEMU SDL audio output driver header
*
* Copyright (c) 2004 Vassili Karpov (malc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef QEMU_SDLAUDIO_H
#define QEMU_SDLAUDIO_H
typedef struct SDLVoice {
struct HWVoice hw;
} SDLVoice;
extern struct pcm_ops sdl_pcm_ops;
extern struct audio_output_driver sdl_output_driver;
#endif /* sdlaudio.h */
......@@ -23,9 +23,22 @@
*/
#include "vl.h"
#define AUDIO_CAP "wav"
#include "audio/audio.h"
#include "audio/wavaudio.h"
#include "audio/audio_int.h"
typedef struct WAVVoice {
HWVoice hw;
QEMUFile *f;
int64_t old_ticks;
void *pcm_buf;
int total_samples;
} WAVVoice;
#define dolog(...) AUD_log ("wav", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
static struct {
const char *wav_path;
......
/*
* QEMU WAV audio output driver header
*
* Copyright (c) 2004 Vassili Karpov (malc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef QEMU_WAVAUDIO_H
#define QEMU_WAVAUDIO_H
typedef struct WAVVoice {
struct HWVoice hw;
QEMUFile *f;
int64_t old_ticks;
void *pcm_buf;
int total_samples;
} WAVVoice;
extern struct pcm_ops wav_pcm_ops;
extern struct audio_output_driver wav_output_driver;
#endif /* wavaudio.h */
......@@ -72,6 +72,8 @@ mingw32="no"
EXESUF=""
gdbstub="yes"
slirp="yes"
adlib="no"
oss="no"
# OS specific
targetos=`uname -s`
......@@ -81,18 +83,23 @@ mingw32="yes"
;;
FreeBSD)
bsd="yes"
oss="yes"
;;
NetBSD)
bsd="yes"
oss="yes"
;;
OpenBSD)
bsd="yes"
oss="yes"
;;
Darwin)
bsd="yes"
darwin="yes"
;;
*) ;;
*)
oss="yes"