Skip to content

Commit

Permalink
Allocate audio buffer for application mixing in FastRAM if available.…
Browse files Browse the repository at this point in the history
… Contributed by Daniel Illgen.
  • Loading branch information
pmandin committed Apr 17, 2015
1 parent 7033d9f commit a81612c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/audio/mint/SDL_mintaudio.c
Expand Up @@ -80,7 +80,13 @@ int SDL_MintAudio_InitBuffers(SDL_AudioSpec *spec)
SDL_CalculateAudioSpec(spec);
MINTAUDIO_audiosize = spec->size * MAX_DMA_BUF;

/* Allocate memory for audio buffers in DMA-able RAM */
/* Allocate audio buffer memory for application in FastRAM */
MINTAUDIO_fastrambuf = Atari_SysMalloc(MINTAUDIO_audiosize, MX_TTRAM);
if (MINTAUDIO_fastrambuf) {
SDL_memset(MINTAUDIO_fastrambuf, spec->silence, MINTAUDIO_audiosize);
}

/* Allocate audio buffers memory for hardware in DMA-able RAM */
MINTAUDIO_audiobuf[0] = Atari_SysMalloc(2 * MINTAUDIO_audiosize, MX_STRAM);
if (MINTAUDIO_audiobuf[0]==NULL) {
SDL_SetError("SDL_MintAudio_OpenAudio: Not enough memory for audio buffer");
Expand All @@ -104,6 +110,10 @@ void SDL_MintAudio_FreeBuffers(void)
{
SDL_AudioDevice *this = SDL_MintAudio_device;

if (MINTAUDIO_fastrambuf) {
Mfree(MINTAUDIO_fastrambuf);
MINTAUDIO_fastrambuf = NULL;
}
if (MINTAUDIO_audiobuf[0]) {
Mfree(MINTAUDIO_audiobuf[0]);
MINTAUDIO_audiobuf[0] = MINTAUDIO_audiobuf[1] = NULL;
Expand Down Expand Up @@ -156,34 +166,41 @@ static void SDL_MintAudio_Callback(void)
Uint8 *buffer;
int i;

buffer = MINTAUDIO_audiobuf[SDL_MintAudio_numbuf];
buffer = (MINTAUDIO_fastrambuf ?
MINTAUDIO_fastrambuf :
MINTAUDIO_audiobuf[SDL_MintAudio_numbuf]);
SDL_memset(buffer, this->spec.silence, this->spec.size * SDL_MintAudio_max_buf);

if (this->paused)
return;

for (i=0; i<SDL_MintAudio_max_buf; i++) {
if (this->convert.needed) {
int silence;

if ( this->convert.src_format == AUDIO_U8 ) {
silence = 0x80;
if (!this->paused) {
for (i=0; i<SDL_MintAudio_max_buf; i++) {
if (this->convert.needed) {
int silence;

if ( this->convert.src_format == AUDIO_U8 ) {
silence = 0x80;
} else {
silence = 0;
}
SDL_memset(this->convert.buf, silence, this->convert.len);
this->spec.callback(this->spec.userdata,
(Uint8 *)this->convert.buf,this->convert.len);
SDL_ConvertAudio(&this->convert);
SDL_memcpy(buffer, this->convert.buf, this->convert.len_cvt);

buffer += this->convert.len_cvt;
} else {
silence = 0;
}
SDL_memset(this->convert.buf, silence, this->convert.len);
this->spec.callback(this->spec.userdata,
(Uint8 *)this->convert.buf,this->convert.len);
SDL_ConvertAudio(&this->convert);
SDL_memcpy(buffer, this->convert.buf, this->convert.len_cvt);

buffer += this->convert.len_cvt;
} else {
this->spec.callback(this->spec.userdata, buffer, this->spec.size);
this->spec.callback(this->spec.userdata, buffer,
this->spec.size);

buffer += this->spec.size;
buffer += this->spec.size;
}
}
}

if (MINTAUDIO_fastrambuf) {
SDL_memcpy(MINTAUDIO_audiobuf[SDL_MintAudio_numbuf], MINTAUDIO_fastrambuf,
this->spec.size * SDL_MintAudio_max_buf);
}
}

/* Add a new frequency/clock/predivisor to the current list */
Expand Down
2 changes: 2 additions & 0 deletions src/audio/mint/SDL_mintaudio.h
Expand Up @@ -52,6 +52,7 @@ struct SDL_PrivateAudioData {
int numfreq; /* Number of selected frequency */

Uint8 *audiobuf[2]; /* DMA buffers */
Uint8 *fastrambuf; /* Intermediate buffer to be filled by application */
int audiosize; /* and their size, variable depending on latency */

void (*swapbuf)(Uint8 *nextbuf, int nextsize); /* Routine to swap DMA buffers */
Expand All @@ -64,6 +65,7 @@ struct SDL_PrivateAudioData {
#define MINTAUDIO_numfreq (this->hidden->numfreq)
#define MINTAUDIO_swapbuf (this->hidden->swapbuf)
#define MINTAUDIO_audiobuf (this->hidden->audiobuf)
#define MINTAUDIO_fastrambuf (this->hidden->fastrambuf)
#define MINTAUDIO_audiosize (this->hidden->audiosize)

/* _MCH cookie (values>>16) */
Expand Down

0 comments on commit a81612c

Please sign in to comment.