1 #include "osd_linux_sdl_music.h"
2 
3 #ifdef SDL_mixer
4 /* Callback for SDL Mixer */
sdlmixer_fill_audio(int channel)5 void sdlmixer_fill_audio(int channel)
6 {
7 	if (Callback_Stop == TRUE)
8 	{
9 		// free things and
10 		//stop calling backs...
11 		if (chunk) Mix_FreeChunk(chunk);
12 		Log("stop playing back psg\n");
13 		return;
14 	}
15 
16   UChar lvol, rvol;
17   int i;
18 
19 	if (!(cvt.len))
20 	{ //once only ?
21 		memset(stream,0,sbuf_size*host.sound.stereo);
22 			if (!SDL_BuildAudioCVT (&cvt, AUDIO_U8, host.sound.stereo, host.sound.freq,
23                             AUDIO_S16, 2, host.sound.freq))
24 			{
25 				Log("SDL_BuildAudioCVT failed...audio callback stopped.\n");
26 				return;//#warning: avoid leaving without calling again ?
27 			}
28 			Log("SDL_BuildAudioCVT init ok.\n");
29 
30  			cvt.len = sbuf_size*host.sound.stereo;
31 			cvt.buf = (Uint8*) malloc(cvt.len*cvt.len_mult); //for in place conversion
32 	}
33 
34 for (i = 0; i < 6; i++)
35   WriteBuffer(sbuf[i], i, cvt.len);
36 
37   write_adpcm();
38 
39 
40    // Adjust the final post-mixed left/right volumes.  0-15 * 1.22 comes out to
41    // (0..18) which when multiplied by the ((-127..127) * 7) we get in the final
42    // stream mix below we have (-16002..16002) which we then divide by 128 to get
43    // a nice unsigned 8-bit value of 128 + (-125..125).
44 
45   if (host.sound.stereo)
46   {
47     lvol = (io.psg_volume >> 4) * 1.22;
48     rvol = (io.psg_volume & 0x0F) * 1.22;
49   }
50   else
51   {
52     //
53     // Use the average of the two channels for mono
54     //
55     lvol = rvol = (((io.psg_volume >> 4) * 1.22) + ((io.psg_volume & 0x0F) * 1.22)) / 2;
56   }
57 
58     //
59     // Mix streams and apply master volume.
60     //
61 
62 	if ( USE_S16 == TRUE ) {
63     	for (i = 0; i < cvt.len ; i++)
64       	cvt.buf[i] = 128 + ((UInt32) ((sbuf[0][i] + sbuf[1][i] + sbuf[2][i] + sbuf[3][i] + sbuf[4][i] + sbuf[5][i] +
65                 adpcmbuf[i]) * (!(i % 2) ? lvol : rvol)) >> 7);
66     	// convert audio data
67     	SDL_ConvertAudio(&cvt);
68     	chunk = Mix_QuickLoad_RAW(cvt.buf, cvt.len_cvt);
69 	}else{
70     	for (i = 0; i < cvt.len ; i++)
71       	stream[i] = 128 + ((UInt32) ((sbuf[0][i] + sbuf[1][i] + sbuf[2][i] + sbuf[3][i] + sbuf[4][i] + sbuf[5][i] +
72                 adpcmbuf[i]) * (!(i % 2) ? lvol : rvol)) >> 7);
73     	chunk = Mix_QuickLoad_RAW(stream, cvt.len);
74 	}
75 
76 	if (!(chunk))
77     {
78 		Log("Mix_QuickLoad_RAW: %s\n",Mix_GetError());
79 //		return;//#warning: avoid leaving without calling again ?
80 	}
81 //	chunk->allocated = 1;
82 	if (Mix_PlayChannel(channel,chunk,0)==-1)
83 	{
84 		Log("Mix_PlayChannel: %s\n",Mix_GetError());
85 //		return;//#warning: avoid leaving without calling again ?
86 	}
87 
88 //#warning: test dump
89 	if (dump_snd) // We also have to write data into a file
90 	{
91 		dump_audio_chunck(cvt.buf, cvt.len_cvt);
92 	}
93 
94 }
95 #endif
96