1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-30 04:39:03 +01:00
libtgvoip/os/windows/AudioInputWave.cpp
Grishka bfde1a4be3 Update to v0.4
Moved public API classes into namespace tgvoip (CVoIPController -> tgvoip::VoIPController, CVoIPServerConfig -> tgvoip::ServerConfig)
Endpoint is now a class instead of a struct; also, IP addresses are now wrapped into objects instead of relying on in_addr and in6_addr
Full Windows port (Win32 threading + Winsock + WaveOut/WaveIn)
Added support for ALSA audio I/O on Linux (closes #2)
Abstracted away low-level networking to make it more portable
Minor bugfixes
2017-04-17 21:57:07 +03:00

91 lines
2.5 KiB
C++

//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "AudioInputWave.h"
#include "../../logging.h"
using namespace tgvoip::audio;
#define BUFFER_SIZE 960
#define CHECK_ERROR(res, msg) if(res!=MMSYSERR_NOERROR){wchar_t _buf[1024]; waveInGetErrorTextW(res, _buf, 1024); LOGE(msg ": %ws (MMRESULT=0x08X)", _buf, res); failed=true;}
AudioInputWave::AudioInputWave(){
isRecording=false;
for(int i=0;i<4;i++){
ZeroMemory(&buffers[i], sizeof(WAVEHDR));
buffers[i].dwBufferLength=960*2;
buffers[i].lpData=(char*)malloc(960*2);
}
ZeroMemory(&format, sizeof(format));
format.cbSize=0;
format.wFormatTag=WAVE_FORMAT_PCM;
format.nSamplesPerSec=48000;
format.wBitsPerSample=16;
format.nChannels=1;
format.nBlockAlign=2;
MMRESULT res=waveInOpen(&hWaveIn, WAVE_MAPPER, &format, (DWORD_PTR)AudioInputWave::WaveInProc, (DWORD_PTR)this, CALLBACK_FUNCTION);
CHECK_ERROR(res, "waveInOpen failed");
}
AudioInputWave::~AudioInputWave(){
for(int i=0;i<4;i++){
free(buffers[i].lpData);
}
waveInClose(hWaveIn);
}
void AudioInputWave::Configure(uint32_t sampleRate, uint32_t bitsPerSample, uint32_t channels){
}
void AudioInputWave::Start(){
isRecording=true;
MMRESULT res;
for(int i=0;i<4;i++){
res=waveInPrepareHeader(hWaveIn, &buffers[i], sizeof(WAVEHDR));
CHECK_ERROR(res, "waveInPrepareHeader failed");
res=waveInAddBuffer(hWaveIn, &buffers[i], sizeof(WAVEHDR));
CHECK_ERROR(res, "waveInAddBuffer failed");
}
res=waveInStart(hWaveIn);
CHECK_ERROR(res, "waveInStart failed");
}
void AudioInputWave::Stop(){
isRecording=false;
MMRESULT res=waveInStop(hWaveIn);
CHECK_ERROR(res, "waveInStop failed");
res=waveInReset(hWaveIn);
CHECK_ERROR(res, "waveInReset failed");
for(int i=0;i<4;i++){
res=waveInUnprepareHeader(hWaveIn, &buffers[i], sizeof(WAVEHDR));
CHECK_ERROR(res, "waveInUnprepareHeader failed");
}
}
void AudioInputWave::WaveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2){
if(uMsg==WIM_DATA){
((AudioInputWave*)dwInstance)->OnData((WAVEHDR*)dwParam1);
}
}
void AudioInputWave::OnData(WAVEHDR* hdr){
if(!isRecording)
return;
InvokeCallback((unsigned char*)hdr->lpData, hdr->dwBufferLength);
hdr->dwFlags&= ~WHDR_DONE;
MMRESULT res=waveInAddBuffer(hWaveIn, hdr, sizeof(WAVEHDR));
CHECK_ERROR(res, "waveInAddBuffer failed");
}