summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/codecs/cRSID/host/audio.c
blob: 2cfd5dac0b451f038684734788e6410a559efc72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#ifdef CRSID_PLATFORM_PC

#include <SDL/SDL.h>


void cRSID_soundCallback(void* userdata, unsigned char *buf, int len) {
 cRSID_generateSound( (cRSID_C64instance*)userdata, buf, len );
}


void* cRSID_initSound(cRSID_C64instance* C64, unsigned short samplerate, unsigned short buflen) {
 static SDL_AudioSpec soundspec;
 if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
  fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return NULL;
 }
 soundspec.freq=samplerate; soundspec.channels=1; soundspec.format=AUDIO_S16;
 soundspec.samples=buflen; soundspec.userdata=C64; soundspec.callback=cRSID_soundCallback;
 if ( SDL_OpenAudio(&soundspec, NULL) < 0 ) {
  fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); return NULL;
 }
 return (void*)&soundspec;
}


void cRSID_closeSound (void) {
 SDL_PauseAudio(1); SDL_CloseAudio();
}


void cRSID_startSound (void) {
 SDL_PauseAudio(0);
}


void cRSID_stopSound (void) {
 SDL_PauseAudio(1);
}


void cRSID_generateSound(cRSID_C64instance* C64instance, unsigned char *buf, unsigned short len) {
 static unsigned short i;
 static int Output;
 for(i=0;i<len;i+=2) {
  Output=cRSID_generateSample(C64instance); //cRSID_emulateC64(C64instance);
  //if (Output>=32767) Output=32767; else if (Output<=-32768) Output=-32768; //saturation logic on overflow
  buf[i]=Output&0xFF; buf[i+1]=Output>>8;
 }
}


#endif


signed short cRSID_generateSample (cRSID_C64instance* C64) { //call this from custom buffer-filler
 static int Output;
 Output=cRSID_emulateC64(C64);
 if (C64->PSIDdigiMode) Output += cRSID_playPSIDdigi(C64);
 if (Output>=32767) Output=32767; else if (Output<=-32768) Output=-32768; //saturation logic on overflow
 return (signed short) Output;
}