<commit_before>/*
 *  RXCardAudioSource_test.m
 *  rivenx
 *
 *  Created by <NAME> on 27/03/2006.
 *  Copyright 2005-2010 MacStorm. All rights reserved.
 *
 */

#import <Foundation/Foundation.h>

#import <sysexits.h>
#import <fcntl.h>
#import <unistd.h>

#import <MHKKit/MHKAudioDecompression.h>

#import "Base/RXThreadUtilities.h"
#import "Base/RXLogging.h"

#import "Rendering/Audio/PublicUtility/CAAudioFile.h"
#import "Rendering/Audio/PublicUtility/CAPThread.h"
#import "Rendering/Audio/PublicUtility/CAGuard.h"

#import "Rendering/Audio/RXAudioRenderer.h"
#import "Rendering/Audio/RXCardAudioSource.h"

using namespace RX;


@interface EAFDecompressor : NSObject <MHKAudioDecompression> {
    CAAudioFile* audioFile;
    UInt32 clientBytesPerFrame;
}

- (id)initWithSystemPath:(const char *)syspath;

@end

@implementation EAFDecompressor

- (id)initWithSystemPath:(const char *)syspath {
    self = [super init];
    if (!self) return nil;
    
    if (!syspath) {
        [self release];
        return nil;
    }
    
    RXOLog(@"opening %s", syspath);
    audioFile = new CAAudioFile();
    audioFile->Open(syspath);
    
    // set client format to canonical + interleaved
    CAStreamBasicDescription clientFormat;
    clientFormat.mSampleRate = 44100.0;
    clientFormat.SetCanonical(audioFile->GetFileDataFormat().mChannelsPerFrame, true);
    audioFile->SetClientFormat(clientFormat);
    
    clientBytesPerFrame = [self outputFormat].mBytesPerFrame;
    
    RXOLog(@"%u bpf, %lld frames", clientBytesPerFrame, [self frameCount]);
    return self;
}

- (void)dealloc {
    if (audioFile) delete audioFile;

    [super dealloc];
}

- (AudioStreamBasicDescription)outputFormat {
    return audioFile->GetClientDataFormat();
}

- (SInt64)frameCount {
    return audioFile->GetNumberFrames();
}

- (void)reset {
    audioFile->Seek(0);
}

- (void)fillAudioBufferList:(AudioBufferList *)abl {
    UInt32 frames = abl->mBuffers[0].mDataByteSize / clientBytesPerFrame;
    audioFile->Read(frames, abl);
}

@end


void* source_task_thread(void* context) {
    CardAudioSource* source = reinterpret_cast<CardAudioSource *>(context);
    CAGuard taskGuard("task guard");
    
    taskGuard.Lock();
    while(true) {
        source->RenderTask();
        taskGuard.WaitFor(100000000ULL);
    }
    taskGuard.Unlock();
    
    return NULL;
}

int main (int argc, char* const argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];<commit_msg>Migrated the audio soure test to using ExtAudioFile directly, since apparently CAAudioFile was removed from the CA PublicUtility.<commit_after>/*
 *  RXCardAudioSource_test.m
 *  rivenx
 *
 *  Created by <NAME> on 27/03/2006.
 *  Copyright 2005-2010 MacStorm. All rights reserved.
 *
 */

#import <sysexits.h>
#import <fcntl.h>
#import <unistd.h>

#import <Foundation/Foundation.h>
#import <AudioToolbox/ExtendedAudioFile.h>

#import <MHKKit/MHKAudioDecompression.h>

#import "Base/RXThreadUtilities.h"
#import "Base/RXLogging.h"

#import "Rendering/Audio/PublicUtility/CAPThread.h"
#import "Rendering/Audio/PublicUtility/CAGuard.h"

#import "Rendering/Audio/RXAudioRenderer.h"
#import "Rendering/Audio/RXCardAudioSource.h"

using namespace RX;


@interface EAFDecompressor : NSObject <MHKAudioDecompression> {
    ExtAudioFileRef audioFile;
    UInt32 clientBytesPerFrame;
}

- (id)initWithSystemPath:(const char *)syspath;

@end

@implementation EAFDecompressor

- (id)initWithSystemPath:(const char *)syspath {
    self = [super init];
    if (!self) return nil;
    
    if (!syspath) {
        [self release];
        return nil;
    }
    
    RXOLog(@"opening %s", syspath);
    CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)syspath, strlen(syspath), false);
    assert(ExtAudioFileOpenURL(url, &audioFile) == noErr);
    CFRelease(url);
    
    AudioStreamBasicDescription file_asbd;
    UInt32 prop_size = sizeof(AudioStreamBasicDescription);
    assert(ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_FileDataFormat, &prop_size, &file_asbd) == noErr);
    
    // set client format to canonical + interleaved
    CAStreamBasicDescription clientFormat;
    clientFormat.mSampleRate = 44100.0;
    clientFormat.SetCanonical(file_asbd.mChannelsPerFrame, true);
    assert(ExtAudioFileSetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &clientFormat) == noErr);
    
    clientBytesPerFrame = [self outputFormat].mBytesPerFrame;
    assert(clientBytesPerFrame > 0);
    
    RXOLog(@"%u bpf, %lld frames", clientBytesPerFrame, [self frameCount]);
    return self;
}

- (void)dealloc {
    if (audioFile)
        ExtAudioFileDispose(audioFile);

    [super dealloc];
}

- (AudioStreamBasicDescription)outputFormat {
    AudioStreamBasicDescription asbd;
    UInt32 size = sizeof(AudioStreamBasicDescription);
    ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, &size, &asbd);
    return asbd;
}

- (SInt64)frameCount {
    SInt64 frames;
    UInt32 size = sizeof(SInt64);
    ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_FileLengthFrames, &size, &frames);
    return frames;
}

- (void)reset {
    ExtAudioFileSeek(audioFile, 0);
}

- (void)fillAudioBufferList:(AudioBufferList *)abl {
    UInt32 frames = abl->mBuffers[0].mDataByteSize / clientBytesPerFrame;
    ExtAudioFileRead(audioFile, &frames, abl);
}

@end


void* source_task_thread(void* context) {
    CardAudioSource* source = reinterpret_cast<CardAudioSource *>(context);
    CAGuard taskGuard("task guard");
    
    taskGuard.Lock();
    while(true) {
        source->RenderTask();
        taskGuard.WaitFor(100000000ULL);
    }
    taskGuard.Unlock();
    
    return NULL;
}

int main (int argc, char* const argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
{'fraction_non_alphanumeric': 0.11636239549564921, 'fraction_numerical': 0.015355741341068077, 'mean_word_length': 4.290613718411552, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5977957', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1930, 'n_words': 370}