1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-02 17:51:06 +01:00
libtgvoip/os/darwin/AudioOutputAudioUnit.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2017-02-02 17:24:40 +01:00
//
// 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 <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include "AudioOutputAudioUnit.h"
#include "../../logging.h"
#include "AudioUnitIO.h"
#define BUFFER_SIZE 960
using namespace tgvoip;
using namespace tgvoip::audio;
2018-05-15 20:23:46 +02:00
AudioOutputAudioUnit::AudioOutputAudioUnit(std::string deviceID, AudioUnitIO* io){
2017-02-02 17:24:40 +01:00
isPlaying=false;
remainingDataSize=0;
2018-05-15 20:23:46 +02:00
this->io=io;
#if TARGET_OS_OSX
io->SetCurrentDevice(false, deviceID);
#endif
2017-02-02 17:24:40 +01:00
}
AudioOutputAudioUnit::~AudioOutputAudioUnit(){
2017-02-02 17:24:40 +01:00
}
void AudioOutputAudioUnit::Start(){
2017-02-02 17:24:40 +01:00
isPlaying=true;
io->EnableOutput(true);
}
void AudioOutputAudioUnit::Stop(){
2017-02-02 17:24:40 +01:00
isPlaying=false;
io->EnableOutput(false);
}
bool AudioOutputAudioUnit::IsPlaying(){
2017-02-02 17:24:40 +01:00
return isPlaying;
}
void AudioOutputAudioUnit::HandleBufferCallback(AudioBufferList *ioData){
2017-02-02 17:24:40 +01:00
int i;
for(i=0;i<ioData->mNumberBuffers;i++){
AudioBuffer buf=ioData->mBuffers[i];
if(!isPlaying){
memset(buf.mData, 0, buf.mDataByteSize);
return;
}
2017-09-07 22:02:22 +02:00
#if TARGET_OS_OSX
2018-05-15 20:23:46 +02:00
unsigned int k;
2017-09-07 22:02:22 +02:00
while(remainingDataSize<buf.mDataByteSize/2){
assert(remainingDataSize+BUFFER_SIZE*2<sizeof(remainingData));
2017-02-02 17:24:40 +01:00
InvokeCallback(remainingData+remainingDataSize, BUFFER_SIZE*2);
remainingDataSize+=BUFFER_SIZE*2;
}
float* dst=reinterpret_cast<float*>(buf.mData);
int16_t* src=reinterpret_cast<int16_t*>(remainingData);
for(k=0;k<buf.mDataByteSize/4;k++){
dst[k]=src[k]/(float)INT16_MAX;
}
remainingDataSize-=buf.mDataByteSize/2;
memmove(remainingData, remainingData+buf.mDataByteSize/2, remainingDataSize);
#else
2017-09-07 22:02:22 +02:00
while(remainingDataSize<buf.mDataByteSize){
assert(remainingDataSize+BUFFER_SIZE*2<sizeof(remainingData));
InvokeCallback(remainingData+remainingDataSize, BUFFER_SIZE*2);
remainingDataSize+=BUFFER_SIZE*2;
}
2017-02-02 17:24:40 +01:00
memcpy(buf.mData, remainingData, buf.mDataByteSize);
remainingDataSize-=buf.mDataByteSize;
memmove(remainingData, remainingData+buf.mDataByteSize, remainingDataSize);
#endif
2017-02-02 17:24:40 +01:00
}
}
#if TARGET_OS_OSX
void AudioOutputAudioUnit::SetCurrentDevice(std::string deviceID){
io->SetCurrentDevice(false, deviceID);
}
#endif