Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ void AndroidRotatingFileWriter::writeAudioData(AudioDataType data, int numFrames
rotateFiles();
}
}
framesWritten_.fetch_add(numFrames, std::memory_order_relaxed);
}

double AndroidRotatingFileWriter::getCurrentDuration() const {
return static_cast<double>(framesWritten_.load(std::memory_order_relaxed)) /
fileProperties_->sampleRate;
double currentSegmentDuration = 0.0;
if (currentWriter_ != nullptr) {
currentSegmentDuration = currentWriter_->getCurrentDuration();
}
return cumulativeDurationSec_ + currentSegmentDuration;
}

void AndroidRotatingFileWriter::rotateFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct objc_object AVAudioConverter;
#endif // __OBJC__

struct WriterData {
// Owned by the worker thread; freed in IOSFileWriter::taskOffloaderFunction.
const AudioBufferList *audioBufferList;
int numFrames;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#include <audioapi/events/AudioEventHandlerRegistry.h>
#include <audioapi/ios/core/utils/FileOptions.h>
#include <audioapi/ios/core/utils/IOSFileWriter.h>
#include <audioapi/ios/core/utils/OwnedAudioBufferList.h>
#include <audioapi/utils/AudioFileProperties.h>
#include <audioapi/utils/Result.hpp>
#include <audioapi/utils/UnitConversion.h>

namespace audioapi {

using ios::copyAudioBufferList;
using ios::freeOwnedAudioBufferList;

IOSFileWriter::IOSFileWriter(
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
const std::shared_ptr<AudioFileProperties> &fileProperties)
Expand Down Expand Up @@ -157,34 +161,45 @@
{
if (audioFile_ == nil) {
invokeOnErrorCallback("Attempted to write audio data when file is not open");
} else {
offloader_->getSender()->send({audioBufferList, numFrames});
return;
}

// CoreAudio owns `audioBufferList` only for the duration of this synchronous
// callback. Copy into an owned AudioBufferList before handing off to the
// worker thread; the consumer in taskOffloaderFunction frees it.
AudioBufferList *owned = copyAudioBufferList(audioBufferList);
if (owned == nullptr) {
return;
}

offloader_->getSender()->send({.audioBufferList = owned, .numFrames = numFrames});
}

void IOSFileWriter::taskOffloaderFunction(WriterData data)
{
auto [audioBufferList, numFrames] = data;
if (audioBufferList == nullptr)
if (audioBufferList == nullptr) {
return;
}
@autoreleasepool {
NSError *error = nil;

for (size_t i = 0; i < bufferFormat_.channelCount; ++i) {
memcpy(
converterInputBuffer_.mutableAudioBufferList->mBuffers[i].mData,
audioBufferList->mBuffers[i].mData,
audioBufferList->mBuffers[i].mDataByteSize);
}

freeOwnedAudioBufferList(audioBufferList);
audioBufferList = nullptr;
converterInputBuffer_.frameLength = numFrames;

AVAudioFormat *fileFormat = [audioFile_ processingFormat];

if (bufferFormat_.sampleRate == fileFormat.sampleRate &&
bufferFormat_.channelCount == fileFormat.channelCount &&
bufferFormat_.isInterleaved == fileFormat.isInterleaved) {
// We can use the converter input buffer as a "transport" layer to the file
for (size_t i = 0; i < bufferFormat_.channelCount; ++i) {
memcpy(
converterInputBuffer_.mutableAudioBufferList->mBuffers[i].mData,
audioBufferList->mBuffers[i].mData,
audioBufferList->mBuffers[i].mDataByteSize);
}

audioBufferList = nullptr;
converterInputBuffer_.frameLength = numFrames;

[audioFile_ writeFromBuffer:converterInputBuffer_ error:&error];

if (error != nil) {
Expand All @@ -198,16 +213,6 @@
return;
}

for (size_t i = 0; i < bufferFormat_.channelCount; ++i) {
memcpy(
converterInputBuffer_.mutableAudioBufferList->mBuffers[i].mData,
audioBufferList->mBuffers[i].mData,
audioBufferList->mBuffers[i].mDataByteSize);
}

audioBufferList = nullptr;
converterInputBuffer_.frameLength = numFrames;

__block BOOL handedOff = false;
AVAudioConverterInputBlock inputBlock = ^AVAudioBuffer *_Nullable(
AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus)
Expand All @@ -223,8 +228,6 @@
};

[converter_ convertToBuffer:converterOutputBuffer_ error:&error withInputFromBlock:inputBlock];
converterOutputBuffer_.frameLength =
fileProperties_->sampleRate / bufferFormat_.sampleRate * numFrames;

if (error != nil) {
invokeOnErrorCallback(
Expand All @@ -233,6 +236,11 @@
return;
}

AVAudioFrameCount producedFrames = converterOutputBuffer_.frameLength;
if (producedFrames == 0) {
return;
}

[audioFile_ writeFromBuffer:converterOutputBuffer_ error:&error];

if (error != nil) {
Expand All @@ -242,14 +250,14 @@
return;
}

framesWritten_.fetch_add(numFrames, std::memory_order_acq_rel);
framesWritten_.fetch_add(producedFrames, std::memory_order_acq_rel);
}
}

double IOSFileWriter::getCurrentDuration() const
{
return static_cast<double>(framesWritten_.load(std::memory_order_acquire)) /
bufferFormat_.sampleRate;
fileProperties_->sampleRate;
}

std::string IOSFileWriter::getFilePath() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
#include <audioapi/dsp/VectorMath.h>
#include <audioapi/events/AudioEventHandlerRegistry.h>
#include <audioapi/ios/core/utils/IOSRecorderCallback.h>
#include <audioapi/ios/core/utils/OwnedAudioBufferList.h>
#include <audioapi/utils/AudioArray.hpp>
#include <audioapi/utils/AudioBuffer.hpp>
#include <audioapi/utils/CircularArray.hpp>
#include <audioapi/utils/Result.hpp>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <utility>

namespace audioapi {

using ios::copyAudioBufferList;
using ios::freeOwnedAudioBufferList;

IOSRecorderCallback::IOSRecorderCallback(
const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
float sampleRate,
Expand Down Expand Up @@ -118,17 +119,6 @@
}
}

static inline void freeOwnedAudioBufferList(const AudioBufferList *bufferList)
{
if (bufferList == nullptr) {
return;
}
for (UInt32 i = 0; i < bufferList->mNumberBuffers; ++i) {
std::free(bufferList->mBuffers[i].mData);
}
std::free(const_cast<AudioBufferList *>(bufferList));
}

/// @brief Receives audio data from the recorder, processes it, and stores it in the circular buffer.
/// The data is converted using AVAudioConverter if the input format differs from the user desired callback format.
/// This method runs on the audio thread.
Expand All @@ -148,28 +138,10 @@ static inline void freeOwnedAudioBufferList(const AudioBufferList *bufferList)
// CoreAudio owns `audioBufferList` only for the duration of this synchronous
// callback. Copy into an owned AudioBufferList before handing off to the
// worker thread; the consumer in taskOffloaderFunction frees it.
UInt32 bufferCount = audioBufferList->mNumberBuffers;
size_t headerSize = offsetof(AudioBufferList, mBuffers) + sizeof(AudioBuffer) * bufferCount;
AudioBufferList *owned = static_cast<AudioBufferList *>(std::malloc(headerSize));
AudioBufferList *owned = copyAudioBufferList(audioBufferList);
if (owned == nullptr) {
return;
}
owned->mNumberBuffers = bufferCount;
for (UInt32 i = 0; i < bufferCount; ++i) {
UInt32 byteSize = audioBufferList->mBuffers[i].mDataByteSize;
owned->mBuffers[i].mNumberChannels = audioBufferList->mBuffers[i].mNumberChannels;
owned->mBuffers[i].mDataByteSize = byteSize;
void *channelData = std::malloc(byteSize);
if (channelData == nullptr) {
for (UInt32 j = 0; j < i; ++j) {
std::free(owned->mBuffers[j].mData);
}
std::free(owned);
return;
}
std::memcpy(channelData, audioBufferList->mBuffers[i].mData, byteSize);
owned->mBuffers[i].mData = channelData;
}
offloader_->getSender()->send({.audioBufferList = owned, .numFrames = numFrames});
}

Expand Down Expand Up @@ -202,8 +174,6 @@ static inline void freeOwnedAudioBufferList(const AudioBufferList *bufferList)
return;
}

size_t outputFrameCount = ceil(numFrames * (sampleRate_ / bufferFormat_.sampleRate));

for (size_t i = 0; i < bufferFormat_.channelCount; ++i) {
memcpy(
converterInputBuffer_.mutableAudioBufferList->mBuffers[i].mData,
Expand All @@ -230,7 +200,6 @@ static inline void freeOwnedAudioBufferList(const AudioBufferList *bufferList)
};

[converter_ convertToBuffer:converterOutputBuffer_ error:&error withInputFromBlock:inputBlock];
converterOutputBuffer_.frameLength = sampleRate_ / bufferFormat_.sampleRate * numFrames;

if (error != nil) {
invokeOnErrorCallback(
Expand All @@ -239,9 +208,14 @@ static inline void freeOwnedAudioBufferList(const AudioBufferList *bufferList)
return;
}

AVAudioFrameCount producedFrames = converterOutputBuffer_.frameLength;
if (producedFrames == 0) {
return;
}

for (int i = 0; i < channelCount_; ++i) {
auto *data = static_cast<float *>(converterOutputBuffer_.audioBufferList->mBuffers[i].mData);
circularBuffer_[i]->push_back(data, outputFrameCount);
circularBuffer_[i]->push_back(data, producedFrames);
}

if (circularBuffer_[0]->getNumberOfAvailableFrames() >= bufferLength_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
rotateFiles();
}
}
framesWritten_.fetch_add(numFrames, std::memory_order_relaxed);
}

CloseFileResult IOSRotatingFileWriter::closeFile()
Expand All @@ -76,8 +75,11 @@

double IOSRotatingFileWriter::getCurrentDuration() const
{
return static_cast<double>(framesWritten_.load(std::memory_order_relaxed)) /
fileProperties_->sampleRate;
double currentSegmentDuration = 0.0;
if (currentWriter_ != nullptr) {
currentSegmentDuration = currentWriter_->getCurrentDuration();
}
return cumulativeDurationSec_ + currentSegmentDuration;
}

OpenFileResult IOSRotatingFileWriter::openInnerWriter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#ifndef __OBJC__
typedef struct AudioBufferList AudioBufferList;
#endif

namespace audioapi::ios {

/// Frees an AudioBufferList allocated by `copyAudioBufferList`.
void freeOwnedAudioBufferList(const AudioBufferList *bufferList);

/// Deep-copies a Core Audio buffer list so it can outlive a synchronous I/O callback.
/// Returns nullptr on allocation failure. Caller must free with `freeOwnedAudioBufferList`.
AudioBufferList *copyAudioBufferList(const AudioBufferList *audioBufferList);

} // namespace audioapi::ios
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#import <CoreAudio/CoreAudioTypes.h>

#include <audioapi/ios/core/utils/OwnedAudioBufferList.h>

#include <cstddef>
#include <cstdlib>
#include <cstring>

namespace audioapi::ios {

void freeOwnedAudioBufferList(const AudioBufferList *bufferList)
{
if (bufferList == nullptr) {
return;
}
for (UInt32 i = 0; i < bufferList->mNumberBuffers; ++i) {
std::free(bufferList->mBuffers[i].mData);
}
std::free(const_cast<AudioBufferList *>(bufferList));
}

AudioBufferList *copyAudioBufferList(const AudioBufferList *audioBufferList)
{
if (audioBufferList == nullptr) {
return nullptr;
}

UInt32 bufferCount = audioBufferList->mNumberBuffers;
size_t headerSize = offsetof(AudioBufferList, mBuffers) + sizeof(AudioBuffer) * bufferCount;
auto *owned = static_cast<AudioBufferList *>(std::malloc(headerSize));
if (owned == nullptr) {
return nullptr;
}
owned->mNumberBuffers = bufferCount;
for (UInt32 i = 0; i < bufferCount; ++i) {
UInt32 byteSize = audioBufferList->mBuffers[i].mDataByteSize;
owned->mBuffers[i].mNumberChannels = audioBufferList->mBuffers[i].mNumberChannels;
owned->mBuffers[i].mDataByteSize = byteSize;
void *channelData = std::malloc(byteSize);
if (channelData == nullptr) {
for (UInt32 j = 0; j < i; ++j) {
std::free(owned->mBuffers[j].mData);
}
std::free(owned);
return nullptr;
}
std::memcpy(channelData, audioBufferList->mBuffers[i].mData, byteSize);
owned->mBuffers[i].mData = channelData;
}
return owned;
}

} // namespace audioapi::ios
Loading