static ALCdevice* alcopencapture(void *handle) { if(!handle) { return NULL; } if(handle == (void*)1) { return handle; } return alcCaptureOpenDevice(handle, UTOX_DEFAULT_SAMPLE_RATE_A, AL_FORMAT_MONO16, (UTOX_DEFAULT_FRAME_A * UTOX_DEFAULT_SAMPLE_RATE_A * 4) / 1000); // return alcCaptureOpenDevice(handle, av_DefaultSettings.audio_sample_rate, AL_FORMAT_STERIO16, ((av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000) * av_DefaultSettings.audio_channels); }
static void find_capture_device(OpenALInformation* info) { const char *user_device = os_config_read_string( "Sound", "CaptureDevice", NULL ); const char *default_device = info->default_capture_device.c_str(); // in case they are the same, we only want to test it once if ( (user_device && default_device) && !strcmp(user_device, default_device) ) { user_device = NULL; } for (auto& device : info->capture_devices) { OALdevice new_device(device.c_str()); if (user_device && !strcmp(device.c_str(), user_device)) { new_device.type = OAL_DEVICE_USER; } else if (default_device && !strcmp(device.c_str(), default_device)) { new_device.type = OAL_DEVICE_DEFAULT; } CaptureDevices.push_back( new_device ); } if ( CaptureDevices.empty() ) { return; } std::sort( CaptureDevices.begin(), CaptureDevices.end(), openal_device_sort_func ); // for each device that we have available, try and figure out which to use for (size_t idx = 0; idx < CaptureDevices.size(); idx++) { const ALCchar *device_name = CaptureDevices[idx].device_name.c_str(); ALCdevice *device = alcCaptureOpenDevice(device_name, 22050, AL_FORMAT_MONO8, 22050 * 2); if (device == NULL) { continue; } if (alcGetError(device) != ALC_NO_ERROR) { alcCaptureCloseDevice(device); continue; } // ok, we should be good with this one Capture_device = CaptureDevices[idx].device_name; alcCaptureCloseDevice(device); break; } }
void Audio::openInput(const QString& inDevDescr) { QMutexLocker lock(audioInLock); auto* tmp = alInDev; alInDev = nullptr; if (tmp) alcCaptureCloseDevice(tmp); int stereoFlag = av_DefaultSettings.audio_channels==1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; if (inDevDescr.isEmpty()) alInDev = alcCaptureOpenDevice(nullptr,av_DefaultSettings.audio_sample_rate, stereoFlag, (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000 * av_DefaultSettings.audio_channels); else alInDev = alcCaptureOpenDevice(inDevDescr.toStdString().c_str(),av_DefaultSettings.audio_sample_rate, stereoFlag, (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000 * av_DefaultSettings.audio_channels); if (!alInDev) qWarning() << "Cannot open input audio device " + inDevDescr; else qDebug() << "Opening audio input "<<inDevDescr; Core* core = Core::getInstance(); if (core) core->resetCallSources(); // Force to regen each group call's sources // Restart the capture if necessary if (userCount.load() != 0 && alInDev) { alcCaptureStart(alInDev); } else { #if (FIX_SND_PCM_PREPARE_BUG) alcCaptureStart(alInDev); #endif } }
bool cAudioCapture::initOpenALDevice() { cAudioMutexBasicLock lock(Mutex); if(CaptureDevice) shutdownOpenALDevice(); if(DeviceName.empty()) CaptureDevice = alcCaptureOpenDevice(NULL, Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize); else CaptureDevice = alcCaptureOpenDevice(toUTF8(DeviceName), Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize); if(CaptureDevice) { DeviceName = fromUTF8(alcGetString(CaptureDevice, ALC_CAPTURE_DEVICE_SPECIFIER)); Ready = true; checkError(); getLogger()->logDebug("AudioCapture", "OpenAL Capture Device Opened."); return true; } checkError(); return false; }
// create a capture buffer with the specified format // exit: 0 -> buffer created successfully // !0 -> error creating the buffer int dscap_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds) { ALenum al_format = AL_FORMAT_MONO8; ALsizei buf_size = freq * nseconds; if ( !dscap_inited ) { dscap_init(); } //Just in case we couldn't init for whatever reason if ( !dscap_inited ) { //-V581 return -1; } Assert( (nchannels == 1) || (nchannels == 2) ); Assert( (bits_per_sample == 8) || (bits_per_sample == 16) ); if (nchannels == 1) { if (bits_per_sample == 8) { al_format = AL_FORMAT_MONO8; } else if (bits_per_sample == 16) { al_format = AL_FORMAT_MONO16; } } else if (nchannels == 2) { if (bits_per_sample == 8) { al_format = AL_FORMAT_STEREO8; } else if (bits_per_sample == 16) { al_format = AL_FORMAT_STEREO16; } } const ALCchar *dev_name = (const ALCchar*) capture_dev_name.c_str(); ds_capture_device = alcCaptureOpenDevice(dev_name, freq, al_format, buf_size); if (ds_capture_device == NULL) { return -1; } if ( alcGetError(ds_capture_device) != ALC_NO_ERROR ) { return -1; } ALCaptureInfo.format = al_format; ALCaptureInfo.bits_per_sample = bits_per_sample; ALCaptureInfo.n_channels = nchannels; ALCaptureInfo.samples_per_second = freq; ALCaptureInfo.block_align = (nchannels * bits_per_sample) / 8; return 0; }
Core::Core(Camera* cam, QThread *coreThread) : tox(nullptr), camera(cam) { videobuf = new uint8_t[videobufsize]; videoBusyness=0; toxTimer = new QTimer(this); toxTimer->setSingleShot(true); //saveTimer = new QTimer(this); //saveTimer->start(TOX_SAVE_INTERVAL); bootstrapTimer = new QTimer(this); bootstrapTimer->start(TOX_BOOTSTRAP_INTERVAL); connect(toxTimer, &QTimer::timeout, this, &Core::process); //connect(saveTimer, &QTimer::timeout, this, &Core::saveConfiguration); //Disable save timer in favor of saving on events //connect(fileTimer, &QTimer::timeout, this, &Core::fileHeartbeat); connect(bootstrapTimer, &QTimer::timeout, this, &Core::onBootstrapTimer); connect(&Settings::getInstance(), &Settings::dhtServerListChanged, this, &Core::bootstrapDht); connect(this, SIGNAL(fileTransferFinished(ToxFile)), this, SLOT(onFileTransferFinished(ToxFile))); for (int i=0; i<TOXAV_MAX_CALLS;i++) { calls[i].sendAudioTimer = new QTimer(); calls[i].sendVideoTimer = new QTimer(); calls[i].sendAudioTimer->moveToThread(coreThread); calls[i].sendVideoTimer->moveToThread(coreThread); connect(calls[i].sendVideoTimer, &QTimer::timeout, [this,i](){sendCallVideo(i);}); } // OpenAL init alOutDev = alcOpenDevice(nullptr); if (!alOutDev) { qWarning() << "Core: Cannot open output audio device"; } else { alContext=alcCreateContext(alOutDev,nullptr); if (!alcMakeContextCurrent(alContext)) { qWarning() << "Core: Cannot create output audio context"; alcCloseDevice(alOutDev); } else alGenSources(1, &alMainSource); } alInDev = alcCaptureOpenDevice(NULL,av_DefaultSettings.audio_sample_rate, AL_FORMAT_MONO16, (av_DefaultSettings.audio_frame_duration * av_DefaultSettings.audio_sample_rate * 4) / 1000); if (!alInDev) qWarning() << "Core: Cannot open input audio device"; }
OpenALCapture::OpenALCapture() { const ALchar *devicelist = alcGetString (NULL, ALC_CAPTURE_DEVICE_SPECIFIER); if (devicelist) { std::cout << "\nDispositivos de captura de audio disponíveis:\n"; int countDevs = 0; while (*devicelist ) { std::cout << "\n" << countDevs << " " << devicelist; countDevs++; devicelist += strlen(devicelist) + 1; } } devicelist = alcGetString (NULL, ALC_CAPTURE_DEVICE_SPECIFIER); if (devicelist) { int countDevs = 0; while (*devicelist && countDevs < DEVICE) { devicelist += strlen(devicelist) + 1; countDevs++; } std::cout << "\n\nDispositivo selecionado: "; std::cout << "\n" << countDevs << " " << devicelist; std::cout << "\n" << countDevs << " " << devicelist; std::cout << "\n" << countDevs << " " << devicelist; } this->capDevice = alcCaptureOpenDevice(devicelist, SAMPLE_RATE, AL_FORMAT_STEREO16, BUFFER_SIZE); this->error(alcGetError(this->capDevice)); for (int i=0; i<BUFFER_SIZE; i++) { this->buffer[i] = 0; this->tempBuffer[i] = 0; } for (int i=0; i<SOFT_AMP_BUFFER_SIZE; i++) { this->softAmpArray[i] = 0; } soma = 0; this->startCapture(); }
color_organ_t *color_organ_init(double sample_rate, size_t capturebuf_size) { color_organ_t *c = malloc(sizeof(color_organ_t)); c->mic = alcCaptureOpenDevice(NULL, (ALCuint) sample_rate, AL_FORMAT_MONO16, (ALCsizei) capturebuf_size); if (alGetError() != AL_NO_ERROR) { return NULL; } c->capturebuf_size = capturebuf_size; c->capture_buf = malloc(capturebuf_size * sizeof(int16_t)); alcCaptureStart(c->mic); return c; }
bool SoundRecorder::start(unsigned int sampleRate) { // Check if the device can do audio capture if (!isAvailable()) { err() << "Failed to start capture: your system cannot capture audio data (call SoundRecorder::isAvailable to check it)" << std::endl; return false; } // Check that another capture is not already running if (captureDevice) { err() << "Trying to start audio capture, but another capture is already running" << std::endl; return false; } // Determine the recording format ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; // Open the capture device for capturing 16 bits samples captureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), sampleRate, format, sampleRate); if (!captureDevice) { err() << "Failed to open the audio capture device with the name: " << m_deviceName << std::endl; return false; } // Clear the array of samples m_samples.clear(); // Store the sample rate m_sampleRate = sampleRate; // Notify derived class if (onStart()) { // Start the capture alcCaptureStart(captureDevice); // Start the capture in a new thread, to avoid blocking the main thread m_isCapturing = true; m_thread.launch(); return true; } return false; }
int init_send_audio(codec_state *cs) { cs->support_send_audio = 0; const ALchar *pDeviceList = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); int i = 0; const ALchar *device_names[20]; if (pDeviceList) { printf("\nAvailable Capture Devices are:\n"); while (*pDeviceList) { device_names[i] = pDeviceList; printf("%d) %s\n", i, device_names[i]); pDeviceList += strlen(pDeviceList) + 1; ++i; } } printf("enter capture device number: \n"); char dev[2]; fgets(dev, sizeof(dev), stdin); cs->audio_capture_device = alcCaptureOpenDevice(device_names[dev[0] - 48], AUDIO_SAMPLE_RATE, AL_FORMAT_MONO16, AUDIO_FRAME_SIZE * 4); if (alcGetError(cs->audio_capture_device) != AL_NO_ERROR) { printf("could not start capture device! %d\n", alcGetError(cs->audio_capture_device)); return 0; } int err = OPUS_OK; cs->audio_bitrate = AUDIO_BITRATE; cs->audio_encoder = opus_encoder_create(AUDIO_SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP, &err); err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate)); err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10)); err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); opus_encoder_init(cs->audio_encoder, AUDIO_SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP); int nfo; err = opus_encoder_ctl(cs->audio_encoder, OPUS_GET_LOOKAHEAD(&nfo)); /* printf("Encoder lookahead delay : %d\n", nfo); */ printf("init audio encoder successful\n"); return 1; }
OpenALCapture::OpenALCapture() { const ALchar *devicelist = alcGetString (NULL, ALC_CAPTURE_DEVICE_SPECIFIER); if (devicelist) { std::cout << "\nAvailable Capture Devices are:-\n"; int countDevs = 0; while (*devicelist ) { std::cout << "\n" << countDevs << "-" << devicelist; countDevs++; devicelist += strlen(devicelist) + 1; } } devicelist = alcGetString (NULL, ALC_CAPTURE_DEVICE_SPECIFIER); if (devicelist) { int countDevs = 0; while (*devicelist && countDevs < DEVICE) { devicelist += strlen(devicelist) + 1; countDevs++; } std::cout << "\n\nSelected device: "; std::cout << "\n" << countDevs << "-" << devicelist; } this->capDevice = alcCaptureOpenDevice(devicelist, SAMPLE_RATE, AL_FORMAT_STEREO16, BUFFER_SIZE); for (int i=0; i<BUFFER_SIZE; i++) { this->buffer[i] = 0; this->tempBuffer[i] = 0; } for (int i=0; i<SOFT_AMP_BUFFER_SIZE; i++) { this->softAmpArray[i] = 0; } soma = 0; this->startCapture(); }
static gboolean gst_openal_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec) { GstOpenalSrc *osrc = GST_OPENAL_SRC (asrc); ALenum format; guint64 bufferSize; switch (spec->width) { case 8: format = AL_FORMAT_STEREO8; break; case 16: format = AL_FORMAT_STEREO16; break; default: g_assert_not_reached (); } bufferSize = spec->buffer_time * spec->rate * spec->bytes_per_sample / 1000000; GST_INFO_OBJECT (osrc, "Open device : %s", osrc->deviceName); osrc->deviceHandle = alcCaptureOpenDevice (osrc->device, spec->rate, format, bufferSize); if (!osrc->deviceHandle) { GST_ELEMENT_ERROR (osrc, RESOURCE, FAILED, ("Can't open device \"%s\"", osrc->device), ("Can't open device \"%s\"", osrc->device) ); return FALSE; } osrc->deviceName = g_strdup (alcGetString (osrc->deviceHandle, ALC_DEVICE_SPECIFIER)); osrc->bytes_per_sample = spec->bytes_per_sample; GST_INFO_OBJECT (osrc, "Start capture"); alcCaptureStart (osrc->deviceHandle); return TRUE; }
SoundController::SoundController(Controller *aController) : controller(aController) { device = alcOpenDevice(NULL); if (!device) throw std::runtime_error("Could not open sound device."); context = alcCreateContext(device, NULL); if (!context) throw std::runtime_error("Could not create sound context."); alcMakeContextCurrent(context); alListenerf(AL_GAIN, 1.0f); capturedNoiseLevel = 0; // Sound capture captureDevice = alcCaptureOpenDevice(NULL, 44000, AL_FORMAT_MONO8, 2000); if (captureDevice) alcCaptureStart(captureDevice); isRecording = true; }
AUD_NAMESPACE_BEGIN OpenALReader::OpenALReader(Specs specs, int buffersize) : m_specs(specs), m_position(0), m_device(nullptr) { if((specs.channels != CHANNELS_MONO) && (specs.channels != CHANNELS_STEREO)) specs.channels = CHANNELS_MONO; m_device = alcCaptureOpenDevice(nullptr, specs.rate, specs.channels == CHANNELS_MONO ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, buffersize * specs.channels * 2); if(!m_device) AUD_THROW(DeviceException, "The capture device couldn't be opened with OpenAL."); alcCaptureStart(m_device); }
int psm_record::start(int gap, int rate){ alGetError(); int channels = AL_MONO_SOFT; int type = AL_SHORT_SOFT; context_ = &rc_; context_->gap = gap; context_->channels = channels; context_->type = type; context_->rate = rate; int SSIZE = rate * FramesToBytes(1, channels, type) * 1/*1000ms, samples bytes*/; context_->format = GetFormat(channels, type, alIsBufferFormatSupportedSOFT); ALCdevice *device = alcCaptureOpenDevice(NULL, context_->rate, context_->format, SSIZE); if (alGetError() != AL_NO_ERROR) { return -1; } alcCaptureStart(device); context_->device = device; return 0; }
ALCdevice* InitOpenALCaptureDevice(ALCuint sample_frequency, ALCenum al_format, ALCsizei max_buffer_size) { ALCdevice* al_capture_device; if(!IsOpenALCaptureExtensionAvailable()) { return NULL; } al_capture_device = alcCaptureOpenDevice(NULL, sample_frequency, al_format, max_buffer_size); if(NULL == al_capture_device) { printf("Failed to get capture device\n"); return NULL; } // printf("Finished initializing AL capture\n"); return al_capture_device; }
//////////////////////////////////////////////////////////// /// Initialise la capture audio /// /// \param DeviceName : Nom du device de capture � ouvrir (NULL pour le device par d�faut) /// /// \return True si tout s'est bien pass�, false en cas d'erreur /// //////////////////////////////////////////////////////////// bool InitCapture(const char* DeviceName = NULL) { // On commence par v�rifier que la capture audio est support�e if (alcIsExtensionPresent(Device, "ALC_EXT_CAPTURE") == AL_FALSE) { std::cerr << "La capture audio n'est pas support�e par votre systeme" << std::endl; return false; } // Ouverture du device CaptureDevice = alcCaptureOpenDevice(DeviceName, 44100, AL_FORMAT_MONO16, 44100); if (!CaptureDevice) { std::cerr << "Impossible d'ouvrir le device de capture" << std::endl; return false; } return true; }
CaptureStreamT::CaptureStreamT(const std::string& DeviceName, ALenum Format, unsigned int SampleFrq) : m_FORMAT(Format), m_SAMPLE_FRQ(SampleFrq), m_MAX_CAPTURE_BUFFER_SAMPLES(SampleFrq*4), m_CaptureDevice(NULL), m_IsCapturing(false), m_ZeroSamples(0) { // m_MAX_CAPTURE_BUFFER_SAMPLES is the size of the ring buffer that OpenAL is to use for capturing, // given in number of samples and initialized above to keep 4 seconds worth of data. // We always try to keep this buffer about half filled in order to avoid buffer over- or underflow. m_CaptureDevice=alcCaptureOpenDevice( DeviceName.empty() ? NULL : DeviceName.c_str(), m_SAMPLE_FRQ, m_FORMAT, m_MAX_CAPTURE_BUFFER_SAMPLES); if (m_CaptureDevice==NULL) throw std::runtime_error("Error opening capture device "+DeviceName); }
ALCdevice* InitOpenALCaptureDevice(ALCuint sample_frequency, ALCenum al_format, ALCsizei max_buffer_size) { ALCdevice* al_capture_device; if(!IsOpenALCaptureExtensionAvailable()) { return NULL; } const char* default_device = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER); fprintf(stderr, "default device=%s", default_device); al_capture_device = alcCaptureOpenDevice(default_device, sample_frequency, al_format, max_buffer_size); if(NULL == al_capture_device) { printf("Failed to get capture device\n"); return NULL; } // printf("Finished initializing AL capture\n"); return al_capture_device; }
void SoundRecorder::start(unsigned int sampleRate) { // Check if the device can do audio capture if (!isAvailable()) { err() << "Failed to start capture : your system cannot capture audio data (call SoundRecorder::IsAvailable to check it)" << std::endl; return; } // Check that another capture is not already running if (captureDevice) { err() << "Trying to start audio capture, but another capture is already running" << std::endl; return; } // Open the capture device for capturing 16 bits mono samples captureDevice = alcCaptureOpenDevice(NULL, sampleRate, AL_FORMAT_MONO16, sampleRate); if (!captureDevice) { err() << "Failed to open the audio capture device" << std::endl; return; } // Clear the array of samples m_samples.clear(); // Store the sample rate m_sampleRate = sampleRate; // Notify derived class if (onStart()) { // Start the capture alcCaptureStart(captureDevice); // Start the capture in a new thread, to avoid blocking the main thread m_isCapturing = true; m_thread.launch(); } }
bool AudioPrivate::initInput(QString inDevDescr) { qDebug() << "Opening audio input" << inDevDescr; inputInitialized = false; if (inDevDescr == "none") return true; assert(!alInDev); /// TODO: Try to actually detect if our audio source is stereo int stereoFlag = AUDIO_CHANNELS == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; const uint32_t sampleRate = AUDIO_SAMPLE_RATE; const uint16_t frameDuration = AUDIO_FRAME_DURATION; const uint32_t chnls = AUDIO_CHANNELS; const ALCsizei bufSize = (frameDuration * sampleRate * 4) / 1000 * chnls; if (inDevDescr.isEmpty()) { const ALchar *pDeviceList = Audio::inDeviceNames(); if (pDeviceList) inDevDescr = QString::fromUtf8(pDeviceList, strlen(pDeviceList)); } if (!inDevDescr.isEmpty()) alInDev = alcCaptureOpenDevice(inDevDescr.toUtf8().constData(), sampleRate, stereoFlag, bufSize); // Restart the capture if necessary if (alInDev) { qDebug() << "Opened audio input" << inDevDescr; alcCaptureStart(alInDev); } else { qCritical() << "Failed to initialize audio input device:" << inDevDescr; return false; } inputInitialized = true; return true; }
SoundManager() { error = 0; read_buf_size = 44000; play_buf_size = 44000; play_buf[0] = 0; // Инициализируем OpenAL и создаем всякие штучки out_device = alcOpenDevice (0); context = alcCreateContext(out_device, 0); alcMakeContextCurrent(context); const char * szDefaultCaptureDevice = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER); /* while(*szDefaultCaptureDevice) { ALFWprintf("%s\n", pDeviceList); pDeviceList += strlen(pDeviceList) + 1; std::cout<<"Capture devices: "<< szDefaultCaptureDevice <<std::endl ; szDefaultCaptureDevice += strlen(szDefaultCaptureDevice) + 1; //next device } */ in_device = alcCaptureOpenDevice (szDefaultCaptureDevice, 44100, AL_FORMAT_STEREO16, read_buf_size); if( (error= alcGetError(in_device)) != AL_NO_ERROR) std::cout<<error<<" alcCaptureOpenDevice "<<__LINE__<<"\n"; alGenSources(1, &al_source); alGenBuffers(BUF_COUNT, al_buffers); // Заполняем первую очередь for (int i = 0; i < BUF_COUNT; ++i) { alBufferData(al_buffers[i], AL_FORMAT_MONO16, play_buf, play_buf_size * sizeof(ALshort), play_buf_size); } // Здесь начинаем запись и воспроизведение alSourceQueueBuffers(al_source, BUF_COUNT, al_buffers); alSourcePlay(al_source); alcCaptureStart(in_device); if( (error= alcGetError(in_device)) != AL_NO_ERROR) std::cout<<error<<" alcCaptureStart "<<__LINE__<<"\n"; }
bool SoundRecorder::setDevice(const std::string& name) { // Store the device name if (name.empty()) m_deviceName = getDefaultDevice(); else m_deviceName = name; if (m_isCapturing) { // Stop the capturing thread m_isCapturing = false; m_thread.wait(); // Determine the recording format ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; // Open the requested capture device for capturing 16 bits samples captureDevice = alcCaptureOpenDevice(name.c_str(), m_sampleRate, format, m_sampleRate); if (!captureDevice) { // Notify derived class onStop(); err() << "Failed to open the audio capture device with the name: " << m_deviceName << std::endl; return false; } // Start the capture alcCaptureStart(captureDevice); // Start the capture in a new thread, to avoid blocking the main thread m_isCapturing = true; m_thread.launch(); } return true; }
//////////////////////////////////////////////////////////// /// Start the capture. /// Warning : only one capture can happen at the same time //////////////////////////////////////////////////////////// void SoundRecorder::Start(unsigned int SampleRate) { // Check if the device can do audio capture if (!CanCapture()) { std::cerr << "Failed to start capture : your system cannot capture audio data (call sfSoundRecorder::CanCapture to check it)" << std::endl; return; } // Check that another capture is not already running if (CaptureDevice) { std::cerr << "Trying to start audio capture, but another capture is already running" << std::endl; return; } // Open the capture device for capturing 16 bits mono samples CaptureDevice = alcCaptureOpenDevice(NULL, SampleRate, AL_FORMAT_MONO16, SampleRate); if (!CaptureDevice) { std::cerr << "Failed to open the audio capture device" << std::endl; return; } // Clear the sample array mySamples.clear(); // Store the sample rate mySampleRate = SampleRate; // Start the capture alcCaptureStart(CaptureDevice); // Start the capture in a new thread, to avoid blocking the main thread myIsCapturing = true; Launch(); }
// static void SdSoundCaptureAL::StartCapture( BtBool isToFile, BtBool isFFT ) { // http://stackoverflow.com/questions/3056113/recording-audio-with-openal // http://stackoverflow.com/questions/9785447/simultaneous-record-playing-with-openal-using-threads pCaptureDevice = alcCaptureOpenDevice( NULL, 22050, AL_FORMAT_MONO16, FFTBufferSize); if( !pCaptureDevice ) { ErrorLog::Fatal_Printf( "Could not find EXT capture\r\n" ); return; } ALCdevice *pDevice; ALCcontext *pContext; ALint iDataSize = 0; // Check for Capture Extension support pContext = alcGetCurrentContext(); pDevice = alcGetContextsDevice(pContext); if (alcIsExtensionPresent(pDevice, "ALC_EXT_CAPTURE") == AL_FALSE) { ErrorLog::Fatal_Printf( "Could not find EXT capture\r\n" ); return; } // Create / open a file for the captured data audioFile = fopen( "C:\\temp\\sample.wav", "wb"); if ( audioFile == NULL ) { //ErrorLog::Fatal_Printf( "Could not find EXT capture\r\n" ); //return; } audioFile = NULL; iDataSize = 0; // Prepare a WAVE file header for the captured data sprintf(sWaveHeader.szRIFF, "RIFF"); sWaveHeader.lRIFFSize = 0; sprintf(sWaveHeader.szWave, "WAVE"); sprintf(sWaveHeader.szFmt, "fmt "); sWaveHeader.lFmtSize = sizeof(WAVEFORMATEX); sWaveHeader.wfex.nChannels = 1; sWaveHeader.wfex.wBitsPerSample = 16; sWaveHeader.wfex.wFormatTag = 1; //WAVE_FORMAT_PCM; sWaveHeader.wfex.nSamplesPerSec = 22050; sWaveHeader.wfex.nBlockAlign = sWaveHeader.wfex.nChannels * sWaveHeader.wfex.wBitsPerSample / 8; sWaveHeader.wfex.nAvgBytesPerSec = sWaveHeader.wfex.nSamplesPerSec * sWaveHeader.wfex.nBlockAlign; sWaveHeader.wfex.cbSize = 0; sprintf(sWaveHeader.szData, "data"); sWaveHeader.lDataSize = 0; if( audioFile ) { fwrite(&sWaveHeader, sizeof(WAVEHEADER), 1, audioFile); } // Start audio capture alcCaptureStart(pCaptureDevice); m_isFFT= isFFT; init_fft( FFTBufferSize, 22050); }
av_session_t *av_init_session() { av_session_t *_retu = malloc(sizeof(av_session_t)); /* Initialize our mutex */ pthread_mutex_init ( &_retu->_mutex, NULL ); _retu->_messenger = tox_new(1); if ( !_retu->_messenger ) { fprintf ( stderr, "tox_new() failed!\n" ); return NULL; } _retu->_friends = NULL; const ALchar *_device_list = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); int i = 0; const ALchar *device_names[20]; if ( _device_list ) { INFO("\nAvailable Capture Devices are:"); while (*_device_list ) { device_names[i] = _device_list; INFO("%d) %s", i, device_names[i]); _device_list += strlen( _device_list ) + 1; ++i; } } INFO("Enter capture device number"); char dev[2]; char *left; char *warned_ = fgets(dev, 2, stdin); (void)warned_; long selection = strtol(dev, &left, 10); if ( *left ) { printf("'%s' is not a number!", dev); fflush(stdout); exit(EXIT_FAILURE); } else { INFO("Selected: %d ( %s )", selection, device_names[selection]); } _retu->audio_capture_device = (struct ALCdevice *)alcCaptureOpenDevice( device_names[selection], AUDIO_SAMPLE_RATE, AL_FORMAT_MONO16, AUDIO_FRAME_SIZE * 4); if (alcGetError((ALCdevice *)_retu->audio_capture_device) != AL_NO_ERROR) { printf("Could not start capture device! %d\n", alcGetError((ALCdevice *)_retu->audio_capture_device)); return 0; } uint16_t height = 0, width = 0; #ifdef TOX_FFMPEG avdevice_register_all(); avcodec_register_all(); av_register_all(); _retu->video_input_format = av_find_input_format(VIDEO_DRIVER); if (avformat_open_input(&_retu->video_format_ctx, DEFAULT_WEBCAM, _retu->video_input_format, NULL) != 0) { fprintf(stderr, "Opening video_input_format failed!\n"); //return -1; goto failed_init_ffmpeg; } avformat_find_stream_info(_retu->video_format_ctx, NULL); av_dump_format(_retu->video_format_ctx, 0, DEFAULT_WEBCAM, 0); for (i = 0; i < _retu->video_format_ctx->nb_streams; ++i) { if (_retu->video_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { _retu->video_stream = i; break; } } _retu->webcam_decoder_ctx = _retu->video_format_ctx->streams[_retu->video_stream]->codec; _retu->webcam_decoder = avcodec_find_decoder(_retu->webcam_decoder_ctx->codec_id); if (_retu->webcam_decoder == NULL) { fprintf(stderr, "Unsupported codec!\n"); //return -1; goto failed_init_ffmpeg; } if (_retu->webcam_decoder_ctx == NULL) { fprintf(stderr, "Init webcam_decoder_ctx failed!\n"); //return -1; goto failed_init_ffmpeg; } if (avcodec_open2(_retu->webcam_decoder_ctx, _retu->webcam_decoder, NULL) < 0) { fprintf(stderr, "Opening webcam decoder failed!\n"); //return -1; goto failed_init_ffmpeg; } width = _retu->webcam_decoder_ctx->width; height = _retu->webcam_decoder_ctx->height; failed_init_ffmpeg: ; #endif uint8_t _byte_address[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(_retu->_messenger, _byte_address ); fraddr_to_str( _byte_address, _retu->_my_public_id ); _retu->av = toxav_new(_retu->_messenger, width, height); /* ------------------ */ toxav_register_callstate_callback(callback_call_started, av_OnStart, _retu->av); toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, _retu->av); toxav_register_callstate_callback(callback_call_rejected, av_OnReject, _retu->av); toxav_register_callstate_callback(callback_call_ended, av_OnEnd, _retu->av); toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, _retu->av); toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, _retu->av); toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, _retu->av); toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, _retu->av); toxav_register_callstate_callback(callback_recv_error, av_OnError, _retu->av); toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, _retu->av); /* ------------------ */ return _retu; }
/* OpenAL ALC 1.1 functions */ ALCdevice* CDECL wine_alcCaptureOpenDevice(const ALCchar *devicename, ALCuint frequency, ALCenum format, ALCsizei buffersize) { return alcCaptureOpenDevice(devicename, frequency, format, buffersize); }
int main(int argc, char** argv) { /* init ps */ fprintf(stderr, "INIT PS\n"); cmd_ln_t* psconfig = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/en-us/en-us", "-lm", MODELDIR "/en-us/en-us.lm.bin", "-dict", MODELDIR "/en-us/cmudict-en-us.dict", NULL); if (psconfig == NULL) { fprintf(stderr, "Cannot create config for PS\n"); return -1; } ps_decoder_t* ps = ps_init(psconfig); if (ps == NULL) { fprintf(stderr, "Cannot create PS decoder\n"); return -1; } fprintf(stderr, "INIT AL\n"); /* init openal and create microphone */ ALCdevice* aldevice = alcCaptureOpenDevice(NULL,AL_MIC_FREQ,AL_FORMAT_MONO16,AL_MIC_FREQ/2); if (aldevice == NULL) { fprintf(stderr, "Cannot open AL device"); return -1; } alcCaptureStart(aldevice); /* conna capture some wods */ ALCint samplesIn = 0; short buff[AL_MIC_FREQ*2]; int ignoreCounter = 20; uint8 utt_started; uint8 in_speech; utt_started = FALSE; ps_start_utt(ps); const char* hyp; for (;;) { /* poll some data */ alcGetIntegerv(aldevice,ALC_CAPTURE_SAMPLES,1,&samplesIn); if(samplesIn>AL_MIC_CAP) { alcCaptureSamples(aldevice,buff,AL_MIC_CAP); /* actual voice processing */ ps_process_raw(ps, buff, AL_MIC_CAP, FALSE, FALSE); in_speech = ps_get_in_speech(ps); if (in_speech && !utt_started) { utt_started = TRUE; fprintf(stderr, "Hearing something i guess...\n"); } if (!in_speech && utt_started) { fprintf(stderr, "Processing it now...\n"); ps_end_utt(ps); hyp = ps_get_hyp(ps,NULL); if (hyp != NULL) { fprintf(stderr, "Here we go!\n\n"); fprintf(stdout,"%s\n",hyp); fflush(stdout); fprintf(stderr,"\n\nTo the next round!\n"); } ps_start_utt(ps); utt_started = FALSE; } } } return 0; }
int main(void) { /*Отсюда нужно две функции - 1) получение в виде массива (строки) данных с микрофона за время необработки 2)Проигрвание строки равной значению */ const ALCchar * devices; const ALCchar * ptr; ALCdevice * mainDev; ALCcontext * mainContext; ALCdevice * captureDev; ALubyte captureBuffer[1048576]; ALubyte *captureBufPtr; ALint samplesAvailable; ALint samplesCaptured; time_t currentTime; time_t lastTime; ALuint buffer; ALuint source; ALint playState; int i; // Print the list of capture devices printf("Available playback devices:\n"); devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER); ptr = devices; //while (ptr[0] != NULL) while (*ptr) { printf(" %s\n", ptr); ptr += strlen(ptr) + 1; } // Open a playback device and create a context first printf("Opening playback device:\n"); mainDev = alcOpenDevice(NULL); if (mainDev == NULL) { printf("Unable to open playback device!\n"); exit(1); } devices = alcGetString(mainDev, ALC_DEVICE_SPECIFIER); printf(" opened device '%s'\n", devices); mainContext = alcCreateContext(mainDev, NULL); if (mainContext == NULL) { printf("Unable to create playback context!\n"); exit(1); } printf(" created playback context\n"); // Make the playback context current alcMakeContextCurrent(mainContext); alcProcessContext(mainContext); // Print the list of capture devices printf("Available capture devices:\n"); devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); ptr = devices; //while (ptr[0] != NULL) while (*ptr) { printf(" %s\n", ptr); ptr += strlen(ptr) + 1; } // Open the default device printf("Opening capture device:\n"); captureDev = alcCaptureOpenDevice(NULL, 8000, AL_FORMAT_MONO16, 800); if (captureDev == NULL) { printf(" Unable to open device!\n"); exit(1); } devices = alcGetString(captureDev, ALC_CAPTURE_DEVICE_SPECIFIER); printf(" opened device %s\n", devices); // Wait for three seconds to prompt the user for (i = 3; i > 0; i--) { printf("Starting capture in %d...\r", i); fflush(stdout); lastTime = time(NULL); currentTime = lastTime; while (currentTime == lastTime) { currentTime = time(NULL); usleep(100000); } } printf("Starting capture NOW!\n"); fflush(stdout); lastTime = currentTime; // Capture (roughly) five seconds of audio alcCaptureStart(captureDev); samplesCaptured = 0; captureBufPtr = captureBuffer; while (currentTime < (lastTime + 5)) { // Get the number of samples available alcGetIntegerv(captureDev, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable); // Copy the samples to our capture buffer if (samplesAvailable > 0) { alcCaptureSamples(captureDev, captureBufPtr, samplesAvailable); samplesCaptured += samplesAvailable; printf("Captured %d samples (adding %d)\r", samplesCaptured, samplesAvailable); fflush(stdout); // Advance the buffer (two bytes per sample * number of samples) captureBufPtr += samplesAvailable * 2; } // Wait for a bit usleep(10000); // Update the clock currentTime = time(NULL); } printf("\nPausing capture.\n"); alcCaptureStop(captureDev); // Wait for three seconds to prompt the user for (i = 3; i > 0; i--) { printf("Resuming capture in %d...\r", i); fflush(stdout); lastTime = time(NULL); currentTime = lastTime; while (currentTime == lastTime) { currentTime = time(NULL); usleep(100000); } } printf("Resuming capture NOW!\n"); fflush(stdout); lastTime = currentTime; // Capture (roughly) five seconds of audio alcCaptureStart(captureDev); while (currentTime < (lastTime + 5)) { // Get the number of samples available alcGetIntegerv(captureDev, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable); // Copy the samples to our capture buffer if (samplesAvailable > 0) { alcCaptureSamples(captureDev, captureBufPtr, samplesAvailable); samplesCaptured += samplesAvailable; printf("Captured %d samples (adding %d)\r", samplesCaptured, samplesAvailable); fflush(stdout); // Advance the buffer (two bytes per sample * number of samples) captureBufPtr += samplesAvailable * 2; } // Wait for a bit usleep(10000); // Update the clock currentTime = time(NULL); } printf("\nDone capturing.\n"); alcCaptureStop(captureDev); // Play back the captured data printf("Starting playback...\n"); fflush(stdout); // Generate an OpenAL buffer for the captured data alGenBuffers(1, &buffer); alGenSources(1, &source); alBufferData(buffer, AL_FORMAT_MONO16, captureBuffer,samplesCaptured*2, 8000); alSourcei(source, AL_BUFFER, buffer); alSourcePlay(source); // Wait for the source to stop playing playState = AL_PLAYING; while (playState == AL_PLAYING) { printf(" source %d is playing...\r", source); fflush(stdout); alGetSourcei(source, AL_SOURCE_STATE, &playState); usleep(100000); } printf("\nDone with playback.\n"); fflush(stdout); // Shut down OpenAL alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); alcMakeContextCurrent(NULL); alcCloseDevice(mainDev); alcCaptureCloseDevice(captureDev); }
// TODO: generate buffers separately DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels) { if (size[type] <= selection || selection < 0) return de_InvalidSelection; if (channels != 1 && channels != 2) return de_UnsupportedMode; lock; const uint32_t frame_size = (sample_rate * frame_duration / 1000); uint32_t i; for (i = 0; i < MAX_DEVICES && running[type][i] != NULL; i ++); if (i == MAX_DEVICES) { unlock; return de_AllDevicesBusy; } else *device_idx = i; Device* device = running[type][*device_idx] = calloc(1, sizeof(Device)); device->selection = selection; device->sample_rate = sample_rate; device->frame_duration = frame_duration; device->sound_mode = channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; for (i = 0; i < *device_idx; i ++) { /* Check if any previous has the same selection */ if ( running[type][i]->selection == selection ) { device->dhndl = running[type][i]->dhndl; if (type == output) { device->ctx = running[type][i]->ctx; memcpy(device->buffers, running[type][i]->buffers, sizeof(running[type][i]->buffers)); device->source = running[type][i]->source; } device->ref_count++; pthread_mutex_init(device->mutex, NULL); unlock; return de_None; } } if (type == input) { device->dhndl = alcCaptureOpenDevice(devices_names[type][selection], sample_rate, device->sound_mode, frame_size * 2); #ifdef _AUDIO device->VAD_treshold = user_settings_->VAD_treshold; #endif } else { device->dhndl = alcOpenDevice(devices_names[type][selection]); if ( !device->dhndl ) { free(device); running[type][*device_idx] = NULL; unlock; return de_FailedStart; } device->ctx = alcCreateContext(device->dhndl, NULL); alcMakeContextCurrent(device->ctx); alGenBuffers(OPENAL_BUFS, device->buffers); alGenSources((uint32_t)1, &device->source); alSourcei(device->source, AL_LOOPING, AL_FALSE); uint16_t zeros[frame_size]; memset(zeros, 0, frame_size*2); for ( i =0; i < OPENAL_BUFS; ++i) { alBufferData(device->buffers[i], device->sound_mode, zeros, frame_size*2, sample_rate); } alSourceQueueBuffers(device->source, OPENAL_BUFS, device->buffers); alSourcePlay(device->source); } if (alcGetError(device->dhndl) != AL_NO_ERROR) { free(device); running[type][*device_idx] = NULL; unlock; return de_FailedStart; } if (type == input) { alcCaptureStart(device->dhndl); thread_paused = _False; } pthread_mutex_init(device->mutex, NULL); unlock; return de_None; }