126 lines
6.2 KiB
C
126 lines
6.2 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include "rbp.h"
|
|
typedef unsigned char Uint8;
|
|
typedef unsigned int Uint32;
|
|
#define SDL_INIT_AUDIO 16
|
|
#define AUDIO_S16SYS 0x9010
|
|
#define AUDIO_RING 16384
|
|
static int init_fail,open_fail,initialized,paused;
|
|
static Uint32 ticks;
|
|
static const char *driver="sndmgr";
|
|
typedef struct {int freq;unsigned short format;Uint8 channels;unsigned short samples;void (*callback)(void*,Uint8*,int);} SDL_AudioSpec;
|
|
static Uint32 SDL_WasInit(Uint32 n){return initialized?n:0;}
|
|
static int SDL_InitSubSystem(Uint32 n){(void)n;if(init_fail)return -1;initialized=1;return 0;}
|
|
static const char *SDL_GetError(void){return "simulated error";}
|
|
static char *SDL_AudioDriverName(char *s,int n){strncpy(s,driver,n);return s;}
|
|
static Uint32 SDL_GetTicks(void){return ticks;}
|
|
static int SDL_OpenAudio(SDL_AudioSpec *s,SDL_AudioSpec *o){(void)o;assert(s->format==AUDIO_S16SYS);paused=1;return open_fail?-1:0;}
|
|
static void SDL_CloseAudio(void){paused=1;}
|
|
static void SDL_LockAudio(void){}
|
|
static void SDL_UnlockAudio(void){}
|
|
static void SDL_PauseAudio(int p){paused=p;}
|
|
static short ring[AUDIO_RING], samples[4410];
|
|
/* One producer (event loop), one consumer (SDL sound callback). Classic Mac
|
|
* may invoke the callback in an interrupt; never allocate or log in it. */
|
|
static volatile unsigned head,tail;
|
|
static unsigned rate0,ch0;
|
|
static int audio_open,audio_available,audio_advertised;
|
|
static unsigned long audio_rx,audio_bad,audio_dropped;
|
|
static Uint32 last_audio_open,tone_started;
|
|
static int audio_retry,tone_active;
|
|
static char audio_driver[32],audio_state[120]="not initialized";
|
|
static void sound_message(const char *prefix,const char *detail){
|
|
/* Bounded because SDL errors and driver names can be arbitrary strings. */
|
|
sprintf(audio_state,"%.30s %.80s",prefix,detail?detail:"");
|
|
}
|
|
static void sound_callback(void*unused,Uint8 *stream,int len){
|
|
int i;short*out=(short*)stream;unsigned pos=tail,limit=head;(void)unused;
|
|
for(i=0;i<len/2;i++){
|
|
if(pos==limit)out[i]=0;
|
|
else{out[i]=ring[pos];pos=(pos+1)%AUDIO_RING;}
|
|
}
|
|
if(len&1)stream[len-1]=0;
|
|
tail=pos;
|
|
}
|
|
static void sound_close(void){if(audio_open){SDL_CloseAudio();audio_open=0;}head=tail=0;}
|
|
static void sound_init(void){
|
|
audio_available=0;audio_driver[0]=0;
|
|
if(!(SDL_WasInit(SDL_INIT_AUDIO)&SDL_INIT_AUDIO)&&SDL_InitSubSystem(SDL_INIT_AUDIO)<0){sound_message("Audio init:",SDL_GetError());return;}
|
|
if(!SDL_AudioDriverName(audio_driver,sizeof audio_driver)){sound_message("Audio driver:",SDL_GetError());return;}
|
|
if(!strcmp(audio_driver,"dummy")||!strcmp(audio_driver,"disk")){sound_message("No speaker driver:",audio_driver);return;}
|
|
audio_available=1;sound_message("Waiting for audio:",audio_driver);
|
|
}
|
|
static int sound_open(unsigned rate,unsigned ch){
|
|
SDL_AudioSpec spec;
|
|
if(audio_open&&rate0==rate&&ch0==ch)return 1;
|
|
sound_close();memset(&spec,0,sizeof spec);spec.freq=rate;spec.format=AUDIO_S16SYS;
|
|
spec.channels=(Uint8)ch;spec.samples=1024;spec.callback=sound_callback;
|
|
last_audio_open=SDL_GetTicks();audio_retry=1;
|
|
if(SDL_OpenAudio(&spec,NULL)<0){sound_message("Audio open:",SDL_GetError());return 0;}
|
|
audio_open=1;rate0=rate;ch0=ch;audio_retry=0;return 1;
|
|
}
|
|
static unsigned sound_queue(const short *src,unsigned count,unsigned ch){
|
|
unsigned i=0,j,pos,limit,next;
|
|
SDL_LockAudio();pos=head;limit=tail;
|
|
while(i+ch<=count){
|
|
next=(pos+ch)%AUDIO_RING;
|
|
if(next==limit)break;
|
|
for(j=0;j<ch;j++){ring[pos]=src[i++];pos=(pos+1)%AUDIO_RING;}
|
|
}
|
|
/* Publish only completed sample frames. */
|
|
head=pos;SDL_UnlockAudio();return i;
|
|
}
|
|
static void sound_test(void){
|
|
unsigned i,j,n;short wave[128];
|
|
if(!audio_available)sound_init();
|
|
if(!audio_available)return;
|
|
sound_close();audio_retry=0;
|
|
if(!sound_open(22050,1))return;
|
|
/* 0.4 seconds, quiet square wave, no server involvement. */
|
|
for(i=0;i<8820;i+=128){
|
|
n=8820-i;if(n>128)n=128;
|
|
for(j=0;j<n;j++)wave[j]=((i+j)%50<25)?2500:-2500;
|
|
sound_queue(wave,n,1);
|
|
}
|
|
tone_active=1;tone_started=SDL_GetTicks();
|
|
sound_message("Local sound test:",audio_driver);SDL_PauseAudio(0);
|
|
}
|
|
static void audio(const rb_u8*p,rb_u32 n){
|
|
int k,i;long peak=0,v;unsigned rate,ch,queued;
|
|
audio_rx++;
|
|
if(n<8){audio_bad++;sound_message("Audio:","short packet");return;}
|
|
rate=(unsigned)rb_get32(p);ch=p[4];
|
|
if((rate!=11025&&rate!=22050&&rate!=44100)||(ch!=1&&ch!=2)){audio_bad++;sound_message("Audio:","unsupported format");return;}
|
|
k=rb_audio(p,n,samples,4410);
|
|
if(k<1){audio_bad++;sound_message("Audio:","invalid PCM/ADPCM packet");return;}
|
|
if(tone_active){if((Uint32)(SDL_GetTicks()-tone_started)<600)return;tone_active=0;}
|
|
if(!audio_available)return;
|
|
if(!audio_open||rate0!=rate||ch0!=ch){
|
|
if(audio_retry&&(Uint32)(SDL_GetTicks()-last_audio_open)<3000)return;
|
|
if(!sound_open(rate,ch))return;
|
|
}
|
|
for(i=0;i<k;i++){v=samples[i];if(v<0)v=-v;if(v>peak)peak=v;}
|
|
queued=sound_queue(samples,(unsigned)k,ch);
|
|
if(queued<(unsigned)k)audio_dropped++;
|
|
SDL_PauseAudio(0);
|
|
sprintf(audio_state,"%s %s %uHz/%u peak=%ld",audio_driver,p[5]==2?"ADPCM":"PCM",rate,ch,peak);
|
|
}
|
|
int main(void){
|
|
short dst[4];
|
|
const rb_u8 pcm[]={0,0,0x56,0x22,1,1,0,2,0x80,0,0x7f,0xff};
|
|
const rb_u8 adpcm[]={0,0,0x56,0x22,1,2,0,4,0,0,0,0,0,0};
|
|
init_fail=1;sound_init();assert(!audio_available&&strstr(audio_state,"init"));
|
|
init_fail=0;driver="dummy";sound_init();assert(!audio_available);
|
|
driver="sndmgr";sound_init();assert(audio_available);audio_advertised=audio_available;
|
|
open_fail=1;audio(pcm,sizeof pcm);assert(!audio_open&&strstr(audio_state,"open"));
|
|
open_fail=0;ticks=4000;audio(pcm,sizeof pcm);assert(audio_open&&!paused&&audio_rx==2);
|
|
sound_callback(0,(Uint8*)dst,sizeof dst);assert(dst[0]==-32768&&dst[1]==32767&&dst[2]==0);
|
|
audio(adpcm,sizeof adpcm);sound_callback(0,(Uint8*)dst,sizeof dst);assert(dst[0]==0&&dst[3]==0);
|
|
audio(pcm,2);assert(audio_bad==1);sound_test();assert(tone_active&&audio_open&&head==8820);
|
|
sound_callback(0,(Uint8*)dst,sizeof dst);assert(dst[0]==2500&&dst[3]==2500);
|
|
ticks+=700;audio(pcm,sizeof pcm);assert(!tone_active);sound_close();assert(!audio_open&&head==0&&tail==0);
|
|
assert(audio_advertised&&audio_dropped==0);puts("PASS mocked SDL init/open errors, PCM, ADPCM, underrun, tone");return 0;
|
|
}
|