Commit 98d6b80b authored by Alexandre Lision's avatar Alexandre Lision

Revert "coreaudio: modern C++"

This reverts commit f3d677fb.

Change-Id: Iebfbb5554c1d910c7ea0d919b13669e760d512ca
parent e4e67249
......@@ -27,7 +27,8 @@
#include "audiodevice.h"
#include <cmath>
#include <vector>
#include <thread>
#include <atomic>
namespace ring {
......@@ -38,52 +39,55 @@ CoreLayer::CoreLayer(const AudioPreference &pref)
, indexOut_(pref.getAlsaCardout())
, indexRing_(pref.getAlsaCardring())
, playbackBuff_(0, audioFormat_)
, captureBuff_(0)
, mainRingBuffer_(Manager::instance().getRingBufferPool().getRingBuffer(RingBufferPool::DEFAULT_ID))
{}
CoreLayer::~CoreLayer()
{}
{
if (captureBuff_) {
for (UInt32 i = 0; i < captureBuff_->mNumberBuffers; ++i)
free(captureBuff_->mBuffers[i].mData);
free(captureBuff_);
captureBuff_ = 0;
}
}
std::vector<std::string>
CoreLayer::getCaptureDeviceList() const
std::vector<std::string> CoreLayer::getCaptureDeviceList() const
{
std::vector<std::string> ret;
#if !TARGET_OS_IPHONE
for (const auto& x : getDeviceList(true))
for (auto x : getDeviceList(true))
ret.push_back(x.name_);
#endif
return ret;
}
std::vector<std::string>
CoreLayer::getPlaybackDeviceList() const
std::vector<std::string> CoreLayer::getPlaybackDeviceList() const
{
std::vector<std::string> ret;
#if !TARGET_OS_IPHONE
for (const auto& x : getDeviceList(false))
for (auto x : getDeviceList(false))
ret.push_back(x.name_);
#endif
return ret;
}
int
CoreLayer::getAudioDeviceIndex(const std::string& name, DeviceType type) const
int CoreLayer::getAudioDeviceIndex(const std::string& name, DeviceType type) const
{
return 0;
}
std::string
CoreLayer::getAudioDeviceName(int index, DeviceType type) const
std::string CoreLayer::getAudioDeviceName(int index, DeviceType type) const
{
return "";
}
void
CoreLayer::initAudioLayerIO()
void CoreLayer::initAudioLayerIO()
{
// OS X uses Audio Units for output. Steps:
// 1) Create a description.
......@@ -103,8 +107,8 @@ CoreLayer::initAudioLayerIO()
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
auto comp = AudioComponentFindNext(nullptr, &desc);
if (comp == nullptr) {
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
if (comp == NULL) {
RING_ERR("Can't find default output audio component.");
return;
}
......@@ -121,8 +125,7 @@ CoreLayer::initAudioLayerIO()
&info,
&size));
audioFormat_ = {static_cast<unsigned int>(info.mSampleRate),
static_cast<unsigned int>(info.mChannelsPerFrame)};
audioFormat_ = {(unsigned int)info.mSampleRate, (unsigned int)info.mChannelsPerFrame};
checkErr(AudioUnitGetProperty(ioUnit_,
kAudioUnitProperty_StreamFormat,
......@@ -151,8 +154,7 @@ CoreLayer::initAudioLayerIO()
&info,
&size));
audioInputFormat_ = {static_cast<unsigned int>(info.mSampleRate),
static_cast<unsigned int>(info.mChannelsPerFrame)};
audioInputFormat_ = {(unsigned int)info.mSampleRate, (unsigned int)info.mChannelsPerFrame};
hardwareInputFormatAvailable(audioInputFormat_);
// Set format on output *SCOPE* in input *BUS*.
......@@ -194,15 +196,15 @@ CoreLayer::initAudioLayerIO()
#endif
UInt32 bufferSizeBytes = bufferSizeFrames * sizeof(Float32);
size = offsetof(AudioBufferList, mBuffers) + (sizeof(AudioBuffer) * info.mChannelsPerFrame);
rawBuff_.reset(new Byte[size + bufferSizeBytes * info.mChannelsPerFrame]);
captureBuff_ = reinterpret_cast<::AudioBufferList*>(rawBuff_.get());
size = offsetof(AudioBufferList, mBuffers[0]) +
(sizeof(AudioBuffer) * info.mChannelsPerFrame);
captureBuff_ = (AudioBufferList *)malloc(size);
captureBuff_->mNumberBuffers = info.mChannelsPerFrame;
for (UInt32 i = 0; i < captureBuff_->mNumberBuffers; ++i) {
captureBuff_->mBuffers[i].mNumberChannels = 1;
captureBuff_->mBuffers[i].mDataByteSize = bufferSizeBytes;
captureBuff_->mBuffers[i].mData = rawBuff_.get() + bufferSizeBytes * i;
captureBuff_->mBuffers[i].mData = malloc(bufferSizeBytes);
}
// Input callback setup.
......@@ -230,8 +232,7 @@ CoreLayer::initAudioLayerIO()
sizeof(AURenderCallbackStruct)));
}
void
CoreLayer::startStream()
void CoreLayer::startStream()
{
RING_DBG("START STREAM");
......@@ -251,16 +252,14 @@ CoreLayer::startStream()
checkErr(AudioOutputUnitStart(ioUnit_));
}
void
CoreLayer::destroyAudioLayer()
void CoreLayer::destroyAudioLayer()
{
AudioOutputUnitStop(ioUnit_);
AudioUnitUninitialize(ioUnit_);
AudioComponentInstanceDispose(ioUnit_);
}
void
CoreLayer::stopStream()
void CoreLayer::stopStream()
{
RING_DBG("STOP STREAM");
......@@ -280,8 +279,7 @@ CoreLayer::stopStream()
//// PRIVATE /////
OSStatus
CoreLayer::outputCallback(void* inRefCon,
OSStatus CoreLayer::outputCallback(void* inRefCon,
AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
......@@ -292,8 +290,7 @@ CoreLayer::outputCallback(void* inRefCon,
return kAudioServicesNoError;
}
void
CoreLayer::write(AudioUnitRenderActionFlags* ioActionFlags,
void CoreLayer::write(AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
......@@ -306,29 +303,26 @@ CoreLayer::write(AudioUnitRenderActionFlags* ioActionFlags,
if (toPlay.frames() == 0) {
for (int i = 0; i < audioFormat_.nb_channels; ++i)
std::fill_n(reinterpret_cast<Float32*>(ioData->mBuffers[i].mData),
ioData->mBuffers[i].mDataByteSize, 0);
} else {
memset((Float32*)ioData->mBuffers[i].mData, 0, ioData->mBuffers[i].mDataByteSize);
}
else {
for (int i = 0; i < audioFormat_.nb_channels; ++i)
toPlay.channelToFloat(reinterpret_cast<Float32*>(ioData->mBuffers[i].mData), i);
toPlay.channelToFloat((Float32*)ioData->mBuffers[i].mData, i);
}
}
OSStatus
CoreLayer::inputCallback(void* inRefCon,
OSStatus CoreLayer::inputCallback(void* inRefCon,
AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList* ioData)
{
static_cast<CoreLayer*>(inRefCon)->read(ioActionFlags, inTimeStamp, inBusNumber,
inNumberFrames, ioData);
static_cast<CoreLayer*>(inRefCon)->read(ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
return kAudioServicesNoError;
}
void
CoreLayer::read(AudioUnitRenderActionFlags* ioActionFlags,
void CoreLayer::read(AudioUnitRenderActionFlags* ioActionFlags,
const AudioTimeStamp* inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
......@@ -361,12 +355,12 @@ CoreLayer::read(AudioUnitRenderActionFlags* ioActionFlags,
bool resample = info.mSampleRate != mainBufferFormat.sample_rate;
// FIXME: Performance! There *must* be a better way. This is testing only.
auto inBuff = AudioBuffer {inNumberFrames, audioInputFormat_};
AudioBuffer inBuff(inNumberFrames, audioInputFormat_);
for (int i = 0; i < info.mChannelsPerFrame; ++i) {
auto data = reinterpret_cast<Float32*>(captureBuff_->mBuffers[i].mData);
Float32* data = (Float32*)captureBuff_->mBuffers[i].mData;
for (int j = 0; j < inNumberFrames; ++j) {
(*inBuff.getChannel(i))[j] = static_cast<AudioSample>(data[j] / .000030517578125f);
(*inBuff.getChannel(i))[j] = (AudioSample)((data)[j] / .000030517578125f);
}
}
......@@ -375,8 +369,8 @@ CoreLayer::read(AudioUnitRenderActionFlags* ioActionFlags,
//FIXME: May be a multiplication, check alsa vs pulse implementation.
UInt32 outSamples = inNumberFrames / (static_cast<double>(audioInputFormat_.sample_rate) / mainBufferFormat.sample_rate);
auto out = AudioBuffer {outSamples, mainBufferFormat};
int outSamples = inNumberFrames / (static_cast<double>(audioInputFormat_.sample_rate) / mainBufferFormat.sample_rate);
AudioBuffer out(outSamples, mainBufferFormat);
inputResampler_->resample(inBuff, out);
dcblocker_.process(out);
mainRingBuffer_->put(out);
......@@ -406,8 +400,7 @@ void CoreLayer::updatePreference(AudioPreference &preference, int index, DeviceT
}
}
std::vector<AudioDevice>
CoreLayer::getDeviceList(bool getCapture) const
std::vector<AudioDevice> CoreLayer::getDeviceList(bool getCapture) const
{
std::vector<AudioDevice> ret;
#if !TARGET_OS_IPHONE
......@@ -416,31 +409,31 @@ CoreLayer::getDeviceList(bool getCapture) const
AudioObjectPropertyAddress theAddress = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
kAudioObjectPropertyElementMaster };
verify_noerr(AudioObjectGetPropertyDataSize(kAudioObjectSystemObject,
&theAddress,
0,
nullptr,
&propsize));
&theAddress,
0,
NULL,
&propsize));
std::size_t nDevices = propsize / sizeof(AudioDeviceID);
auto devids = std::vector<AudioDeviceID>(nDevices);
size_t nDevices = propsize / sizeof(AudioDeviceID);
AudioDeviceID *devids = new AudioDeviceID[nDevices];
verify_noerr(AudioObjectGetPropertyData(kAudioObjectSystemObject,
&theAddress,
0,
nullptr,
&propsize,
devids.data()));
&theAddress,
0,
NULL,
&propsize,
devids));
for (int i = 0; i < nDevices; ++i) {
auto dev = AudioDevice {devids[i], getCapture};
AudioDevice dev(devids[i], getCapture);
if (dev.channels_ > 0) { // Channels < 0 if inactive.
ret.push_back(std::move(dev));
ret.push_back(dev);
}
}
delete[] devids;
#endif
return ret;
}
......
......@@ -162,8 +162,7 @@ class CoreLayer : public AudioLayer {
/** Non-interleaved audio buffers */
AudioBuffer playbackBuff_;
::AudioBufferList* captureBuff_ {nullptr}; // CoreAudio buffer (pointer is casted rawBuff_)
std::unique_ptr<Byte[]> rawBuff_; // raw allocation of captureBuff_
::AudioBufferList* captureBuff_; // CoreAudio buffer.
/** Interleaved buffer */
std::vector<AudioSample> playbackIBuff_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment