-
Notifications
You must be signed in to change notification settings - Fork 7
/
UKExtAudioFile.m
112 lines (85 loc) · 2.79 KB
/
UKExtAudioFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// UKExtAudioFile.m
// UKSoundWaveformView
//
// Created by Uli Kusterer on 07.10.09.
// Copyright 2009 The Void Software. All rights reserved.
//
// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------
#import "UKExtAudioFile.h"
#import "UKHelperMacros.h"
#import "UKAudioBufferList.h"
#import <AudioToolbox/AudioToolbox.h>
// -----------------------------------------------------------------------------
// Instance vars:
// -----------------------------------------------------------------------------
struct UKExtAudioFileIVars
{
ExtAudioFileRef mAudioFileRef; // Reference to the audio file object while we have it opened.
NSURL* mFileURL; // URL of our file.
AudioStreamBasicDescription mReadingFormat; // Format in which we get data from the audio file.
};
@implementation UKExtAudioFile
-(id) initWithContentsOfURL: (NSURL*)fileURL
{
if(( self = [super init] ))
{
ivars = calloc( 1, sizeof(struct UKExtAudioFileIVars) );
ivars->mFileURL = [fileURL retain];
OSStatus err = ExtAudioFileOpenURL( (CFURLRef)ivars->mFileURL, &ivars->mAudioFileRef );
if( err != noErr )
{
[self release];
return nil;
}
AudioStreamBasicDescription desiredFormat = { 44100.0, kAudioFormatLinearPCM,
kAudioFormatFlagsNativeFloatPacked,
sizeof(float), 1, sizeof(float),
1, sizeof(float) * 8 };
ivars->mReadingFormat = desiredFormat;
if( ExtAudioFileSetProperty( ivars->mAudioFileRef, kExtAudioFileProperty_ClientDataFormat,
sizeof(ivars->mReadingFormat), &ivars->mReadingFormat ) != noErr )
{
[self release];
return nil;
}
}
return self;
}
-(void) dealloc
{
if( ivars )
{
if( ivars->mAudioFileRef != NULL )
ExtAudioFileDispose( ivars->mAudioFileRef );
DESTROY(ivars->mFileURL);
free( ivars );
ivars = NULL;
}
[super dealloc];
}
-(UKAudioBufferList*) framesFromIndex: (long long)firstIdx numItems: (NSInteger)count
{
if( ExtAudioFileSeek( ivars->mAudioFileRef, firstIdx ) != noErr )
return nil;
UInt32 numFramesRead = count;
UKAudioBufferList* bufList = [[UKAudioBufferList alloc] initWithCapacity: numFramesRead audioFormat: &ivars->mReadingFormat];
if( ExtAudioFileRead( ivars->mAudioFileRef, &numFramesRead, [bufList bufferList] ) != noErr )
{
[bufList release];
return nil;
}
[bufList setFrameCount: numFramesRead]; // Make sure buffer list knows how much was actually read.
return [bufList autorelease];
}
-(long long) frameCount
{
SInt64 frameCount = 0;
UInt32 propSize = sizeof(SInt64);
if( ExtAudioFileGetProperty( ivars->mAudioFileRef, kExtAudioFileProperty_FileLengthFrames, &propSize, &frameCount ) != noErr )
frameCount = -1;
return frameCount;
}
@end