static int NTO_OpenAudio(_THIS, SDL_AudioSpec *spec) { int rval; int format; Uint16 test_format; int twidth; int found; #ifdef DEBUG_AUDIO fprintf(stderr, "NTO_OpenAudio\n"); #endif audio_handle = NULL; this->enabled = 0; if ( pcm_buf != NULL ) { free((Uint8 *)pcm_buf); pcm_buf = NULL; } /* initialize channel transfer parameters to default */ init_pcm_cparams(&cparams); /* Open the audio device */ rval = snd_pcm_open_preferred(&audio_handle, &card_no, &device_no, OPEN_FLAGS); if ( rval < 0 ) { SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval)); return(-1); } /* set to nonblocking mode */ if ((rval = snd_pcm_nonblock_mode(audio_handle, 1))<0) //I assume 1 means on { SDL_SetError("snd_pcm_nonblock_mode failed: %s\n", snd_strerror(rval)); return(-1); } /* enable count status parameter */ if ((rval = snd_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0) { SDL_SetError("snd_plugin_set_disable failed: %s\n", snd_strerror(rval)); return(-1); } /* Try for a closest match on audio format */ format = 0; found = 0; // can't use format as SND_PCM_SFMT_U8 = 0 in nto for ( test_format = SDL_FirstAudioFormat(spec->format); !found ; ) { #ifdef DEBUG_AUDIO fprintf(stderr, "Trying format 0x%4.4x spec->samples %d\n", test_format,spec->samples); #endif /* if match found set format to equivalent ALSA format */ switch ( test_format ) { case AUDIO_U8: format = SND_PCM_SFMT_U8; cparams.buf.block.frag_size = spec->samples * spec->channels; found = 1; break; case AUDIO_S8: format = SND_PCM_SFMT_S8; cparams.buf.block.frag_size = spec->samples * spec->channels; found = 1; break; case AUDIO_S16LSB: format = SND_PCM_SFMT_S16_LE; cparams.buf.block.frag_size = spec->samples*2 * spec->channels; found = 1; break; case AUDIO_S16MSB: format = SND_PCM_SFMT_S16_BE; cparams.buf.block.frag_size = spec->samples*2 * spec->channels; found = 1; break; case AUDIO_U16LSB: format = SND_PCM_SFMT_U16_LE; cparams.buf.block.frag_size = spec->samples*2 * spec->channels; found = 1; break; case AUDIO_U16MSB: format = SND_PCM_SFMT_U16_BE; cparams.buf.block.frag_size = spec->samples*2 * spec->channels; found = 1; break; default: break; } if ( ! found ) { test_format = SDL_NextAudioFormat(); } } /* assumes test_format not 0 on success */ if ( test_format == 0 ) { SDL_SetError("Couldn't find any hardware audio formats"); return(-1); } spec->format = test_format; /* Set the audio format */ cparams.format.format = format; /* Set mono or stereo audio (currently only two channels supported) */ cparams.format.voices = spec->channels; #ifdef DEBUG_AUDIO fprintf(stderr,"intializing channels %d\n", cparams.format.voices); #endif /* Set rate */ cparams.format.rate = spec->freq ; /* Setup the transfer parameters according to cparams */ rval = snd_pcm_plugin_params(audio_handle, &cparams); if (rval < 0) { SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval)); return(-1); } /* Make sure channel is setup right one last time */ memset( &csetup, 0, sizeof( csetup ) ); csetup.channel = SND_PCM_CHANNEL_PLAYBACK; if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 ) { SDL_SetError("Unable to setup playback channel\n" ); return(-1); } else { #ifdef DEBUG_AUDIO fprintf(stderr,"requested format: %d\n",cparams.format.format); fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size); fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max); fprintf(stderr,"real format: %d\n", csetup.format.format ); fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size ); fprintf(stderr,"real max frags : %d\n", csetup.buf.block.frags_max ); #endif // DEBUG_AUDIO } /* Allocate memory to the audio buffer and initialize with silence (Note that buffer size must be a multiple of fragment size, so find closest multiple) */ twidth = snd_pcm_format_width(format); if (twidth < 0) { printf("snd_pcm_format_width failed\n"); twidth = 0; } #ifdef DEBUG_AUDIO fprintf(stderr,"format is %d bits wide\n",twidth); #endif pcm_len = spec->size ; #ifdef DEBUG_AUDIO fprintf(stderr,"pcm_len set to %d\n", pcm_len); #endif if (pcm_len == 0) { pcm_len = csetup.buf.block.frag_size; } pcm_buf = (Uint8*)malloc(pcm_len); if (pcm_buf == NULL) { SDL_SetError("pcm_buf malloc failed\n"); return(-1); } memset(pcm_buf,spec->silence,pcm_len); #ifdef DEBUG_AUDIO fprintf(stderr,"pcm_buf malloced and silenced.\n"); #endif /* get the file descriptor */ if( (audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0) { fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd); } /* Trigger audio playback */ rval = snd_pcm_plugin_prepare( audio_handle, SND_PCM_CHANNEL_PLAYBACK); if (rval < 0) { SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval)); return(-1); } this->enabled = 1; /* Get the parent process id (we're the parent of the audio thread) */ parent = getpid(); /* We're ready to rock and roll. :-) */ return(0); }
/* public methods (static but exported through the sysdep_dsp or plugin struct) */ static void *alsa_dsp_create(const void *flags) { int i, j; // audio_buf_info info; struct alsa_dsp_priv_data *priv = NULL; struct sysdep_dsp_struct *dsp = NULL; const struct sysdep_dsp_create_params *params = flags; const char *device = params->device; int err; int bytespersample; fprintf(stderr,"info: dsp_create called\n"); /* allocate the dsp struct */ if (!(dsp = calloc(1, sizeof(struct sysdep_dsp_struct)))) { fprintf(stderr, "error: malloc failed for struct sysdep_dsp_struct\n"); return NULL; } /* alloc private data */ if(!(priv = calloc(1, sizeof(struct alsa_dsp_priv_data)))) { fprintf(stderr, "error: malloc failed for struct dsp_priv_data\n"); alsa_dsp_destroy(dsp); return NULL; } /* fill in the functions and some data */ dsp->_priv = priv; dsp->get_freespace = alsa_dsp_get_freespace; dsp->write = alsa_dsp_write; dsp->destroy = alsa_dsp_destroy; dsp->hw_info.type = params->type; dsp->hw_info.samplerate = params->samplerate; priv->audio_dev.bMute = 0; priv->audio_dev.m_AudioHandle = NULL; priv->audio_dev.m_MixerHandle = NULL; priv->audio_dev.m_Acard = 0; priv->audio_dev.m_Adevice = 0; if (preferred_device) { if((err = snd_pcm_open_preferred(&(priv->audio_dev.m_AudioHandle), &priv->audio_dev.m_Acard, &priv->audio_dev.m_Adevice, SND_PCM_OPEN_PLAYBACK)) < 0) { fprintf(stderr,"info: snd_pcm_open_preferred failed: %s \n", snd_strerror(err)); alsa_dsp_destroy(dsp); return NULL; } } else { fprintf(stderr,"info: audio is using primary device\n"); if((err = snd_pcm_open(&(priv->audio_dev.m_AudioHandle), 0, 0, SND_PCM_OPEN_PLAYBACK)) < 0) { fprintf(stderr,"info: snd_pcm_open failed: %s \n", snd_strerror(err)); alsa_dsp_destroy(dsp); return NULL; } } memset (&(priv->audio_dev.m_Achaninfo), 0, sizeof (priv->audio_dev.m_Achaninfo)); priv->audio_dev.m_Achaninfo.channel = SND_PCM_CHANNEL_PLAYBACK; if ((err = snd_pcm_plugin_info (priv->audio_dev.m_AudioHandle, &(priv->audio_dev.m_Achaninfo))) < 0) { fprintf (stderr, "info: snd_pcm_plugin_info failed: %s\n", snd_strerror (err)); alsa_dsp_destroy(dsp); return NULL; } //needed to enable the count status parameter, mmap plugin disables this if((err = snd_plugin_set_disable(priv->audio_dev.m_AudioHandle, PLUGIN_DISABLE_MMAP)) < 0) { fprintf (stderr, "info: snd_plugin_set_disable failed: %s\n", snd_strerror (err)); alsa_dsp_destroy(dsp); return NULL; } /* calculate and set the fragsize & number of frags */ /* fragsize (as power of 2) */ i = 8; if (dsp->hw_info.type & SYSDEP_DSP_16BIT) i++; if (dsp->hw_info.type & SYSDEP_DSP_STEREO) i++; i += dsp->hw_info.samplerate / 22000; /* number of frags */ j = ((dsp->hw_info.samplerate * alsa_dsp_bytes_per_sample[dsp->hw_info.type] * params->bufsize) / (0x01 << i)) + 1; bytespersample=1; // dsp->hw_info.type &= ~SYSDEP_DSP_16BIT; // dsp->hw_info.type &= ~SYSDEP_DSP_STEREO; if (dsp->hw_info.type & SYSDEP_DSP_16BIT) bytespersample++; if (dsp->hw_info.type & SYSDEP_DSP_STEREO) bytespersample <<= 1; memset( &(priv->audio_dev.m_Aparams), 0, sizeof(priv->audio_dev.m_Aparams)); priv->audio_dev.m_Aparams.mode = SND_PCM_MODE_BLOCK; priv->audio_dev.m_Aparams.channel = SND_PCM_CHANNEL_PLAYBACK; priv->audio_dev.m_Aparams.start_mode = SND_PCM_START_FULL; priv->audio_dev.m_Aparams.stop_mode = SND_PCM_STOP_ROLLOVER; #if 0 priv->audio_dev.m_Aparams.buf.stream.queue_size = 512 * bytespersample; priv->audio_dev.m_Aparams.buf.stream.fill = SND_PCM_FILL_SILENCE; priv->audio_dev.m_Aparams.buf.stream.max_fill = 512 * bytespersample; #endif priv->audio_dev.m_Aparams.format.interleave = 1; priv->audio_dev.m_Aparams.format.rate = dsp->hw_info.samplerate; priv->audio_dev.m_Aparams.format.voices = (dsp->hw_info.type & SYSDEP_DSP_STEREO) ? 2 : 1; priv->audio_dev.m_Aparams.buf.block.frag_size = 1000; priv->audio_dev.m_Aparams.buf.block.frags_min = 1; priv->audio_dev.m_Aparams.buf.block.frags_max = 5; priv->audio_dev.m_BytesPerSample = bytespersample; priv->audio_dev.m_Aparams.format.format = #ifdef LSB_FIRST (dsp->hw_info.type & SYSDEP_DSP_16BIT) ? SND_PCM_SFMT_S16_LE : SND_PCM_SFMT_U8; #else (dsp->hw_info.type & SYSDEP_DSP_16BIT) ? SND_PCM_SFMT_S16_BE : SND_PCM_SFMT_U8; #endif if ((err = snd_pcm_plugin_params (priv->audio_dev.m_AudioHandle, &(priv->audio_dev.m_Aparams))) < 0) { fprintf (stderr, "info: snd_pcm_plugin_params failed: %s\n", snd_strerror (err)); alsa_dsp_destroy(dsp); return NULL; } if ((err = snd_pcm_plugin_prepare (priv->audio_dev.m_AudioHandle, SND_PCM_CHANNEL_PLAYBACK)) < 0) { fprintf (stderr, "warning: snd_pcm_plugin_prepare failed: %s\n", snd_strerror (err)); } memset (&(priv->audio_dev.m_Asetup), 0, sizeof (priv->audio_dev.m_Asetup)); priv->audio_dev.m_Asetup.channel = SND_PCM_CHANNEL_PLAYBACK; if ((err = snd_pcm_plugin_setup (priv->audio_dev.m_AudioHandle, &(priv->audio_dev.m_Asetup))) < 0) { fprintf (stderr, "warning: snd_pcm_plugin_setup failed: %s\n", snd_strerror (err)); alsa_dsp_destroy(dsp); return NULL; } memset (&(priv->audio_dev.m_Astatus), 0, sizeof (priv->audio_dev.m_Astatus)); priv->audio_dev.m_Astatus.channel = SND_PCM_CHANNEL_PLAYBACK; if ((err = snd_pcm_plugin_status (priv->audio_dev.m_AudioHandle, &(priv->audio_dev.m_Astatus))) < 0) { fprintf (stderr, "warning: snd_pcm_plugin_status failed: %s\n", snd_strerror (err)); } dsp->hw_info.bufsize = priv->audio_dev.m_Asetup.buf.stream.queue_size / alsa_dsp_bytes_per_sample[dsp->hw_info.type]; #if 0 if ((err=snd_pcm_nonblock_mode(priv->audio_dev.m_AudioHandle, 1))<0) { fprintf(stderr, "error: error with non block mode: %s\n", snd_strerror (err)); } #endif return dsp; }