diff options
Diffstat (limited to 'lib/rbcodec/codecs/cRSID/host/audio.c')
-rw-r--r-- | lib/rbcodec/codecs/cRSID/host/audio.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/cRSID/host/audio.c b/lib/rbcodec/codecs/cRSID/host/audio.c new file mode 100644 index 0000000000..2e06279113 --- /dev/null +++ b/lib/rbcodec/codecs/cRSID/host/audio.c | |||
@@ -0,0 +1,62 @@ | |||
1 | |||
2 | #ifdef CRSID_PLATFORM_PC | ||
3 | |||
4 | #include <SDL/SDL.h> | ||
5 | |||
6 | |||
7 | void cRSID_soundCallback(void* userdata, unsigned char *buf, int len) { | ||
8 | cRSID_generateSound( (cRSID_C64instance*)userdata, buf, len ); | ||
9 | } | ||
10 | |||
11 | |||
12 | void* cRSID_initSound(cRSID_C64instance* C64, unsigned short samplerate, unsigned short buflen) { | ||
13 | static SDL_AudioSpec soundspec; | ||
14 | if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { | ||
15 | fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return NULL; | ||
16 | } | ||
17 | soundspec.freq=samplerate; soundspec.channels=1; soundspec.format=AUDIO_S16; | ||
18 | soundspec.samples=buflen; soundspec.userdata=C64; soundspec.callback=cRSID_soundCallback; | ||
19 | if ( SDL_OpenAudio(&soundspec, NULL) < 0 ) { | ||
20 | fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); return NULL; | ||
21 | } | ||
22 | return (void*)&soundspec; | ||
23 | } | ||
24 | |||
25 | |||
26 | void cRSID_closeSound (void) { | ||
27 | SDL_PauseAudio(1); SDL_CloseAudio(); | ||
28 | } | ||
29 | |||
30 | |||
31 | void cRSID_startSound (void) { | ||
32 | SDL_PauseAudio(0); | ||
33 | } | ||
34 | |||
35 | |||
36 | void cRSID_stopSound (void) { | ||
37 | SDL_PauseAudio(1); | ||
38 | } | ||
39 | |||
40 | |||
41 | void cRSID_generateSound(cRSID_C64instance* C64instance, unsigned char *buf, unsigned short len) { | ||
42 | static unsigned short i; | ||
43 | static int Output; | ||
44 | for(i=0;i<len;i+=2) { | ||
45 | Output=cRSID_generateSample(C64instance); //cRSID_emulateC64(C64instance); | ||
46 | //if (Output>=32767) Output=32767; else if (Output<=-32768) Output=-32768; //saturation logic on overflow | ||
47 | buf[i]=Output&0xFF; buf[i+1]=Output>>8; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | |||
52 | #endif | ||
53 | |||
54 | |||
55 | static inline signed short cRSID_generateSample (cRSID_C64instance* C64) { //call this from custom buffer-filler | ||
56 | static int Output; | ||
57 | Output=cRSID_emulateC64(C64); | ||
58 | if (C64->PSIDdigiMode) Output += cRSID_playPSIDdigi(C64); | ||
59 | if (Output>=32767) Output=32767; else if (Output<=-32768) Output=-32768; //saturation logic on overflow | ||
60 | return (signed short) Output; | ||
61 | } | ||
62 | |||