static void alsa_log(snd_pcm_hw_params_t* hw_params, snd_pcm_sw_params_t* sw_params) { unsigned period_time; snd_pcm_uframes_t period_size; unsigned period_count; unsigned buffer_time; snd_pcm_uframes_t buffer_size; unsigned tick_time; snd_pcm_uframes_t xfer_align; snd_pcm_hw_params_get_period_time(hw_params, &period_time, 0); snd_pcm_hw_params_get_period_size(hw_params, &period_size, 0); snd_pcm_hw_params_get_periods(hw_params, &period_count, 0); snd_pcm_hw_params_get_buffer_time(hw_params, &buffer_time, 0); snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size); snd_pcm_hw_params_get_tick_time(hw_params, &tick_time, 0); log_std(("sound:alsa: hw period_time %g [us], period_size %d, periods %d, buffer_time %g [us], buffer_size %d, tick_time %g [us]\n", (double)(period_time / 1000000.0), (unsigned)period_size, (unsigned)period_count, (double)(buffer_time / 1000000.0), (unsigned)buffer_size, (double)(tick_time / 1000000.0) )); snd_pcm_sw_params_get_xfer_align(sw_params, &xfer_align); log_std(("sound:alsa: sw xfer_align %d\n", (unsigned)xfer_align )); }
bool SoundProcessor::SetUpRecorder(){ rc_m = snd_pcm_open(&handle_m, "default", SND_PCM_STREAM_CAPTURE, 0); if (rc_m < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc_m)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms_m); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle_m, params_m); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle_m, params_m, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle_m, params_m, SND_PCM_FORMAT_U8); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle_m, params_m, 1); /* 44100 bits/second sampling rate (CD quality) */ val_m = 8000; snd_pcm_hw_params_set_rate_near(handle_m, params_m, &val_m, &dir_m); /* Set period size to 32 frames. */ //frames = 32; snd_pcm_hw_params_set_period_size_near(handle_m, params_m, &frames, &dir_m); /* Write the parameters to the driver */ rc_m = snd_pcm_hw_params(handle_m, params_m); if (rc_m < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc_m)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params_m, &frames, &dir_m); size = frames; /* 2 bytes/sample, 2 channels */ buffer_m = (char *) malloc(size); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params_m, &val_m, &dir_m); return true; }
static int ste_iop_hwparams(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params) { int err = 0; int direction; ste_iop_t *ste_iop = io->private_data; /* Get and print HW params */ /* PCM format */ err = snd_pcm_hw_params_get_format(params, &ste_iop->hwparams.pcm_format); if (err < 0) { fprintf(stderr, "ST-Ericsson ALSA ADM I/O Plugin: Failed to get HW param PCM format: %s\n", snd_strerror(err)); return err; } /* PCM access mode */ err = snd_pcm_hw_params_get_access(params, &ste_iop->hwparams.pcm_access); if (err < 0) { fprintf(stderr, "ST-Ericsson ALSA ADM I/O Plugin: Failed to get HW param PCM Access mode: %s\n", snd_strerror(err)); return err; } /* PCM number of channels */ err = snd_pcm_hw_params_get_channels(params, &ste_iop->hwparams.pcm_channels); if (err < 0) { fprintf(stderr, "ST-Ericsson ALSA ADM I/O Plugin: Failed to get HW param PCM number of channels: %s\n", snd_strerror(err)); return err; } /* Sample Rate */ err = snd_pcm_hw_params_get_rate(params, &ste_iop->hwparams.pcm_rate, &direction); if (err < 0) { fprintf(stderr, "ST-Ericsson ALSA ADM I/O Plugin: Failed to get HW param PCM Sample Rate: %s\n", snd_strerror(err)); return err; } ste_iop->bufsz = ste_iop->hwparams.pcm_rate * 32 / 1000 * 2 * ste_iop->hwparams.pcm_channels; /* Period time */ err = snd_pcm_hw_params_get_period_time(params, &ste_iop->hwparams.pcm_period_time, &direction); if (err < 0) { fprintf(stderr, "ST-Ericsson ALSA ADM I/O Plugin: Failed to get HW param PCM period time: %s\n", snd_strerror(err)); return err; } return 0; }
void Recorder::initRecoder() { int rc; snd_pcm_hw_params_t *params; unsigned int val; int dir=0; /* Open PCM device for recording (capture). */ rc = snd_pcm_open(&handle, "default",SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) { fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, CHANNELS); /* 44100 bits/second sampling rate (CD quality) */ val = SAMPLERATE; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to 32 frames. */ frames = 32; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr,"unable to set hw parameters: %s\n",snd_strerror(rc)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params,&frames, &dir); size = frames * 2; /* 2 bytes/sample, 2 channels */ /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params,&val, &dir); //loops = 5000000 / val; //获取5秒中所对应的loop这里用不到 }
/************************************************************************** * wodPlayer_DSPWait [internal] * Returns the number of milliseconds to wait for the DSP buffer to play a * period */ static DWORD wodPlayer_DSPWait(const WINE_WAVEDEV *wwo) { /* time for one period to be played */ unsigned int val=0; int dir=0; int err=0; err = snd_pcm_hw_params_get_period_time(wwo->hw_params, &val, &dir); return val / 1000; }
int audio_capture_init(char *audio_dev) { unsigned int val; int dir, rc, size; /* Open PCM device for recording (capture). */ rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); return -1; } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(&hw_params); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, hw_params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE); /* 1 channel mono */ snd_pcm_hw_params_set_channels(handle, hw_params, 1); /* 44100 bits/second sampling rate (CD quality) */ val = 44100; snd_pcm_hw_params_set_rate_near(handle, hw_params, &val, &dir); /* Set period size to 32 frames. */ frames = 32; snd_pcm_hw_params_set_period_size_near(handle, hw_params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, hw_params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); return -1; } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(hw_params, &frames, &dir); size = frames * 4; /* 2 bytes/sample, 2 channels */ frame_buffer = (char *) malloc(size); printf("- Allocated %d bytes frame buffer\n", size); snd_pcm_hw_params_get_period_time(hw_params, &val, &dir); return 0; }
/**************************************************************************************** * 函数名: init_alsa_play * 输 入: 无 * 输 出: 无 * 功能说明:alsa音频播放器初始化 * * ******************************************************************************************/ void init_alsa_play() { snd_pcm_hw_params_t *params; unsigned int val; int dir; /* Open PCM device for playback. */ rc=snd_pcm_open(&handle,"plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0); if (rc< 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle,params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format 格式*/ snd_pcm_hw_params_set_format(handle,params, SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) 声道*/ snd_pcm_hw_params_set_channels(handle, params, channels); /* 32000 bits/second sampling rate (CD quality) 采样率设置*/ snd_pcm_hw_params_set_rate_near(handle,params, &rate, &dir); /* Set period size to 32 frames. */ frames = SAMPLERATE/1000*READMSFORONCE; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); exit(1); } /*Use a buffer large enough to hold one period*/ snd_pcm_hw_params_get_period_size(params, &frames, &dir); //32个采样点算一帧 size = frames * (channels); /* 2 bytes/sample, 2 channels */ buffer = (char *) malloc(size); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); }
void carmen_sound_set_device_parameters(snd_pcm_t *handle, int sampling_rate, int num_frames) { int rc, dir; unsigned int val; snd_pcm_hw_params_t *params; snd_pcm_uframes_t frames; val = sampling_rate; frames = (snd_pcm_uframes_t) num_frames; /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 2); /* set bits/second sampling rate */ snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to num of frames. */ snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) exit(printf("unable to set hw parameters: %s\n", snd_strerror(rc))); /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &frames, &dir); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); // @TODO: pesquisar por que tem que ler um pouco que vem vazio nao sei por que... char buffer[4 * 32]; rc = snd_pcm_readi(handle, buffer, 32); }
int sound_init () { int rc; rc = snd_pcm_open (&handle, "default", SND_PCM_STREAM_PLAYBACK, 0); if (rc < 0) { fprintf (stderr, "unable to open pcm device: %s\n", snd_strerror (rc)); return rc; } g_paused = 0; snd_pcm_hw_params_alloca (¶ms); snd_pcm_hw_params_any (handle, params); snd_pcm_hw_params_set_access (handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format (handle, params, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_channels (handle, params, CHANNELS); val = RATE; snd_pcm_hw_params_set_rate_near (handle, params, &val, &dir); frames = FRAMES; snd_pcm_hw_params_set_period_size_near (handle, params, &frames, &dir); rc = snd_pcm_hw_params (handle, params); if (rc < 0) return rc; snd_pcm_hw_params_get_period_size (params, &frames, &dir); snd_pcm_hw_params_get_period_time (params, &val, &dir); return 0; }
// 声卡采集初始化 struct SoundInfo* sound_info_record_new() { struct SoundInfo *info = (struct SoundInfo*)malloc(sizeof(struct SoundInfo)); int rc; int dir = 0; info->channels = 2; info->frequency = 44100; rc = snd_pcm_open(&info->handle, "default", SND_PCM_STREAM_CAPTURE, 0); snd_pcm_hw_params_alloca(&info->params); rc = snd_pcm_hw_params_any(info->handle, info->params); rc = snd_pcm_hw_params_set_access(info->handle, info->params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(info->handle, info->params, SND_PCM_FORMAT_S16_LE); rc = snd_pcm_hw_params_set_channels(info->handle, info->params, info->channels); unsigned int val = info->frequency; rc = snd_pcm_hw_params_set_rate_near(info->handle, info->params, &val, &dir); rc = snd_pcm_hw_params(info->handle, info->params); info->frames = 32;//"frames"=channel*size/8=4, frames = "frames"*8 one period contains 8 "frames" snd_pcm_hw_params_set_period_size_near(info->handle, info->params, &info->frames, &dir); rc = snd_pcm_hw_params(info->handle, info->params); snd_pcm_hw_params_get_period_size(info->params, &info->frames, &dir); info->bufferSize = info->frames * 4; info->buffer = (char*) malloc(info->bufferSize); snd_pcm_hw_params_get_period_time(info->params, &val, &dir); memset(info->buffer, 0, info->bufferSize); return info; }
void alsa_print_info(snd_pcm_t * handle, snd_pcm_hw_params_t * hwp) { printf("device [%s] opened with\n", snd_pcm_name(handle)); printf("\tstate=%s\n", snd_pcm_state_name(snd_pcm_state(handle))); unsigned int val, val2; snd_pcm_hw_params_get_access(hwp, (snd_pcm_access_t *) &val); printf("\taccess_type=%s\n", snd_pcm_access_name((snd_pcm_access_t)val)); snd_pcm_hw_params_get_format(hwp, (snd_pcm_format_t *) &val); printf("\tformat=%s\n", snd_pcm_format_name((snd_pcm_format_t) val) ); snd_pcm_hw_params_get_channels(hwp, &val); printf("\tchannels=%d\n", val); snd_pcm_hw_params_get_rate(hwp, &val, (int *) &val2); printf("\trate=%d fps\n", val); snd_pcm_hw_params_get_period_time(hwp, &val, (int *) &val2); printf("\tperiod_time=%d us\n", val); snd_pcm_uframes_t frames; snd_pcm_hw_params_get_period_size(hwp, &frames, (int *) &val2); printf("\tperiod_size=%d frames\n", (int)frames); snd_pcm_hw_params_get_buffer_size(hwp, (snd_pcm_uframes_t *) &val); printf("\tbuffer_size=%d frames\n", val); snd_pcm_hw_params_get_periods(hwp, &val, (int *) &val2); printf("\tperiods_per_buffer=%d periods\n", val); }
void sound_dev_prepare(const char *device, struct sound_device_t *dev, unsigned int sample_rate, unsigned int channels){ int rc; rc = snd_pcm_open(&(*dev).handle, device, SND_PCM_STREAM_PLAYBACK, 0); if (rc < 0){ exception(snd_strerror(rc),BAD_PCM_DEV); } snd_pcm_hw_params_alloca(&(*dev).params); snd_pcm_hw_params_any((*dev).handle, (*dev).params); snd_pcm_hw_params_set_access((*dev).handle, (*dev).params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format((*dev).handle, (*dev).params, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_channels((*dev).handle, (*dev).params, channels); snd_pcm_hw_params_set_rate_near((*dev).handle, (*dev).params, &sample_rate, &(*dev).dir); rc = snd_pcm_hw_params((*dev).handle, (*dev).params); if (rc < 0){ exception(snd_strerror(rc), BAD_HW_PARAMS); } snd_pcm_hw_params_get_period_size((*dev).params, &(*dev).frames, &(*dev).dir); (*dev).size = (*dev).frames * 4; snd_pcm_hw_params_get_period_time((*dev).params, &(*dev).val, &(*dev).dir); (*dev).loops = TIME / (*dev).val; (*dev).buff_size_bytes = (*dev).size * (*dev).loops; (*dev).buff_size_frames = (*dev).frames * (*dev).loops; }
int main() { int rc; snd_pcm_t *handle; snd_pcm_hw_params_t *params; unsigned int val, val2; int dir; snd_pcm_uframes_t frames; /* Open PCM device for playback. */ rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0); if (rc < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 2); /* 44100 bits/second sampling rate (CD quality) */ val = 44100; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); exit(1); } /* Display information about the PCM interface */ printf("PCM handle name = '%s'\n", snd_pcm_name(handle)); printf("PCM state = %s\n", snd_pcm_state_name(snd_pcm_state(handle))); snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) &val); printf("access type = %s\n", snd_pcm_access_name((snd_pcm_access_t)val)); snd_pcm_hw_params_get_format(params, &val); printf("format = '%s' (%s)\n", snd_pcm_format_name((snd_pcm_format_t)val), snd_pcm_format_description( (snd_pcm_format_t)val)); snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val); printf("subformat = '%s' (%s)\n", snd_pcm_subformat_name((snd_pcm_subformat_t)val), snd_pcm_subformat_description( (snd_pcm_subformat_t)val)); snd_pcm_hw_params_get_channels(params, &val); printf("channels = %d\n", val); snd_pcm_hw_params_get_rate(params, &val, &dir); printf("rate = %d bps\n", val); snd_pcm_hw_params_get_period_time(params, &val, &dir); printf("period time = %d us\n", val); snd_pcm_hw_params_get_period_size(params, &frames, &dir); printf("period size = %d frames\n", (int)frames); snd_pcm_hw_params_get_buffer_time(params, &val, &dir); printf("buffer time = %d us\n", val); snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val); printf("buffer size = %d frames\n", val); snd_pcm_hw_params_get_periods(params, &val, &dir); printf("periods per buffer = %d frames\n", val); snd_pcm_hw_params_get_rate_numden(params, &val, &val2); printf("exact rate = %d/%d bps\n", val, val2); val = snd_pcm_hw_params_get_sbits(params); printf("significant bits = %d\n", val); snd_pcm_hw_params_get_tick_time(params, &val, &dir); printf("tick time = %d us\n", val); val = snd_pcm_hw_params_is_batch(params); printf("is batch = %d\n", val); val = snd_pcm_hw_params_is_block_transfer(params); printf("is block transfer = %d\n", val); val = snd_pcm_hw_params_is_double(params); printf("is double = %d\n", val); val = snd_pcm_hw_params_is_half_duplex(params); printf("is half duplex = %d\n", val); val = snd_pcm_hw_params_is_joint_duplex(params); printf("is joint duplex = %d\n", val); val = snd_pcm_hw_params_can_overrange(params); printf("can overrange = %d\n", val); val = snd_pcm_hw_params_can_mmap_sample_resolution(params); printf("can mmap = %d\n", val); val = snd_pcm_hw_params_can_pause(params); printf("can pause = %d\n", val); val = snd_pcm_hw_params_can_resume(params); printf("can resume = %d\n", val); val = snd_pcm_hw_params_can_sync_start(params); printf("can sync start = %d\n", val); snd_pcm_close(handle); return 0; }
void *data_streaming(void *socket_desc) { int rc; snd_pcm_t *handle; snd_pcm_hw_params_t *params; unsigned int val; int dir; snd_pcm_uframes_t frames; int i; /* Open PCM device for recording (capture). */ rc = snd_pcm_open(&handle, "plughw:1,0", SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_U8); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 1); /* 44100 bits/second sampling rate (CD quality) */ val = 32768; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to 32 frames. */ frames = SIZE; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &frames, &dir); /* 2 bytes/sample, 2 channels */ /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); while (1) { pthread_mutex_lock(&mutex); rc = snd_pcm_readi(handle, buffer, frames); pthread_kill(tid, SIGUSR1); pthread_mutex_unlock(&mutex); if (rc == -EPIPE) { /* EPIPE means overrun */ fprintf(stderr, "overrun occurred\n"); snd_pcm_prepare(handle); } else if (rc < 0) { fprintf(stderr,"error from read: %s\n",snd_strerror(rc)); } else if (rc != (int)frames) { fprintf(stderr, "short read, read %d frames\n", rc); } } snd_pcm_drain(handle); snd_pcm_close(handle); free(buffer); return 0; }
alsa_dev_t* alsa_open(char *name) { alsa_dev_t *alsa_dev = NULL; snd_pcm_hw_params_t *params = NULL; uint32_t val = 0; int32_t rc = 0; int32_t dir = 0; alsa_dev = malloc(sizeof(alsa_dev_t)); if (!alsa_dev) { syslog(LOG_ERR, "Failed to create audio device: Out of memory"); exit(EXIT_FAILURE); } /* Open PCM device for recording (capture). */ rc = snd_pcm_open(&alsa_dev->snd_pcm, name, SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) { syslog(LOG_ERR, "Cannot open pcm device: %s", snd_strerror(rc)); exit(EXIT_FAILURE); } alsa_dev->name = name; /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(alsa_dev->snd_pcm, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(alsa_dev->snd_pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(alsa_dev->snd_pcm, params, SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(alsa_dev->snd_pcm, params, AUDIO_CHANNEL_NUM); /* 44100 bits/second sampling rate (CD quality) */ val = AUDIO_SAMPLE_RATE; snd_pcm_hw_params_set_rate_near(alsa_dev->snd_pcm, params, &val, &dir); alsa_dev->frames = 1152; snd_pcm_hw_params_set_period_size_near(alsa_dev->snd_pcm, params, &alsa_dev->frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(alsa_dev->snd_pcm, params); if (rc < 0) { syslog(LOG_ERR, "Cannot set hw parameters: %s", snd_strerror(rc)); exit(EXIT_FAILURE); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &alsa_dev->frames, &dir); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); snd_pcm_prepare(alsa_dev->snd_pcm); snd_pcm_start(alsa_dev->snd_pcm); return alsa_dev; }
static GF_Err ALSA_ConfigureOutput(GF_AudioOutput*dr, u32 *SampleRate, u32 *NbChannels, u32 *nbBitsPerSample, u32 channel_cfg) { snd_pcm_hw_params_t *hw_params = NULL; int err; int nb_bufs, sr, val, period_time; ALSAContext *ctx = (ALSAContext*)dr->opaque; if (!ctx) return GF_BAD_PARAM; /*close device*/ if (ctx->playback_handle) { snd_pcm_close(ctx->playback_handle); ctx->playback_handle = NULL; } if (ctx->wav_buf) free(ctx->wav_buf); ctx->wav_buf = NULL; err = snd_pcm_open(&ctx->playback_handle, ctx->dev_name, SND_PCM_STREAM_PLAYBACK, 0/*SND_PCM_NONBLOCK*/); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot open audio device %s: %s\n", ctx->dev_name, snd_strerror (err)) ); return GF_IO_ERR; } err = snd_pcm_hw_params_malloc(&hw_params); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot allocate hardware params: %s\n", snd_strerror (err)) ); goto err_exit; } err = snd_pcm_hw_params_any(ctx->playback_handle, hw_params); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot initialize hardware params: %s\n", snd_strerror (err)) ); goto err_exit; } err = snd_pcm_hw_params_set_access(ctx->playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set access type: %s\n", snd_strerror (err)) ); goto err_exit; } /*set output format*/ ctx->nb_ch = (int) (*NbChannels); ctx->block_align = ctx->nb_ch; if ((*nbBitsPerSample) == 16) { ctx->block_align *= 2; err = snd_pcm_hw_params_set_format(ctx->playback_handle, hw_params, SND_PCM_FORMAT_S16_LE); } else { err = snd_pcm_hw_params_set_format(ctx->playback_handle, hw_params, SND_PCM_FORMAT_U8); } if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set sample format: %s\n", snd_strerror (err)) ); goto err_exit; } /*set output sample rate*/ if (ctx->force_sr) *SampleRate = ctx->force_sr; sr = *SampleRate; err = snd_pcm_hw_params_set_rate_near(ctx->playback_handle, hw_params, SampleRate, 0); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set sample rate: %s\n", snd_strerror (err)) ); goto err_exit; } if (sr != *SampleRate) { GF_LOG(GF_LOG_INFO, GF_LOG_MMIO, ("[ALSA] Sample rate %d not supported, using %d instead\n", sr, *SampleRate ) ); sr = *SampleRate; } /*set output channels*/ err = snd_pcm_hw_params_set_channels_near(ctx->playback_handle, hw_params, NbChannels); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set channel count: %s\n", snd_strerror (err)) ); goto err_exit; } if (ctx->nb_ch != *NbChannels) { GF_LOG(GF_LOG_INFO, GF_LOG_MMIO, ("[ALSA] %d channels not supported - using %d instead\n", ctx->nb_ch, *NbChannels ) ); ctx->block_align /= ctx->nb_ch; ctx->nb_ch = *NbChannels; ctx->block_align *= ctx->nb_ch; } err = snd_pcm_hw_params(ctx->playback_handle, hw_params); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set parameters: %s\n", snd_strerror (err)) ); goto err_exit; } /* Set number of buffers*/ snd_pcm_hw_params_get_periods_min(hw_params, &val, 0); nb_bufs = (ctx->num_buffers>val) ? ctx->num_buffers : val; err = snd_pcm_hw_params_set_periods_near(ctx->playback_handle, hw_params, &nb_bufs, 0); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set number of HW buffers (%d): %s\n", nb_bufs, snd_strerror(err) )); goto err_exit; } /* Set total buffer size*/ if (ctx->total_duration) { ctx->buf_size = (sr * ctx->total_duration)/1000; } else { ctx->buf_size = 2048; } ctx->buf_size /= nb_bufs; err = snd_pcm_hw_params_set_period_size_near(ctx->playback_handle, hw_params, (snd_pcm_uframes_t *)&ctx->buf_size, 0); if (err < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_MMIO, ("[ALSA] Cannot set HW buffer size (%d): %s\n", ctx->buf_size, snd_strerror(err) )); goto err_exit; } /*get complete buffer size*/ snd_pcm_hw_params_get_buffer_size(hw_params, (snd_pcm_uframes_t *)&ctx->buf_size); ctx->buf_size *= ctx->block_align; /*get period time*/ snd_pcm_hw_params_get_period_time(hw_params, &period_time, 0); snd_pcm_hw_params_free (hw_params); hw_params = NULL; ctx->delay = (ctx->buf_size*1000) / (sr*ctx->block_align); /*allocate a single buffer*/ ctx->wav_buf = malloc(ctx->buf_size*sizeof(char)); if(!ctx->wav_buf) return GF_OUT_OF_MEM; memset(ctx->wav_buf, 0, ctx->buf_size*sizeof(char)); GF_LOG(GF_LOG_DEBUG, GF_LOG_MMIO, ("[ALSA] Setup %d ch @ %d hz - %d periods of %d us - total buffer size %d - overall delay %d ms\n", ctx->nb_ch, sr, nb_bufs, period_time, ctx->buf_size, ctx->delay)); return GF_OK; err_exit: if (hw_params) snd_pcm_hw_params_free(hw_params); snd_pcm_close(ctx->playback_handle); ctx->playback_handle = NULL; return GF_IO_ERR; }
bool open() { // Open the Alsa playback device. int err=-1,count=0; unsigned int freakuency = frequency; while((count < 5) && (err < 0)) { err = snd_pcm_open ( &handle, ALSA_OUTPUT_NAME, SND_PCM_STREAM_PLAYBACK, 0 ); if(err < 0) { count++; qWarning()<<"QAudioOutput::open() err="<<err<<", count="<<count; } } if (( err < 0)||(handle == 0)) { qWarning( "QAudioOuput: snd_pcm_open: error %d", err ); return false; } snd_pcm_nonblock( handle, 0 ); // Set the desired HW parameters. snd_pcm_hw_params_t *hwparams; snd_pcm_hw_params_alloca( &hwparams ); err = snd_pcm_hw_params_any( handle, hwparams ); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_any: err %d", err); return false; } err = snd_pcm_hw_params_set_access( handle, hwparams, access ); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_access: err %d", err); return false; } err = snd_pcm_hw_params_set_format( handle, hwparams, ( bitsPerSample == 16 ? SND_PCM_FORMAT_S16 : SND_PCM_FORMAT_U8 ) ); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_format: err %d", err); return false; } err = snd_pcm_hw_params_set_channels ( handle, hwparams, (unsigned int)channels ); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_channels: err %d", err); return false; } err = snd_pcm_hw_params_set_rate_near ( handle, hwparams, &freakuency, 0 ); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_rate_near: err %d", err); return false; } #ifndef ALSA_BUFFER_SIZE err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err %d",err); } err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_get_buffer_size: err %d",err); } #else buffer_size = ALSA_BUFFER_SIZE; err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err %d",err); } #endif #ifndef ALSA_PERIOD_SIZE period_time = buffer_time/4; err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_period_time_near: err %d",err); } #else period_size = ALSA_PERIOD_SIZE; err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, 0); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params_set_period_size_near: err %d",err); } #endif err = snd_pcm_hw_params(handle, hwparams); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_hw_params: err %d", err); return false; } int dir; unsigned int vval, vval2; snd_pcm_access_t val; snd_pcm_format_t fval; snd_pcm_subformat_t sval; qLog(QAudioOutput) << "PCM handle name =" << snd_pcm_name(handle); qLog(QAudioOutput) << "PCM state =" << snd_pcm_state_name(snd_pcm_state(handle)); snd_pcm_hw_params_get_access(hwparams,&val); vval = (unsigned int)val; if ( (int)vval != (int)access ) { qWarning( "QAudioInput: access type not set, want %s got %s", snd_pcm_access_name((snd_pcm_access_t)access), snd_pcm_access_name((snd_pcm_access_t)vval) ); access = (snd_pcm_access_t)vval; } qLog(QAudioOutput) << "access type =" << snd_pcm_access_name((snd_pcm_access_t)vval); snd_pcm_hw_params_get_format(hwparams, &fval); vval = (unsigned int)fval; if ( (int)vval != (int)format ) { qWarning( "QAudioInput: format type not set, want %s got %s", snd_pcm_format_name((snd_pcm_format_t)format), snd_pcm_format_name((snd_pcm_format_t)vval) ); format = (snd_pcm_format_t)vval; } qLog(QAudioOutput) << QString("format = '%1' (%2)").arg(snd_pcm_format_name((snd_pcm_format_t)vval)) .arg(snd_pcm_format_description((snd_pcm_format_t)vval)) .toLatin1().constData(); snd_pcm_hw_params_get_subformat(hwparams,&sval); vval = (unsigned int)sval; qLog(QAudioOutput) << QString("subformat = '%1' (%2)").arg(snd_pcm_subformat_name((snd_pcm_subformat_t)vval)) .arg(snd_pcm_subformat_description((snd_pcm_subformat_t)vval)) .toLatin1().constData(); snd_pcm_hw_params_get_channels(hwparams, &vval); if ( (int)vval != (int)channels ) { qWarning( "QAudioInput: channels type not set, want %d got %d",channels,vval); channels = vval; } qLog(QAudioOutput) << "channels =" << vval; snd_pcm_hw_params_get_rate(hwparams, &vval, &dir); if ( (int)vval != (int)frequency ) { qWarning( "QAudioInput: frequency type not set, want %d got %d",frequency,vval); frequency = vval; } qLog(QAudioOutput) << "rate =" << vval << "bps"; snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); qLog(QAudioOutput) << "period time =" << period_time << "us"; snd_pcm_hw_params_get_period_size(hwparams,&period_size, &dir); qLog(QAudioOutput) << "period size =" << (int)period_size << "frames"; qLog(QAudioOutput) << "buffer time =" << (int)buffer_time << "us"; qLog(QAudioOutput) << "buffer size =" << (int)buffer_size << "frames"; snd_pcm_hw_params_get_periods(hwparams, &vval, &dir); periods = vval; qLog(QAudioOutput) << "periods per buffer =" << vval << "frames"; snd_pcm_hw_params_get_rate_numden(hwparams, &vval, &vval2); qLog(QAudioOutput) << QString("exact rate = %1/%2 bps").arg(vval).arg(vval2).toLatin1().constData(); vval = snd_pcm_hw_params_get_sbits(hwparams); qLog(QAudioOutput) << "significant bits =" << vval; vval = snd_pcm_hw_params_is_batch(hwparams); qLog(QAudioOutput) << "is batch =" << vval; vval = snd_pcm_hw_params_is_block_transfer(hwparams); qLog(QAudioOutput) << "is block transfer =" << vval; vval = snd_pcm_hw_params_is_double(hwparams); qLog(QAudioOutput) << "is double =" << vval; vval = snd_pcm_hw_params_is_half_duplex(hwparams); qLog(QAudioOutput) << "is half duplex =" << vval; vval = snd_pcm_hw_params_is_joint_duplex(hwparams); qLog(QAudioOutput) << "is joint duplex =" << vval; vval = snd_pcm_hw_params_can_overrange(hwparams); qLog(QAudioOutput) << "can overrange =" << vval; vval = snd_pcm_hw_params_can_mmap_sample_resolution(hwparams); qLog(QAudioOutput) << "can mmap =" << vval; vval = snd_pcm_hw_params_can_pause(hwparams); qLog(QAudioOutput) << "can pause =" << vval; vval = snd_pcm_hw_params_can_resume(hwparams); qLog(QAudioOutput) << "can resume =" << vval; vval = snd_pcm_hw_params_can_sync_start(hwparams); qLog(QAudioOutput) << "can sync start =" << vval; // Set the desired SW parameters. snd_pcm_sw_params_t *swparams; snd_pcm_sw_params_alloca(&swparams); err = snd_pcm_sw_params_current(handle, swparams); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_sw_params_current: err %d",err); } err = snd_pcm_sw_params_set_start_threshold(handle,swparams,buffer_size); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_sw_params_set_start_threshold: err %d",err); } err = snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_size); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_sw_params_set_stop_threshold: err %d",err); } err = snd_pcm_sw_params_set_avail_min(handle, swparams,period_size); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_sw_params_set_avail_min: err %d",err); } err = snd_pcm_sw_params(handle, swparams); if ( err < 0 ) { qWarning( "QAudioOutput: snd_pcm_sw_params: err %d",err); } // Prepare for audio output. snd_pcm_prepare( handle ); return true; }
int main( int argc, char *argv[] ) { struct structArgs cmdArgs; unsigned int val, val2; int dir; int errNum; cmdArgs.card = 0; // Default card. cmdArgs.control = 1; // Default control. // ************************************************************************ // ALSA control elements. // ************************************************************************ snd_pcm_t *pcmp; snd_pcm_hw_params_t *params; // snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; snd_pcm_stream_t stream = SND_PCM_STREAM_CAPTURE; snd_pcm_uframes_t frames; // ************************************************************************ // Get command line parameters. // ************************************************************************ argp_parse( &argp, argc, argv, 0, 0, &cmdArgs ); printf( "Card = %i\n", cmdArgs.card ); printf( "Control = %i\n", cmdArgs.control ); sprintf( cmdArgs.deviceID, "hw:%i,%i", cmdArgs.card, cmdArgs.control ); printf( "Using device %s :", cmdArgs.deviceID ); /* Allocate a hardware parameters object. */ if ( snd_pcm_hw_params_alloca( ¶ms ) < 0 ) { fprintf( stderr, "Unable to allocate.\n" ); return -1; } /* Open PCM device for playback. */ // if ( snd_pcm_open( &pcmp, cmdArgs.deviceID, stream, 0 ) < 0 ) // { // fprintf( stderr, "Unable to open pcm device.\n" ); // return -1; // } /* Fill it in with default values. */ // if ( snd_pcm_hw_params_any( pcmp, params ) < 0 // { // fprintf( stderr, "Unable to set default values.\n" ); // return -1; // } /* Interleaved mode */ // snd_pcm_hw_params_set_access( pcmp, params, // SND_PCM_ACCESS_RW_INTERLEAVED ); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format( pcmp, params, SND_PCM_FORMAT_S16_LE ); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels( pcmp, params, 2 ); /* 44100 bits/second sampling rate (CD quality) */ val = 44100; snd_pcm_hw_params_set_rate_near( pcmp, params, &val, &dir ); /* Write the parameters to the driver */ errNum = snd_pcm_hw_params( pcmp, params ); if ( errNum < 0 ) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror( errNum )); exit( 1 ); } /* Display information about the PCM interface */ printf( "PCM handle name = '%s'\n", snd_pcm_name( pcmp )); printf("PCM state = %s\n", snd_pcm_state_name( snd_pcm_state( pcmp ))); snd_pcm_hw_params_get_access( params, ( snd_pcm_access_t * ) &val ); printf( "access type = %s\n", snd_pcm_access_name(( snd_pcm_access_t ) val )); snd_pcm_hw_params_get_format( params, ( snd_pcm_format_t * ) &val ); printf( "format = '%s' (%s)\n", snd_pcm_format_name(( snd_pcm_format_t ) val ), snd_pcm_format_description(( snd_pcm_format_t ) val )); snd_pcm_hw_params_get_subformat( params, ( snd_pcm_subformat_t * ) &val ); printf( "subformat = '%s' (%s)\n", snd_pcm_subformat_name(( snd_pcm_subformat_t ) val ), snd_pcm_subformat_description(( snd_pcm_subformat_t ) val )); snd_pcm_hw_params_get_channels( params, &val ); printf( "channels = %d\n", val ); snd_pcm_hw_params_get_rate( params, &val, &dir ); printf( "rate = %d bps\n", val ); snd_pcm_hw_params_get_period_time( params, &val, &dir ); printf( "period time = %d us\n", val ); snd_pcm_hw_params_get_period_size( params, &frames, &dir ); printf( "period size = %d frames\n", ( int ) frames ); snd_pcm_hw_params_get_buffer_time( params, &val, &dir ); printf( "buffer time = %d us\n", val ); snd_pcm_hw_params_get_buffer_size( params, ( snd_pcm_uframes_t * ) &val ); printf( "buffer size = %d frames\n", val ); snd_pcm_hw_params_get_periods( params, &val, &dir ); printf( "periods per buffer = %d frames\n", val ); snd_pcm_hw_params_get_rate_numden( params, &val, &val2 ); printf( "exact rate = %d/%d bps\n", val, val2 ); val = snd_pcm_hw_params_get_sbits( params ); printf( "significant bits = %d\n", val ); // snd_pcm_hw_params_get_tick_time( params, &val, &dir ); // printf( "tick time = %d us\n", val ); val = snd_pcm_hw_params_is_batch( params ); printf( "is batch = %d\n", val ); val = snd_pcm_hw_params_is_block_transfer( params ); printf( "is block transfer = %d\n", val ); val = snd_pcm_hw_params_is_double( params ); printf( "is double = %d\n", val ); val = snd_pcm_hw_params_is_half_duplex( params ); printf( "is half duplex = %d\n", val ); val = snd_pcm_hw_params_is_joint_duplex( params ); printf( "is joint duplex = %d\n", val ); val = snd_pcm_hw_params_can_overrange( params ); printf( "can overrange = %d\n", val ); val = snd_pcm_hw_params_can_mmap_sample_resolution( params ); printf( "can mmap = %d\n", val ); val = snd_pcm_hw_params_can_pause( params ); printf( "can pause = %d\n", val ); val = snd_pcm_hw_params_can_resume( params ); printf( "can resume = %d\n", val ); val = snd_pcm_hw_params_can_sync_start( params ); printf( "can sync start = %d\n", val ); snd_pcm_close( pcmp ); return 0; }
void play(int sec, int freq, char *file) { long loops; int rc; int size; snd_pcm_t *handle; snd_pcm_hw_params_t *params; unsigned int val; int dir; snd_pcm_uframes_t frames; char *buffer; int fd; /* Check ranges */ if (sec < 1 || sec > 4000) { printf("WARNING: Incorrect time to play range [1,4000] s\n"); printf("\tSetting time to play to 5s...\n"); sec = 5; } if (freq < 1000 || freq > 100000) { printf("ERROR: Incorrect frequency range [1000,100000] Hz\n"); printf("\tSetting frequency to 44.1 kHz...\n"); freq = 44100; } /* Open file */ fd = open(file, O_RDONLY); if (fd < 0) { /* There was an error opening the file */ printf("ERROR: Couldn't open file to play\n"); printf("\tPlease make sure file exists\n"); } else { /* Print that the file is playing with its parameters */ printf("Playing file %s for %d seconds", file, sec); printf(" and frequency %d...\n", freq); /* Open PCM device for playback */ rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0); if (rc < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values */ snd_pcm_hw_params_any(handle, params); /* Set hardware parameters */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 2); /* freq bits/second sampling rate (CD quality) */ val = freq; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to 32 frames */ frames = 32; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &frames, &dir); size = frames * 4; /* 2 bytes/sample, 2 channels */ buffer = (char *) malloc(size); /* We want to loop for sec seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); /* sec seconds in microseconds divided by period time */ loops = sec*1000000 / val; while (loops > 0) { loops--; rc = read(fd, buffer, size); if (rc == 0) { fprintf(stderr, "end of file on input\n"); break; } else if (rc != size) { fprintf(stderr, "short read: read %d bytes\n", rc); } rc = snd_pcm_writei(handle, buffer, frames); if (rc == -EPIPE) { /* EPIPE means underrun */ fprintf(stderr, "underrun occurred\n"); snd_pcm_prepare(handle); } else if (rc < 0) { fprintf(stderr, "error from writei: %s\n", snd_strerror(rc)); } else if (rc != (int)frames) { fprintf(stderr, "short write, write %d frames\n", rc); } } /*Close handle and free buffer*/ snd_pcm_drain(handle); snd_pcm_close(handle); free(buffer); } }
int main(int argc, char **argv){ int i; int aver,val,val2; int16_t buf[BUFSIZE]; double d_buffer[BUFSIZE]; double pitch = 0.0; struct timespec before,after; struct pitch_tracker_params *settings; snd_pcm_uframes_t period_size = PERIOD_SIZE; //ALSA PCM configuration snd_pcm_t *handle; snd_pcm_hw_params_t *params; int dir,rc; snd_pcm_uframes_t frames; /* Open PCM device for capture */ rc = snd_pcm_open( &handle, "default" , SND_PCM_STREAM_CAPTURE , 0); if(rc < 0 ){ fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params,SND_PCM_ACCESS_RW_INTERLEAVED); /* unsigned 16-bit format */ snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_S16); snd_pcm_hw_params_set_channels(handle, params, 1); /* 44100 bits/second sampling rate */ val = 44100; snd_pcm_hw_params_set_rate_near(handle,params, &val, &dir); /* set size time*/ snd_pcm_hw_params_set_period_size_near(handle, params, &period_size , &dir); /* write configuration */ rc = snd_pcm_hw_params(handle,params); /*Display info*/ printf("PCM handle name = '%s'\n", snd_pcm_name(handle)); printf("PCM state = %s\n", snd_pcm_state_name(snd_pcm_state(handle))); snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) &val); printf("access type = %s\n", snd_pcm_access_name((snd_pcm_access_t)val)); snd_pcm_hw_params_get_format(params, &val); printf("format = '%s' (%s)\n", snd_pcm_format_name((snd_pcm_format_t)val), snd_pcm_format_description( (snd_pcm_format_t)val)); snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val); printf("subformat = '%s' (%s)\n", snd_pcm_subformat_name((snd_pcm_subformat_t)val), snd_pcm_subformat_description( (snd_pcm_subformat_t)val)); snd_pcm_hw_params_get_channels(params, &val); printf("channels = %d\n", val); snd_pcm_hw_params_get_rate(params, &val, &dir); printf("rate = %d bps\n", val); snd_pcm_hw_params_get_period_time(params, &val, &dir); printf("period time = %d us\n", val); snd_pcm_hw_params_get_period_size(params, &frames, &dir); printf("period size = %d frames\n", (int)frames); snd_pcm_hw_params_get_buffer_time(params, &val, &dir); printf("buffer time = %d us\n", val); snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val); printf("buffer size = %d frames\n", val); snd_pcm_hw_params_get_periods(params, &val, &dir); printf("periods per buffer = %d frames\n", val); snd_pcm_hw_params_get_rate_numden(params, &val, &val2); printf("exact rate = %d/%d bps\n", val, val2); val = snd_pcm_hw_params_get_sbits(params); printf("significant bits = %d\n", val); val = snd_pcm_hw_params_is_batch(params); printf("is batch = %d\n", val); val = snd_pcm_hw_params_is_block_transfer(params); printf("is block transfer = %d\n", val); val = snd_pcm_hw_params_is_double(params); printf("is double = %d\n", val); val = snd_pcm_hw_params_is_half_duplex(params); printf("is half duplex = %d\n", val); val = snd_pcm_hw_params_is_joint_duplex(params); printf("is joint duplex = %d\n", val); val = snd_pcm_hw_params_can_overrange(params); printf("can overrange = %d\n", val); val = snd_pcm_hw_params_can_mmap_sample_resolution(params); printf("can mmap = %d\n", val); val = snd_pcm_hw_params_can_pause(params); printf("can pause = %d\n", val); val = snd_pcm_hw_params_can_resume(params); printf("can resume = %d\n", val); val = snd_pcm_hw_params_can_sync_start(params); printf("can sync start = %d\n", val); settings = open_pitch_tracker(); while(1){ rc = snd_pcm_readi(handle, buf, frames); if (rc == -EPIPE) { /* EPIPE means overrun */ fprintf(stderr, "overrun occurred\n"); snd_pcm_prepare(handle); } else if (rc < 0) { fprintf(stderr, "error from read: %s\n", snd_strerror(rc)); } else if (rc != (int)frames) { fprintf(stderr, "short read, read %d frames\n", rc); } for( i = 0 ; i< BUFSIZE ; i++){ d_buffer[i] = (double) buf[i]; } pitch = compute_pitch(d_buffer, BUFSIZE, S16, settings ,ACCURACY); if( pitch != 0.0 ) printf("frequency -> %f\n",pitch); memset(buf,0,BUFSIZE); } close_pitch_tracker(&settings); snd_pcm_drain(handle); snd_pcm_close(handle); return 0; }
bool QAlsaAudioInput::open() { #ifdef DEBUG_AUDIO QTime now(QTime::currentTime()); qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; #endif clockStamp.restart(); timeStamp.restart(); elapsedTimeOffset = 0; int dir; int err = 0; int count=0; unsigned int sampleRate=settings.sampleRate(); if (!settings.isValid()) { qWarning("QAudioInput: open error, invalid format."); } else if (settings.sampleRate() <= 0) { qWarning("QAudioInput: open error, invalid sample rate (%d).", settings.sampleRate()); } else { err = -1; } if (err == 0) { errorState = QAudio::OpenError; deviceState = QAudio::StoppedState; emit errorChanged(errorState); return false; } QString dev = QString(QLatin1String(m_device.constData())); QList<QByteArray> devices = QAlsaAudioDeviceInfo::availableDevices(QAudio::AudioInput); if(dev.compare(QLatin1String("default")) == 0) { #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) if (devices.size() > 0) dev = QLatin1String(devices.first()); else return false; #else dev = QLatin1String("hw:0,0"); #endif } else { #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) dev = QLatin1String(m_device); #else int idx = 0; char *name; QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); while(snd_card_get_name(idx,&name) == 0) { if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) break; idx++; } dev = QString(QLatin1String("hw:%1,0")).arg(idx); #endif } // Step 1: try and open the device while((count < 5) && (err < 0)) { err=snd_pcm_open(&handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_CAPTURE,0); if(err < 0) count++; } if (( err < 0)||(handle == 0)) { errorState = QAudio::OpenError; deviceState = QAudio::StoppedState; emit stateChanged(deviceState); return false; } snd_pcm_nonblock( handle, 0 ); // Step 2: Set the desired HW parameters. snd_pcm_hw_params_alloca( &hwparams ); bool fatal = false; QString errMessage; unsigned int chunks = 8; err = snd_pcm_hw_params_any( handle, hwparams ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_any: err = %1").arg(err); } if ( !fatal ) { err = snd_pcm_hw_params_set_rate_resample( handle, hwparams, 1 ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_access( handle, hwparams, access ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_access: err = %1").arg(err); } } if ( !fatal ) { err = setFormat(); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_format: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channelCount() ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_channels: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &sampleRate, 0 ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params(handle, hwparams); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioInput: snd_pcm_hw_params: err = %1").arg(err); } } if( err < 0) { qWarning()<<errMessage; errorState = QAudio::OpenError; deviceState = QAudio::StoppedState; emit stateChanged(deviceState); return false; } snd_pcm_hw_params_get_buffer_size(hwparams,&buffer_frames); buffer_size = snd_pcm_frames_to_bytes(handle,buffer_frames); snd_pcm_hw_params_get_period_size(hwparams,&period_frames, &dir); period_size = snd_pcm_frames_to_bytes(handle,period_frames); snd_pcm_hw_params_get_buffer_time(hwparams,&buffer_time, &dir); snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); // Step 3: Set the desired SW parameters. snd_pcm_sw_params_t *swparams; snd_pcm_sw_params_alloca(&swparams); snd_pcm_sw_params_current(handle, swparams); snd_pcm_sw_params_set_start_threshold(handle,swparams,period_frames); snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_frames); snd_pcm_sw_params_set_avail_min(handle, swparams,period_frames); snd_pcm_sw_params(handle, swparams); // Step 4: Prepare audio ringBuffer.resize(buffer_size); snd_pcm_prepare( handle ); snd_pcm_start(handle); // Step 5: Setup timer bytesAvailable = checkBytesReady(); if(pullMode) connect(audioSource,SIGNAL(readyRead()),this,SLOT(userFeed())); // Step 6: Start audio processing chunks = buffer_size/period_size; timer->start(period_time*chunks/2000); errorState = QAudio::NoError; totalTimeValue = 0; return true; }
int main() { int rc; snd_pcm_t* handle; snd_pcm_hw_params_t* params; unsigned int val; unsigned int val2; int dir; snd_pcm_uframes_t frames; if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) { std::cerr << "unable to open pcm devices: " << snd_strerror(rc) << std::endl; exit(1); } snd_pcm_hw_params_alloca(¶ms); snd_pcm_hw_params_any(handle, params); snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_channels(handle, params, 2); val = 44100; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); if ( (rc = snd_pcm_hw_params(handle, params)) < 0) { std::cerr << "unable to set hw parameters: " << snd_strerror(rc) << std::endl; exit(1); } std::cout << "PCM handle name = " << snd_pcm_name(handle) << std::endl; std::cout << "PCM state = " << snd_pcm_state_name(snd_pcm_state(handle)) << std::endl; snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *)&val); std::cout << "access type = " << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl; snd_pcm_hw_params_get_format(params, (snd_pcm_format_t*)(&val)); std::cout << "format = '" << snd_pcm_format_name((snd_pcm_format_t)val) << "' (" << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl; snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val); std::cout << "subformat = '" << snd_pcm_subformat_name((snd_pcm_subformat_t)val) << "' (" << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl; snd_pcm_hw_params_get_channels(params, &val); std::cout << "channels = " << val << std::endl; snd_pcm_hw_params_get_rate(params, &val, &dir); std::cout << "rate = " << val << " bps" << std::endl; snd_pcm_hw_params_get_period_time(params, &val, &dir); std::cout << "period time = " << val << " us" << std::endl; snd_pcm_hw_params_get_period_size(params, &frames, &dir); std::cout << "period size = " << static_cast<int>(frames) << " frames" << std::endl; snd_pcm_hw_params_get_buffer_time(params, &val, &dir); std::cout << "buffer time = " << val << " us" << std::endl; snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val); std::cout << "buffer size = " << val << " frames" << std::endl; snd_pcm_hw_params_get_periods(params, &val, &dir); std::cout << "periods per buffer = " << val << " frames" << std::endl; snd_pcm_hw_params_get_rate_numden(params, &val, &val2); std::cout << "exact rate = " << val/val2 << " bps" << std::endl; val = snd_pcm_hw_params_get_sbits(params); std::cout << "significant bits = " << val << std::endl; snd_pcm_hw_params_get_tick_time(params, &val, &dir); std::cout << "tick time = " << val << " us" << std::endl; val = snd_pcm_hw_params_is_batch(params); std::cout << "is batch = " << val << std::endl; val = snd_pcm_hw_params_is_block_transfer(params); std::cout << "is block transfer = " << val << std::endl; val = snd_pcm_hw_params_is_double(params); std::cout << "is double = " << val << std::endl; val = snd_pcm_hw_params_is_half_duplex(params); std::cout << "is half duplex = " << val << std::endl; val = snd_pcm_hw_params_is_joint_duplex(params); std::cout << "is joint duplex = " << val << std::endl; val = snd_pcm_hw_params_can_overrange(params); std::cout << "can overrange = " << val << std::endl; val = snd_pcm_hw_params_can_mmap_sample_resolution(params); std::cout << "can mmap = " << val << std::endl; val = snd_pcm_hw_params_can_pause(params); std::cout << "can pause = " << val << std::endl; val = snd_pcm_hw_params_can_resume(params); std::cout << "can resume = " << val << std::endl; val = snd_pcm_hw_params_can_sync_start(params); std::cout << "can sync start = " << val << std::endl; snd_pcm_close(handle); return 0; }
int main(int argc, char** argv) { snd_pcm_t *handle = NULL; snd_pcm_hw_params_t *params = NULL; snd_pcm_uframes_t frames = 64; int dir = 0, size, loops; unsigned int val; int ret = 0, n = 0; unsigned char *buf = NULL; FILE *fpcm = NULL; unsigned int rate; int format; int channel; int time; /* int i; for(i = 0; i < argc; i++) AUDIO_ALSA_DBG("param%d: %s\n", i, *(argv + i)); */ if(7 != argc) { AUDIO_ALSA_DBG("\n"); AUDIO_ALSA_DBG("param1 snd dev: alsa device name: default, plughw:0,0 or plughw:1,0\n"); AUDIO_ALSA_DBG("param2 filename: file used to save audio pcm data\n"); AUDIO_ALSA_DBG("param3 sampleRate: 8000 -- 48000 \n"); AUDIO_ALSA_DBG("param4 channel: 1:mono, 2:stereo\n"); AUDIO_ALSA_DBG("param5 format: 1:SND_PCM_FORMAT_U8, 2:SND_PCM_FORMAT_S16_LE\n"); AUDIO_ALSA_DBG("param6 record time: record time(unit: second)\n\n"); return -1; } rate = strtoul(*(argv + 3), NULL, 10); channel = strtoul(*(argv + 4), NULL, 10); format = strtoul(*(argv + 5), NULL, 10); time = strtoul(*(argv + 6), NULL, 10); if(rate < 8000 || rate > 48000) { AUDIO_ALSA_DBG("param2 sampleRate error: 8000 -- 48000 \n"); ret = -1; goto error; } if(channel < 1 || channel > 2) { AUDIO_ALSA_DBG("param3 channel error: 1:mono, 2:stereo\n"); ret = -1; goto error; } if(format < SND_PCM_FORMAT_U8 || format > SND_PCM_FORMAT_S16_LE) { AUDIO_ALSA_DBG("param4 format error: 1:SND_PCM_FORMAT_U8, 2:SND_PCM_FORMAT_S16_LE\n"); ret = -1; goto error; } AUDIO_ALSA_DBG("rate = %u channel = %s format = %s time = %d\n", rate, (1 == channel) ? "mono" : "stereo", (1 == format) ? "SND_PCM_FORMAT_U8" : "SND_PCM_FORMAT_S16_LE", time); if((ret = snd_pcm_open(&handle, *(argv + 1), SND_PCM_STREAM_CAPTURE, 0)) < 0) { AUDIO_ALSA_DBG("snd_pcm_open error: %s\n", snd_strerror(ret)); handle = NULL; goto error; } snd_pcm_hw_params_malloc(¶ms); if(!params) { AUDIO_ALSA_DBG("snd_pcm_hw_params_alloca error ... \n"); ret = -1; goto error; } snd_pcm_hw_params_any(handle, params); snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); if((ret = snd_pcm_hw_params_set_format(handle ,params, format)) < 0) { AUDIO_ALSA_DBG("format set error: %s\n", snd_strerror(ret)); goto error; } if((ret = snd_pcm_hw_params_set_channels(handle, params, channel)) < 0) { AUDIO_ALSA_DBG("channel set error: %s\n", snd_strerror(ret)); goto error; } if((ret = snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir)) < 0) { AUDIO_ALSA_DBG("sampleRate set error: %s\n", snd_strerror(ret)); goto error; } snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); if((ret = snd_pcm_hw_params(handle, params)) < 0) { AUDIO_ALSA_DBG("snd_pcm_hw_params error: %s\n", snd_strerror(ret)); goto error; } snd_pcm_hw_params_get_period_size(params, &frames, &dir); size = frames * channel * format; buf = (unsigned char*)malloc(size); if(!buf) { AUDIO_ALSA_DBG("malloc buf error: buf size = %d\n", size); ret = -1; goto error; } snd_pcm_hw_params_get_period_time(params, &val, &dir); loops = time * 1000000 / val; AUDIO_ALSA_DBG("loops = %d\n", loops); fpcm = fopen(*(argv + 2), "wb"); if(!fpcm) { AUDIO_ALSA_DBG("Can not open file %s ... \n", *(argv + 2)); goto error; } while(loops-- > 0) { n = snd_pcm_readi(handle, buf, frames); if(-EPIPE == n) { AUDIO_ALSA_DBG("snd_pcm_readi over run ... \n"); snd_pcm_prepare(handle); continue; } else if(n < 0) { AUDIO_ALSA_DBG("snd_pcm_readi error: %s\n", snd_strerror(n)); ret = -1; break; } else if(n != frames) { AUDIO_ALSA_DBG("snd_pcm_readi read frames %d, less than %u\n", n, (unsigned int)frames); } fwrite(buf, 1, size, fpcm); } error: if(params) snd_pcm_hw_params_free(params); if(handle) snd_pcm_close(handle); if(buf) free(buf); if(fpcm) fclose(fpcm); return ret; }
bool SoundProcessor::SetUpPlayer(){ /* Open PCM device for playback. */ rc_s = snd_pcm_open(&handle_s, "default", SND_PCM_STREAM_PLAYBACK, 0); if (rc_s < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc_s)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms_s); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle_s, params_s); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle_s, params_s, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle_s, params_s, SND_PCM_FORMAT_U8); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle_s, params_s, 1); /* 44100 bits/second sampling rate (CD quality) */ val_s = 8000; snd_pcm_hw_params_set_rate_near(handle_s, params_s, &val_s, &dir_s); //frames = 32; snd_pcm_hw_params_set_period_size_near(handle_s, params_s, &frames, &dir_s); /* Write the parameters to the driver */ rc_s = snd_pcm_hw_params(handle_s, params_s); if (rc_s < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc_s)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params_s, &frames, &dir_s); size = frames ; /* 2 bytes/sample, 2 channels */ buffer_s = (char *) malloc(size); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params_s, &val_s, &dir_s); snd_pcm_sframes_t availp; snd_pcm_sframes_t delayp; snd_pcm_avail_delay(handle_s,&availp,&delayp); printf("%l %l\n",availp,delayp); return true; }
int main(int argc , char *argv[]) { int socket_desc , client_sock , c , *new_sock; struct sockaddr_in server , client; pthread_t sniffer_thread1; snd_pcm_hw_params_t *params; unsigned int val; int dir; int rcm; //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons( 2008 ); //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); if (socket_desc == -1) { printf("Could not create socket"); } //Bind if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) { //print the error message perror("bind failed. Error"); return 1; } puts("bind done"); //Listen listen(socket_desc , 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); snd_pcm_open(&handle, "plughw:1,0", SND_PCM_STREAM_CAPTURE, 0); /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_U8); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 1); /* 44100 bits/second sampling rate (CD quality) */ val = 32768; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to 32 frames. */ frames = SIZE; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rcm = snd_pcm_hw_params(handle, params); if (rcm < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rcm)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &frames, &dir); /* 2 bytes/sample, 2 channels */ /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); while(1){ pthread_t sniffer_thread; if(client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) { puts("Connection accepted"); new_sock = malloc(4); *new_sock = client_sock; } if( pthread_create(&sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0) { perror("could not create thread"); return 1; } //Now join the thread , so that we dont terminate before the thread //pthread_join( sniffer_thread , NULL); puts("Handler assigned"); if (client_sock < 0) { perror("accept failed"); { perror("accept failed"); return 1; } } } snd_pcm_drain(handle); snd_pcm_close(handle); return 0; }
int main(int argc, char *argv[]) { int32_t dwLoopsToRecord; int32_t dwStatus; int32_t dwAudioBufferSize; snd_pcm_t *AlsaHandle; snd_pcm_hw_params_t *AlsaParams; uint32_t dwSamplingRate; int32_t dwDirection; snd_pcm_uframes_t AlsaFrames; int8_t *bAudioBuffer; uint32_t dwChannels; wave_t WavFile; /* confirm there are enough parameters */ if(argc < 4) { printf("Usage: alsa_record <interface> <sampling_rate> <channels>"); exit(1); } printf("Alsa Record Opening device: %s\n",argv[1]); /* Open PCM device for recording (capture). */ dwStatus = snd_pcm_open(&AlsaHandle, argv[1], SND_PCM_STREAM_CAPTURE, 0); if (dwStatus < 0) { fprintf(stderr,"unable to open pcm device: %s\n",snd_strerror(dwStatus)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(&AlsaParams); /* Fill it in with default values. */ snd_pcm_hw_params_any(AlsaHandle, AlsaParams); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(AlsaHandle, AlsaParams, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(AlsaHandle, AlsaParams, SND_PCM_FORMAT_S16_LE); /* Get the channels from the parameter */ dwChannels = atoi(argv[3]); printf("Alsa Record Recording %d channels\n",dwChannels); /* assign the channels for the wav file */ WavFile.wChannels = dwChannels; /* set channels to ALSA */ snd_pcm_hw_params_set_channels(AlsaHandle, AlsaParams, dwChannels); /* bits/second sampling rate */ dwSamplingRate = atoi(argv[2]); printf("Alsa Record Sampling rate: %i\n", dwSamplingRate); /* Set the sampling rate for the wav file */ WavFile.dwSampleRate = dwSamplingRate; /* Set the sampling rate to ASLA */ snd_pcm_hw_params_set_rate_near(AlsaHandle, AlsaParams, &dwSamplingRate, &dwDirection); /* Set period size to 32 frames. */ AlsaFrames = 32; snd_pcm_hw_params_set_period_size_near(AlsaHandle, AlsaParams, &AlsaFrames, &dwDirection); /* Write the parameters to the driver */ dwStatus = snd_pcm_hw_params(AlsaHandle, AlsaParams); if (dwStatus < 0) { fprintf(stderr,"unable to set hw parameters: %s\n",snd_strerror(dwStatus)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(AlsaParams, &AlsaFrames, &dwDirection); /* 2 bytes/sample, 2 channels */ dwAudioBufferSize = AlsaFrames * 2 * dwChannels; bAudioBuffer = (int8_t *) malloc(dwAudioBufferSize); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(AlsaParams, &dwSamplingRate, &dwDirection); /* Will record 5 seconds, get the amount of frames required */ dwLoopsToRecord = TIME_TO_RECORD_IN_US / dwSamplingRate; printf("SoundCard enabled and configured...\n"); printf("Opening output file\n"); /* Open the file and configure the wav header */ WavFile.dwWaveFileDescriptor = open("audio_mic.wav", O_WRONLY|O_CREAT,S_IRUSR|S_IRGRP|S_IROTH); WavFile.dwSeconds = TIME_TO_RECORD_IN_S; WavBuilder_vfnSetHeader(&WavFile); printf("Recording\n"); while (dwLoopsToRecord) { dwLoopsToRecord--; /* Read an audio frame */ dwStatus = snd_pcm_readi(AlsaHandle, bAudioBuffer, AlsaFrames); if (dwStatus == -EPIPE) { /* EPIPE means overrun */ fprintf(stderr, "overrun occurred\n"); snd_pcm_prepare(AlsaHandle); } else if (dwStatus < 0) { fprintf(stderr,"error from read: %s\n",snd_strerror(dwStatus)); } else if (dwStatus != (int32_t)AlsaFrames) { fprintf(stderr, "short read, read %d AlsaFrames\n", dwStatus); } /* Take the buffer and write it to the file */ WavBuilder_vfnWriteData(&WavFile,bAudioBuffer, dwAudioBufferSize); } printf("Done\n"); /* Once everything is done, close and release all resources */ snd_pcm_drain(AlsaHandle); snd_pcm_close(AlsaHandle); close(WavFile.dwWaveFileDescriptor); free(bAudioBuffer); return 0; }
int main() { long loops; int rc; int size; snd_pcm_t *handle; snd_pcm_hw_params_t *params; unsigned int val; int dir; snd_pcm_uframes_t frames; char *buffer; /* Open PCM device for recording (capture). */ rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 2); /* 44100 bits/second sampling rate (CD quality) */ val = 44100; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to 32 frames. */ frames = 32; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &frames, &dir); size = frames * 4; /* 2 bytes/sample, 2 channels */ buffer = (char *) malloc(size); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); loops = 5000000 / val; while (loops > 0) { loops--; rc = snd_pcm_readi(handle, buffer, frames); if (rc == -EPIPE) { /* EPIPE means overrun */ fprintf(stderr, "overrun occurred\n"); snd_pcm_prepare(handle); } else if (rc < 0) { fprintf(stderr, "error from read: %s\n", snd_strerror(rc)); } else if (rc != (int) frames) { fprintf(stderr, "short read, read %d frames\n", rc); } rc = write(1, buffer, size); if (rc != size) fprintf(stderr, "short write: wrote %d bytes\n", rc); } snd_pcm_drain(handle); snd_pcm_close(handle); free(buffer); return 0; }
int Aisound::Open_Pcm() { BEGINFUNC_USING_BY_VOICE int err; int dir; int size; char *buffer; unsigned int val; snd_pcm_uframes_t frames; snd_pcm_hw_params_t* params; int channels = 0; if ( (err = snd_pcm_open(&handle, "plug:cmd", SND_PCM_STREAM_PLAYBACK, 0)) < 0) { dbug_out("cannot open audio device (%s)\n", snd_strerror (err)); goto OpenPcmFail; } dbug_out("After snd_pcm_open for SND_PCM_STREAM_PLAYBACK"); snd_pcm_hw_params_alloca(¶ms);//为å�‚æ•°å�˜é‡�分é…�空间 if( (err = snd_pcm_hw_params_any(handle, params)) < 0)//å�‚æ•°åˆ�始化 { dbug_out("snd_pcm_hw_params_any failed! err = %d\n", err); goto OpenPcmFail; } if( (err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)//设置为交错模å¼� { dbug_out("snd_pcm_hw_params_set_access failed! err = %d\n", err); goto OpenPcmFail; } if( (err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)) < 0)//使用16ä½�æ ·æœ¬ { dbug_out("snd_pcm_hw_params_set_format failed! err = %d\n", err); goto OpenPcmFail; } channels = val = 1; if( (err = snd_pcm_hw_params_set_channels(handle, params, val)) < 0)//设置为立体声 { dbug_out("snd_pcm_hw_params_set_channels failed! err = %d\n", err); goto OpenPcmFail; } val = 16000; if( (err = snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir)) < 0)//è®¾ç½®é‡‡æ ·çŽ‡ä¸º44.1KHz { dbug_out("snd_pcm_hw_params_set_rate_near failed! err = %d\n", err); goto OpenPcmFail; } #if 1 frames = 32; if( (err = snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir)) < 0) { dbug_out("snd_pcm_hw_params_set_period_size_near failed! err = %d\n", err); goto OpenPcmFail; } if ( (err = snd_pcm_hw_params(handle, params)) < 0) { dbug_out("snd_pcm_hw_params-failed! err = %d err: %s\n", err, snd_strerror(err)); goto OpenPcmFail; } if( (err = snd_pcm_hw_params_get_period_size(params, &frames, &dir)) < 0) { dbug_out("snd_pcm_hw_params_get_period_size failed! err = %d\n", err); goto OpenPcmFail; } size = frames * channels * 2; buffer = (char *)malloc(sizeof(char) * size); if( (err = snd_pcm_hw_params_get_period_time(params, &val, &dir)) < 0) { dbug_out("snd_pcm_hw_params_get_period_time failed! err = %d\n", err); goto OpenPcmFail; } return 0; OpenPcmFail: return -1; #endif ENDFUNC_USING_BY_VOICE }
bool QAudioOutputPrivate::open() { if(opened) return true; #ifdef DEBUG_AUDIO QTime now(QTime::currentTime()); qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; #endif timeStamp.restart(); elapsedTimeOffset = 0; int dir; int err=-1; int count=0; unsigned int freakuency=settings.frequency(); QString dev = QLatin1String(m_device.constData()); QList<QByteArray> devices = QAudioDeviceInfoInternal::availableDevices(QAudio::AudioOutput); if(dev.compare(QLatin1String("default")) == 0) { #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) dev = QLatin1String(devices.first().constData()); #else dev = QLatin1String("hw:0,0"); #endif } else { #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) dev = QLatin1String(m_device); #else int idx = 0; char *name; QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); while(snd_card_get_name(idx,&name) == 0) { if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) break; idx++; } dev = QString(QLatin1String("hw:%1,0")).arg(idx); #endif } // Step 1: try and open the device while((count < 5) && (err < 0)) { err=snd_pcm_open(&handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0); if(err < 0) count++; } if (( err < 0)||(handle == 0)) { errorState = QAudio::OpenError; deviceState = QAudio::StoppedState; return false; } snd_pcm_nonblock( handle, 0 ); // Step 2: Set the desired HW parameters. snd_pcm_hw_params_alloca( &hwparams ); bool fatal = false; QString errMessage; unsigned int chunks = 8; err = snd_pcm_hw_params_any( handle, hwparams ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_any: err = %1").arg(err); } if ( !fatal ) { err = snd_pcm_hw_params_set_rate_resample( handle, hwparams, 1 ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_access( handle, hwparams, access ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_access: err = %1").arg(err); } } if ( !fatal ) { err = setFormat(); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_format: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channels() ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_channels: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &freakuency, 0 ); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err); } } if ( !fatal ) { err = snd_pcm_hw_params(handle, hwparams); if ( err < 0 ) { fatal = true; errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params: err = %1").arg(err); } } if( err < 0) { qWarning()<<errMessage; errorState = QAudio::OpenError; deviceState = QAudio::StoppedState; return false; } snd_pcm_hw_params_get_buffer_size(hwparams,&buffer_frames); buffer_size = snd_pcm_frames_to_bytes(handle,buffer_frames); snd_pcm_hw_params_get_period_size(hwparams,&period_frames, &dir); period_size = snd_pcm_frames_to_bytes(handle,period_frames); snd_pcm_hw_params_get_buffer_time(hwparams,&buffer_time, &dir); snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); // Step 3: Set the desired SW parameters. snd_pcm_sw_params_t *swparams; snd_pcm_sw_params_alloca(&swparams); snd_pcm_sw_params_current(handle, swparams); snd_pcm_sw_params_set_start_threshold(handle,swparams,period_frames); snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_frames); snd_pcm_sw_params_set_avail_min(handle, swparams,period_frames); snd_pcm_sw_params(handle, swparams); // Step 4: Prepare audio if(audioBuffer == 0) audioBuffer = new char[snd_pcm_frames_to_bytes(handle,buffer_frames)]; snd_pcm_prepare( handle ); snd_pcm_start(handle); // Step 5: Setup callback and timer fallback snd_async_add_pcm_handler(&ahandler, handle, async_callback, this); bytesAvailable = bytesFree(); // Step 6: Start audio processing timer->start(period_time/1000); clockStamp.restart(); timeStamp.restart(); elapsedTimeOffset = 0; errorState = QAudio::NoError; totalTimeValue = 0; opened = true; return true; }
main() { long loops; int rc; int size; snd_pcm_t *handle; snd_pcm_hw_params_t *params; unsigned int val; int dir; snd_pcm_uframes_t frames; unsigned char *buffer; int i=0; /* socket setting */ int sd; struct sockaddr_in s_addr; int n, n_send, status; int on = 1; sd = socket (AF_INET, SOCK_DGRAM, 0); bzero(&s_addr, sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_addr.s_addr = inet_addr("192.168.42.255"); s_addr.sin_port = htons(2007); if((status = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) != 0 ) { printf("setsockopt error\n"); exit(-1); } /* Open PCM device for recording (capture). */ rc = snd_pcm_open(&handle, "plughw:1,0", SND_PCM_STREAM_CAPTURE, 0); if (rc < 0) { fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc)); exit(1); } /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms); /* Fill it in with default values. */ snd_pcm_hw_params_any(handle, params); /* Set the desired hardware parameters. */ /* Interleaved mode */ snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); /* Signed 16-bit little-endian format */ snd_pcm_hw_params_set_format(handle, params,SND_PCM_FORMAT_U8); /* Two channels (stereo) */ snd_pcm_hw_params_set_channels(handle, params, 1); /* 44100 bits/second sampling rate (CD quality) */ val = 44100; snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); /* Set period size to 32 frames. */ frames =4800; snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); /* Write the parameters to the driver */ rc = snd_pcm_hw_params(handle, params); if (rc < 0) { fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc)); exit(1); } /* Use a buffer large enough to hold one period */ snd_pcm_hw_params_get_period_size(params, &frames, &dir); size = frames *1; /* 2 bytes/sample, 2 channels */ buffer = (unsigned char*) malloc(size); /* We want to loop for 5 seconds */ snd_pcm_hw_params_get_period_time(params, &val, &dir); while (1) { rc = snd_pcm_readi(handle, buffer, frames); if (rc == -EPIPE) { /* EPIPE means overrun */ fprintf(stderr, "overrun occurred\n"); snd_pcm_prepare(handle); } else if (rc < 0) { fprintf(stderr, "error from read: %s\n", snd_strerror(rc)); } else if (rc != (int)frames) { fprintf(stderr, "short read, read %d frames\n", rc); } if ((n_send = sendto(sd, buffer, strlen(buffer), 0, (struct sockaddr *)&s_addr, sizeof(s_addr))) < 0 ) { fprintf(stderr, "sendto() error"); exit(-3); } } snd_pcm_drain(handle); snd_pcm_close(handle); free(buffer); close(sd); }