Example #1
0
int audio_set_rate(struct audio_info_struct *ai)
{
	int ret;

	if(!ai || ai->rate < 0)
		return -1;

	ai->alsa_format.rate=ai->rate;

	if((ret=snd_pcm_playback_format(ai->handle, &ai->alsa_format)) < 0 )
		return -1;

	audio_set_playback_params(ai);

	return 0;
}
Example #2
0
int audio_set_channels(struct audio_info_struct *ai)
{
	int ret;

	if(ai->alsa_format.channels < 0)
		return 0;

	ai->alsa_format.channels = ai->channels;

	if((ret=snd_pcm_playback_format(ai->handle, &ai->alsa_format)) < 0 )
		return -1;

	audio_set_playback_params(ai);

	return 0;
}
Example #3
0
int audio_set_format(struct audio_info_struct *ai)
{
	int ret;

	if(ai->format == -1)
		return 0;

	switch(ai->format)
	{
		case AUDIO_FORMAT_SIGNED_16:
		default:
			ai->alsa_format.format=SND_PCM_SFMT_S16_NE;
			break;
		case AUDIO_FORMAT_UNSIGNED_8:
			ai->alsa_format.format=SND_PCM_SFMT_U8;
			break;
		case AUDIO_FORMAT_SIGNED_8:
			ai->alsa_format.format=SND_PCM_SFMT_S8;
			break;
		case AUDIO_FORMAT_ULAW_8:
			ai->alsa_format.format=SND_PCM_SFMT_MU_LAW;
			break;
		case AUDIO_FORMAT_ALAW_8:
			ai->alsa_format.format=SND_PCM_SFMT_A_LAW;
			break;
		case AUDIO_FORMAT_UNSIGNED_16:
			ai->alsa_format.format=SND_PCM_SFMT_U16_NE;
			break;
	}

	if((ret=snd_pcm_playback_format(ai->handle, &ai->alsa_format)) < 0 )
		return -1;

	audio_set_playback_params(ai);

	return 0;
}
static gboolean
alsa_open (void *dp)
{
    alsa_driver * const d = dp;
    int mf;

    snd_pcm_format_t pf;
    snd_pcm_playback_info_t pcm_info;
    snd_pcm_playback_params_t pp;
    snd_pcm_playback_status_t pbstat;

    int err = snd_pcm_open(&(d->soundfd), d->card_number, d->device_number,
         		 	  SND_PCM_OPEN_PLAYBACK);
    if (err != 0) {
	char buf[256];
	g_sprintf(buf, _("Couldn't open ALSA device for sound output (card:%d, device:%d):\n%s"), 
		d->card_number, d->device_number, snd_strerror(err));
	error_error(buf);
	goto out;
    }

    // ---
    // Set non-blocking mode.
    // ---

    snd_pcm_block_mode(d->soundfd, 0);    // enable block mode

    d->outtime = 0;
    d->bits = 0;
    mf = 0;

    // ---
    // Select audio format
    // ---

    memset(&pf, 0, sizeof(pf));

    if (d->p_resolution == 16) {
      pf.format = SND_PCM_SFMT_S16_LE;
      d->bits = 16;
      mf = ST_MIXER_FORMAT_S16_LE;
    }
    else {
      pf.format = SND_PCM_SFMT_U8;
      d->bits = 8;
      mf = ST_MIXER_FORMAT_S8;
    }

    if(d->p_channels == 2) {
	d->stereo = 1;
	pf.channels = d->p_channels;
	mf |= ST_MIXER_FORMAT_STEREO;
    }
    else {
      pf.channels = d->p_channels;
      d->stereo = 0;
    }

    pf.rate = d->p_mixfreq;
    d->playrate = d->p_mixfreq;
    d->mf = mf;

    err = snd_pcm_playback_format(d->soundfd, &pf);
    if (err < 0) {
      error_error(_("Required sound output format not supported.\n"));
      goto out;
    }

    snd_pcm_playback_info(d->soundfd, &pcm_info);

    memset(&pp, 0, sizeof(pp));

    pp.fragment_size = d->p_fragsize * pf.channels * (d->bits / 8);
    pp.fragments_max = 1;
    //    pp.fragments_max = 16pcm_info.buffer_size / pp.fragment_size;
    pp.fragments_room = 1;

    err = snd_pcm_playback_params(d->soundfd, &pp);
    if (err < 0) {
      error_error(_("Required sound output parameters not supported.\n"));
      goto out;
    }

    snd_pcm_playback_status(d->soundfd, &pbstat);
    //    d->numfrags = pbstat.fragments;
    //    d->numfrags = 1;
    d->fragsize = pbstat.fragment_size;
    d->numfrags = 1;
    
    /*    fprintf(stderr, "Numfrags: %d\n", d->numfrags);
	  fprintf(stderr, "Fragsize: %d\n", d->fragsize); */

    d->sndbuf = calloc(1, d->fragsize);

    if(d->stereo == 1) { 
      d->fragsize /= 2;  
    } 
    if(d->bits == 16) {  
      d->fragsize /= 2;  
    } 
    
    d->polltag = audio_poll_add(snd_pcm_file_descriptor(d->soundfd), GDK_INPUT_WRITE, alsa_poll_ready_playing, d);
    /*    d->firstpoll = TRUE; */
    d->firstpoll = TRUE;
    d->playtime = 0;

    return TRUE;

  out:
    alsa_release(dp);
    return FALSE;
}