-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecordingInfo.mm
178 lines (150 loc) · 5.28 KB
/
RecordingInfo.mm
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "RecordingInfo.h"
#import "UserSettings.h"
@interface RecordingInfo (PrimitiveAccessors)
- (NSNumber*)primitiveProgress;
- (void)setPrimitiveProgress:(NSNumber*)value;
- (NSNumber*)primitiveUploaded;
- (void)setPrimitiveUploaded:(NSNumber*)value;
- (NSNumber*)primitiveUploading;
- (void)setPrimitiveUploading:(NSNumber*)value;
@end
@implementation RecordingInfo
@dynamic filePath;
@dynamic name;
@dynamic progress;
@dynamic size;
@dynamic uploaded;
@dynamic uploading;
- (float)progress
{
NSNumber* tmpValue;
[self willAccessValueForKey:@"progress"];
tmpValue = [self primitiveProgress];
[self didAccessValueForKey:@"progress"];
return [tmpValue floatValue];
}
- (void)setProgress:(float)value
{
[self willChangeValueForKey:@"progress"];
[self setPrimitiveProgress:[NSNumber numberWithFloat:value]];
[self didChangeValueForKey:@"progress"];
}
- (BOOL)uploaded
{
NSNumber* tmpValue;
[self willAccessValueForKey:@"uploaded"];
tmpValue = [self primitiveUploaded];
[self didAccessValueForKey:@"uploaded"];
return [tmpValue boolValue];
}
- (void)setUploaded:(BOOL)value
{
[self willChangeValueForKey:@"uploaded"];
[self setPrimitiveUploaded:[NSNumber numberWithBool:value]];
[self didChangeValueForKey:@"uploaded"];
}
- (BOOL)uploading
{
NSNumber* tmpValue;
[self willAccessValueForKey:@"uploading"];
tmpValue = [self primitiveUploading];
[self didAccessValueForKey:@"uploading"];
return [tmpValue boolValue];
}
- (void)setUploading:(BOOL)value
{
[self willChangeValueForKey:@"uploading"];
[self setPrimitiveUploading:[NSNumber numberWithBool:value]];
[self didChangeValueForKey:@"uploading"];
}
+ (NSString*)niceSizeOfFileString:(int)bytes
{
if (bytes<1024)
return [NSString stringWithFormat: NSLocalizedString(@"%d bytes", "@Format for size in bytes"), bytes];
else if (bytes<1048576)
return [NSString stringWithFormat: NSLocalizedString(@"%dKB", "@Format for size in kilobytes"), (bytes/1024)];
else
return [NSString stringWithFormat: NSLocalizedString(@"%.2fMB", @"Format for size in megabytes"),
((float)bytes/1048576)];
}
static AudioFileTypeID currentAudioFileType;
+ (NSString*)generateRecordingPath
{
NSString* suffix = [[NSUserDefaults standardUserDefaults] stringForKey:kSettingsRecordingsFileFormatKey];
if ([suffix isEqualToString:@"caf"] == YES) {
currentAudioFileType = kAudioFileCAFType;
}
else if ([suffix isEqualToString:@"wav"] == YES) {
currentAudioFileType = kAudioFileWAVEType;
}
else if ([suffix isEqualToString:@"aiff"] == YES) {
currentAudioFileType = kAudioFileAIFFType;
}
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:NSLocalizedString(@"Recordings",
@"Recordings directory")];
NSFileManager* fileManager = [[[NSFileManager alloc] init] autorelease];
if ([fileManager fileExistsAtPath:path] == NO) {
NSError* err = nil;
if ([fileManager createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:&err] == NO) {;
LOG(@"RecordingInfo.generateRecordingPath: failed to create Recordings directory! - %@", err);
path = [path stringByDeletingLastPathComponent];
}
}
NSString* name = [[dateFormatter stringFromDate:[NSDate date]] stringByAppendingPathExtension:suffix];
path = [path stringByAppendingPathComponent:name];
return path;
}
+ (AudioFileTypeID)getCurrentAudioFileType
{
return currentAudioFileType;
}
- (void)awakeFromFetch
{
[super awakeFromFetch];
//
// Uploading state is temporary and is always reset when reloaded.
//
if ([[self primitiveUploading] boolValue] == YES) {
LOG(@"RecordingInfo.awakeFromFetch: resetting self.uploading");
self.uploading = NO;
}
if ([[self primitiveProgress] floatValue] != 0.0) {
LOG(@"RecordingInfo.awakeFromFetch: resetting self.progress");
self.progress = 0.0;
}
}
- (void)initialize
{
NSString* path = [RecordingInfo generateRecordingPath];
self.filePath = path;
NSURL* fileUrl = [NSURL fileURLWithPath:path];
self.name = [[fileUrl lastPathComponent] stringByDeletingPathExtension];
self.size = [RecordingInfo niceSizeOfFileString:0];
self.uploaded = NO;
self.uploading = NO;
self.progress = 0.0;
}
- (void)updateSizeWith:(UInt32)size
{
self.size = [RecordingInfo niceSizeOfFileString:size];
}
- (void)finalizeSize
{
NSString* path = self.filePath;
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError* err;
NSDictionary* attr = [fileManager attributesOfItemAtPath:path error:&err];
self.size = [RecordingInfo niceSizeOfFileString:attr.fileSize];
}
@end