Optimize busses
This commit is contained in:
parent
2731d01bc1
commit
8f6c8774de
|
@ -9,6 +9,8 @@
|
||||||
#include "music.h"
|
#include "music.h"
|
||||||
#include "stb_vorbis.h"
|
#include "stb_vorbis.h"
|
||||||
|
|
||||||
|
#include "stb_ds.h"
|
||||||
|
|
||||||
#include "mix.h"
|
#include "mix.h"
|
||||||
#include "dsp.h"
|
#include "dsp.h"
|
||||||
|
|
||||||
|
@ -28,6 +30,11 @@
|
||||||
#define TML_IMPLEMENTATION
|
#define TML_IMPLEMENTATION
|
||||||
#include "tml.h"
|
#include "tml.h"
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
char *key;
|
||||||
|
struct wav *value;
|
||||||
|
} *wavhash = NULL;
|
||||||
|
|
||||||
const char *audioDriver;
|
const char *audioDriver;
|
||||||
|
|
||||||
void new_samplerate(short *in, short *out, int n, int ch, int sr_in, int sr_out)
|
void new_samplerate(short *in, short *out, int n, int ch, int sr_in, int sr_out)
|
||||||
|
@ -150,20 +157,39 @@ void audio_close()
|
||||||
//Mix_CloseAudio();
|
//Mix_CloseAudio();
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct wav mwav;
|
|
||||||
|
|
||||||
struct wav *make_sound(const char *wav)
|
struct wav *make_sound(const char *wav)
|
||||||
{
|
{
|
||||||
|
int index = shgeti(wavhash, wav);
|
||||||
|
if (index != -1) return wavhash[index].value;
|
||||||
|
|
||||||
|
struct wav mwav;
|
||||||
mwav.data = drwav_open_file_and_read_pcm_frames_s16(wav, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
|
mwav.data = drwav_open_file_and_read_pcm_frames_s16(wav, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL);
|
||||||
|
|
||||||
if (mwav.samplerate != SAMPLERATE) {
|
if (mwav->samplerate != SAMPLERATE) {
|
||||||
YughInfo("Changing samplerate of %s.", wav);
|
YughInfo("Changing samplerate of %s.", wav);
|
||||||
mwav = change_samplerate(mwav, 48000);
|
mwav = change_samplerate(mwav, 48000);
|
||||||
}
|
}
|
||||||
|
|
||||||
mwav.gain = 1.f;
|
mwav.gain = 1.f;
|
||||||
|
|
||||||
return &mwav;
|
struct wav *newwav = malloc(sizeof(*newwav));
|
||||||
|
*newwav = mwav;
|
||||||
|
|
||||||
|
if (shlen(wavhash) == 0) sh_new_arena(wavhash);
|
||||||
|
|
||||||
|
shput(wavhash, wav, newwav);
|
||||||
|
|
||||||
|
return newwav;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_sound(const char *wav)
|
||||||
|
{
|
||||||
|
struct wav *w = shget(wavhash, wav);
|
||||||
|
if (w == NULL) return;
|
||||||
|
|
||||||
|
free(w->data);
|
||||||
|
free(w);
|
||||||
|
shdel(wavhash, wav);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct soundstream *soundstream_make()
|
struct soundstream *soundstream_make()
|
||||||
|
@ -173,6 +199,14 @@ struct soundstream *soundstream_make()
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void play_oneshot(struct wav *wav) {
|
||||||
|
struct sound *new = calloc(1, sizeof(*new));
|
||||||
|
new->data = wav;
|
||||||
|
new->bus = first_free_bus(dsp_filter(new, sound_fillbuf));
|
||||||
|
new->playing=1;
|
||||||
|
new->loop=0;
|
||||||
|
}
|
||||||
|
|
||||||
struct sound *play_sound(struct wav *wav)
|
struct sound *play_sound(struct wav *wav)
|
||||||
{
|
{
|
||||||
struct sound *new = calloc(1, sizeof(*new));
|
struct sound *new = calloc(1, sizeof(*new));
|
||||||
|
|
|
@ -29,9 +29,10 @@ struct soundconvstream {
|
||||||
|
|
||||||
struct soundstream *soundstream_make();
|
struct soundstream *soundstream_make();
|
||||||
|
|
||||||
|
/* A playing sound;
|
||||||
struct sound {
|
struct sound {
|
||||||
int loop;
|
int loop;
|
||||||
int frame;
|
unsigned int frame;
|
||||||
int playing;
|
int playing;
|
||||||
float gain;
|
float gain;
|
||||||
|
|
||||||
|
@ -39,6 +40,7 @@ struct sound {
|
||||||
struct bus *bus;
|
struct bus *bus;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Represents a sound file */
|
||||||
struct wav {
|
struct wav {
|
||||||
unsigned int ch;
|
unsigned int ch;
|
||||||
unsigned int samplerate;
|
unsigned int samplerate;
|
||||||
|
@ -62,6 +64,7 @@ void audio_close();
|
||||||
void sound_fillbuf(struct sound *s, short *buf, int n);
|
void sound_fillbuf(struct sound *s, short *buf, int n);
|
||||||
|
|
||||||
struct wav *make_sound(const char *wav);
|
struct wav *make_sound(const char *wav);
|
||||||
|
void free_sound(const char *wav);
|
||||||
void wav_norm_gain(struct wav *w, double lv);
|
void wav_norm_gain(struct wav *w, double lv);
|
||||||
struct sound *play_sound(struct wav *wav);
|
struct sound *play_sound(struct wav *wav);
|
||||||
|
|
||||||
|
@ -73,7 +76,7 @@ void sound_pause(struct sound *s);
|
||||||
void sound_resume(struct sound *s);
|
void sound_resume(struct sound *s);
|
||||||
void sound_stop(struct sound *s);
|
void sound_stop(struct sound *s);
|
||||||
|
|
||||||
struct music make_music(const char *ogg);
|
struct music make_music(const char *mp3);
|
||||||
|
|
||||||
const char *get_audio_driver();
|
const char *get_audio_driver();
|
||||||
|
|
||||||
|
|
|
@ -6,37 +6,62 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static struct bus bus[256];
|
static struct bus bus[256];
|
||||||
|
static int first = 0; /* First bus available */
|
||||||
|
static int first_on = -1;
|
||||||
short mastermix[BUF_FRAMES*CHANNELS];
|
short mastermix[BUF_FRAMES*CHANNELS];
|
||||||
|
|
||||||
struct bus *first_free_bus(struct dsp_filter in) {
|
void mixer_init() {
|
||||||
for (int i = 0; i < 256; i++) {
|
for (int i = 0; i < 256; i++) {
|
||||||
if (!bus[i].on) {
|
bus[i].next = i+1;
|
||||||
bus[i].on = 1;
|
bus[i].on = 0;
|
||||||
bus[i].in = in;
|
bus[i].id = i;
|
||||||
return &bus[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
bus[255].next = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct bus *first_free_bus(struct dsp_filter in) {
|
||||||
|
if (first == -1) return NULL;
|
||||||
|
struct bus *ret = bus[first];
|
||||||
|
first = ret->next;
|
||||||
|
ret->on = 1;
|
||||||
|
ret->in = in;
|
||||||
|
|
||||||
|
ret->next = first_on;
|
||||||
|
first_on = ret->id;
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bus_free(struct bus *bus)
|
void bus_free(struct bus *bus)
|
||||||
{
|
{
|
||||||
|
bus->next = first;
|
||||||
bus->on = 0;
|
bus->on = 0;
|
||||||
|
first = bus->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bus_fill_buffers(short *master, int n) {
|
void bus_fill_buffers(short *master, int n) {
|
||||||
|
int curbus = first_one
|
||||||
|
memset(master, 0, BUF_FRAMES*CHANNELS*sizeof(short));
|
||||||
|
while (bus[curbus].next != -1) {
|
||||||
|
dsp_run(bus[curbus].in, bus[curbus].buf, BUF_FRAMES);
|
||||||
|
for (int i = 0; i < BUF_FRAMES*CHANNELS; i++)
|
||||||
|
master[i] += bus[curbus].buf[i];
|
||||||
|
|
||||||
|
curbus = bus[curbus].next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
for (int i = 0; i < 256; i++) {
|
for (int i = 0; i < 256; i++) {
|
||||||
if (bus[i].on != 1) continue;
|
if (bus[i].on != 1) continue;
|
||||||
dsp_run(bus[i].in, bus[i].buf, BUF_FRAMES);
|
dsp_run(bus[i].in, bus[i].buf, BUF_FRAMES);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(master, 0, BUF_FRAMES*CHANNELS*sizeof(short));
|
|
||||||
|
|
||||||
for (int j = 0; j < 256; j++) {
|
for (int j = 0; j < 256; j++) {
|
||||||
if (!bus[j].on) continue;
|
if (!bus[j].on) continue;
|
||||||
for (int i = 0; i < BUF_FRAMES*CHANNELS; i++) {
|
for (int i = 0; i < BUF_FRAMES*CHANNELS; i++) {
|
||||||
master[i] += bus[j].buf[i];
|
master[i] += bus[j].buf[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,22 +7,23 @@ struct sound;
|
||||||
|
|
||||||
|
|
||||||
struct bus {
|
struct bus {
|
||||||
int on;
|
|
||||||
struct dsp_filter in;
|
struct dsp_filter in;
|
||||||
short buf[BUF_FRAMES*CHANNELS];
|
short buf[BUF_FRAMES*CHANNELS];
|
||||||
float gain;
|
float gain;
|
||||||
};
|
int on;
|
||||||
|
int next; /* Next available bus */
|
||||||
struct listener {
|
int prev;
|
||||||
float x;
|
int id;
|
||||||
float y;
|
|
||||||
float z;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern short mastermix[BUF_FRAMES*CHANNELS];
|
extern short mastermix[BUF_FRAMES*CHANNELS];
|
||||||
|
|
||||||
|
void mixer_init();
|
||||||
|
|
||||||
struct bus *first_free_bus(struct dsp_filter in);
|
struct bus *first_free_bus(struct dsp_filter in);
|
||||||
void bus_fill_buffers(short *master, int n);
|
void bus_fill_buffers(short *master, int n);
|
||||||
|
|
||||||
|
|
||||||
void bus_free(struct bus *bus);
|
void bus_free(struct bus *bus);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue