2022-02-06 11:08:34 +00:00
|
|
|
//
|
|
|
|
// ChunkList.m
|
|
|
|
// CogAudio Framework
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 2/5/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "ChunkList.h"
|
|
|
|
|
|
|
|
@implementation ChunkList
|
|
|
|
|
|
|
|
@synthesize listDuration;
|
|
|
|
@synthesize maxDuration;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)initWithMaximumDuration:(double)duration {
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if(self) {
|
|
|
|
chunkList = [[NSMutableArray alloc] init];
|
|
|
|
listDuration = 0.0;
|
|
|
|
maxDuration = duration;
|
|
|
|
|
|
|
|
inAdder = NO;
|
|
|
|
inRemover = NO;
|
|
|
|
stopping = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
stopping = YES;
|
|
|
|
while(inAdder || inRemover) {
|
|
|
|
usleep(500);
|
|
|
|
}
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)reset {
|
|
|
|
@synchronized(chunkList) {
|
|
|
|
[chunkList removeAllObjects];
|
|
|
|
listDuration = 0.0;
|
|
|
|
}
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)isEmpty {
|
|
|
|
@synchronized(chunkList) {
|
|
|
|
return [chunkList count] == 0;
|
|
|
|
}
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)isFull {
|
|
|
|
return listDuration >= maxDuration;
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)addChunk:(AudioChunk *)chunk {
|
|
|
|
if(stopping) return;
|
|
|
|
|
|
|
|
inAdder = YES;
|
|
|
|
|
|
|
|
const double chunkDuration = [chunk duration];
|
|
|
|
|
|
|
|
@synchronized(chunkList) {
|
|
|
|
[chunkList addObject:chunk];
|
|
|
|
listDuration += chunkDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
inAdder = NO;
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (AudioChunk *)removeSamples:(size_t)maxFrameCount {
|
|
|
|
if(stopping) {
|
|
|
|
return [[AudioChunk alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
@synchronized(chunkList) {
|
|
|
|
inRemover = YES;
|
|
|
|
if(![chunkList count])
|
|
|
|
return [[AudioChunk alloc] init];
|
|
|
|
AudioChunk *chunk = [chunkList objectAtIndex:0];
|
|
|
|
if([chunk frameCount] <= maxFrameCount) {
|
|
|
|
[chunkList removeObjectAtIndex:0];
|
|
|
|
listDuration -= [chunk duration];
|
|
|
|
inRemover = NO;
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
NSData *removedData = [chunk removeSamples:maxFrameCount];
|
|
|
|
AudioChunk *ret = [[AudioChunk alloc] init];
|
|
|
|
[ret setFormat:[chunk format]];
|
|
|
|
[ret assignSamples:[removedData bytes] frameCount:maxFrameCount];
|
|
|
|
listDuration -= [ret duration];
|
|
|
|
inRemover = NO;
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-06 11:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|