int KCorrView::openALSADevice(){ int rc = 0; snd_pcm_hw_params_t *hw_params; const char *cTemp = 0; //snd_pcm_access_t access = ; cTemp = sDevice.ascii(); std::cerr << sDevice << std::endl; if ((rc = snd_pcm_open (&capture_handle, sDevice.ascii(), SND_PCM_STREAM_CAPTURE, 0)) < 0) { std::cerr << "cannot open audio device" << sDevice << snd_strerror (rc); return rc; } /* if ((rc = snd_pcm_hw_params_malloc (&hw_params)) < 0) { std::cerr << "cannot allocate hardware parameter structure" << snd_strerror (rc); return rc; } */ snd_pcm_hw_params_alloca (&hw_params); if ((rc = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) { std::cerr << "cannot initialize hardware parameter structure" << snd_strerror (rc); return rc; } if ((rc = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { // cerr << "cannot set access type" << snd_strerror (rc); return rc; } if ((rc = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { std::cerr << "cannot set sample format " << snd_strerror (rc); return rc; } if ((rc = snd_pcm_hw_params_set_channels(capture_handle, hw_params, 2)) < 0) { std::cerr << "Could not set to stereo" << snd_strerror(rc) << std::endl; return rc; } /* int exactRate = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &uSampleRate, &rc); if(rc != 0) { std::cerr << "Sample rate " << uSampleRate << "is'nt supported by your hardware. Using " << exactRate << "Hz instaed. " << std::endl; } std::cout << "snd_pcm_hw_params_set_rate_near returned with: " << exactRate << std::endl; */ unsigned int rrate = uSampleRate; if((rc = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rrate, 0)) < 0){ std::cerr << "Failed to set sample rate: "<< uSampleRate << snd_strerror (rc); } if(rrate != uSampleRate) { std::cerr << "Sample rate " << uSampleRate << "is'nt supported by your hardware. Using " << rrate << "Hz instaed. " << std::endl; } std::cout << "snd_pcm_hw_params_set_rate_near returned with: " << rc << std::endl; if ((rc = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) { std::cerr << "cannot set channel count " << snd_strerror (rc); return rc; } if ((rc = snd_pcm_hw_params (capture_handle, hw_params)) < 0) { std::cerr << "cannot set parameters " << snd_strerror (rc); return rc; } snd_pcm_hw_params_free (hw_params); if ((rc = snd_pcm_prepare (capture_handle)) < 0) { std::cerr << "cannot prepare audio interface for use " << snd_strerror (rc); return rc; } std::cout << "successfully set up ALSA 0.9 device" << std::endl; char buf[512]; long a; if ((a = snd_pcm_readi (capture_handle, buf, 128)) < 0) { std::cerr << "read from audio interface failed: " << snd_strerror (a) << std::endl; return -1; } /* for(int i=0; i <128; i++){ std::cout << buf[i]; } std::cout << " " << std::endl; */ return rc; }