Example #1
0
void sound_play() {

#if defined(D_SOUND_OSS) || defined(D_SOUND_ALSA) || defined(D_SOUND_PULSE)  
	static int retval,retval2;
#endif
	ordenador.current_buffer=sound[0];
	//remove_dc(ordenador.current_buffer,ordenador.buffer_len);

	switch(sound_type) {
	case SOUND_NO: // no sound
		usleep(75000); // wait 1/20 second
		return;
	break;
	case SOUND_SDL: // SDL
		if (!started_sound_sdl) {
		SDL_PauseAudio(0);
		ordenador.current_buffer = sound[0];
		buffer0_occupied=1;
		started_sound_sdl = 1;
		buffer_reading=0;
		}
		//Double buffer
		while ((buffer0_occupied)&&(buffer1_occupied)){usleep(1000);}; //Wait for one buffer to be free
		if (!buffer0_occupied) //Buffer 0 is now free
			{buffer0_occupied=1;
			ordenador.current_buffer = sound[0]; }
		else //Buffer 1 is now free
			{buffer1_occupied=1;
			ordenador.current_buffer = sound[1]; }
		return;
	break;
#ifdef D_SOUND_OSS
	case SOUND_OSS: // OSS
		retval=write(audio_fd,ordenador.current_buffer,ordenador.buffer_len*ordenador.increment);
		return;
	break;
#endif
#ifdef D_SOUND_ALSA
	case SOUND_ALSA: // ALSA
		if (started_sound==0) {
			snd_pcm_prepare (_soundDevice);
			started_sound=1;
		}
		retval=snd_pcm_writei(_soundDevice,sound[0],ordenador.buffer_len);
		if (retval<0) {
			retval=snd_pcm_prepare(_soundDevice);
		}
		return;
	break;
#endif
#ifdef D_SOUND_PULSE
	case SOUND_PULSEAUDIO: // PulseAudio
		// Remove the DC component to avoid losing the sound when multiplexing with other sources
		sound_remove_dc(ordenador.current_buffer,ordenador.buffer_len);
		retval=pa_simple_write(pulse_s,sound[0],ordenador.buffer_len,&retval2);
		return;
	break;
#endif
#ifdef GEKKO
	case SOUND_ASND: // ASND
		if (!started_sound_asnd) {
		ASND_SetVoice(1,VOICE_STEREO_16BIT_BE,ordenador.freq,0,sound[0],ordenador.buffer_len*ordenador.increment,
		255, 255, callback);
		started_sound_asnd = 1;
		}
		//Double buffer
		while (!ASND_TestVoiceBufferReady(1)){}; //Wait for one buffer to be free
		if (!ASND_TestPointer (1, sound[0])) 
			{ASND_AddVoice(1,sound[0],ordenador.buffer_len*ordenador.increment);
			ordenador.current_buffer = sound[0]; }
		else 
			{ASND_AddVoice(1,sound[1],ordenador.buffer_len*ordenador.increment);
			ordenador.current_buffer = sound[1]; }
	
		return;
	break;
#endif

	default:
	break;
	}
}
Example #2
0
static void * ogg_player_thread(private_data_ogg * priv)
{
	int first_time = 1;
	long ret;

	//init
	LWP_InitQueue(&oggplayer_queue);

	priv[0].vi = ov_info(&priv[0].vf, -1);

	ASND_Pause(0);

	priv[0].pcm_indx = 0;
	priv[0].pcmout_pos = 0;
	priv[0].eof = 0;
	priv[0].flag = 0;
	priv[0].current_section = 0;

	ogg_thread_running = 1;

	while (!priv[0].eof && ogg_thread_running)
	{
		if (priv[0].flag)
			LWP_ThreadSleep(oggplayer_queue); // wait only when i have samples to send

		if (priv[0].flag == 0) // wait to all samples are sent
		{
			if (ASND_TestPointer(0, priv[0].pcmout[priv[0].pcmout_pos])
					&& ASND_StatusVoice(0) != SND_UNUSED)
			{
				priv[0].flag |= 64;
				continue;
			}
			if (priv[0].pcm_indx < READ_SAMPLES)
			{
				priv[0].flag = 3;

				if (priv[0].seek_time >= 0)
				{
					ov_time_seek(&priv[0].vf, priv[0].seek_time);
					priv[0].seek_time = -1;
				}

				ret
						= ov_read(
								&priv[0].vf,
								(void *) &priv[0].pcmout[priv[0].pcmout_pos][priv[0].pcm_indx],
								MAX_PCMOUT,/*0,2,1,*/&priv[0].current_section);
				priv[0].flag &= 192;
				if (ret == 0)
				{
					/* EOF */
					if (priv[0].mode & 1)
						ov_time_seek(&priv[0].vf, 0); // repeat
					else
						priv[0].eof = 1; // stops
				}
				else if (ret < 0)
				{
					/* error in the stream.  Not a problem, just reporting it in
					 case we (the app) cares.  In this case, we don't. */
					if (ret != OV_HOLE)
					{
						if (priv[0].mode & 1)
							ov_time_seek(&priv[0].vf, 0); // repeat
						else
							priv[0].eof = 1; // stops
					}
				}
				else
				{
					/* we don't bother dealing with sample rate changes, etc, but
					 you'll have to*/
					priv[0].pcm_indx += ret >> 1; //get 16 bits samples
				}
			}
			else
				priv[0].flag = 1;
		}