-
Notifications
You must be signed in to change notification settings - Fork 49
/
CfxrDocument.m
212 lines (167 loc) · 6.23 KB
/
CfxrDocument.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// MyDocument.m
// cfxr
//
// Created by Joachim Bengtsson on 2008-05-17.
// Copyright Third Cog Software 2008 . All rights reserved.
//
#import "CfxrDocument.h"
#import "Sound.h"
#import "Playback.h"
NSString *CfxrSoundPBoardType = @"CfxrSoundPBoardType";
@implementation CfxrDocument
- (NSString *)windowNibName
{
return @"CfxrDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
[super windowControllerDidLoadNib:windowController];
if([Sound countInContext:[self managedObjectContext]] == 0)
[self generateSoundFromCategory:@"Empty"];
// Sort by inverse index by default
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]
initWithKey: @"index" ascending: NO
selector:@selector(compare:)];
[soundsController setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
soundsTable.delegate = self;
}
-(void)dealloc;
{
[Playback playback].playingSound = nil;
if([Playback playback].delegate == self)
[Playback playback].delegate = nil;
[super dealloc];
}
-(IBAction)generateSound:(id)sender;
{
[self generateSoundFromCategory:[[sender selectedCell] title]];
}
-(Sound*)generateSoundFromCategory:(NSString*)category;
{
Sound *sound = [NSEntityDescription insertNewObjectForEntityForName:@"Sound"
inManagedObjectContext:[self managedObjectContext]];
[sound generateParamsFromCategory:category];
sound.name = category;
[soundsController setSelectedObjects:[NSArray arrayWithObject:sound]];
[[Playback playback] play:sound];
[soundsController rearrangeObjects];
return sound;
}
-(IBAction)play:(id)sender;
{
if([soundsController selectedObjects].count == 0) return;
Sound *s = [[soundsController selectedObjects] objectAtIndex:0];
[[Playback playback] play:s];
}
-(IBAction)playOnChange:(id)sender;
{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"playOnChange"])
[self play:sender];
}
-(IBAction)toggleLooping:(id)sender;
{
if([sender state] == NSOnState) {
[Playback playback].delegate = self;
[self play:nil];
} else
[Playback playback].delegate = nil;
}
-(void)playbackStoppedPlaying:(Playback*)playback_;
{
[self play:nil];
}
-(IBAction)export:(id)sender;
{
if([soundsController selectedObjects].count == 0) return;
Sound *s = [[soundsController selectedObjects] objectAtIndex:0];
NSSavePanel *savePanel = [NSSavePanel savePanel];
savePanel.title = @"Export WAV as";
savePanel.prompt = @"Export WAV";
savePanel.nameFieldLabel = @"Export as:";
savePanel.requiredFileType = @"wav";
savePanel.canSelectHiddenExtension = YES;
NSString *filename = [NSString stringWithFormat:@"%@ %03d %@.wav",
[self displayName], s.index.intValue, s.name];
[savePanel beginSheetForDirectory:nil
file:filename
modalForWindow:[[[self windowControllers] objectAtIndex:0] window]
modalDelegate:self
didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
contextInfo:s];
}
-(IBAction)exportQuickly:(id)sender;
{
if([self fileURL] == nil) {
NSRunAlertPanel(@"You need to save first.", @"When you quick export, you export to the same folder as this document. Thus, you must save this document to somewhere on your computer before you can quick export.", @"Okay then", nil, nil);
return;
}
if([soundsController selectedObjects].count == 0) return;
Sound *s = [[soundsController selectedObjects] objectAtIndex:0];
NSString *sName = [s.name stringByReplacingOccurrencesOfString:@"/" withString:@" or "];
NSString *filename = [NSString stringWithFormat:@"%@ %03d %@.wav",
[self displayName], s.index.intValue, sName];
NSString *path = [[[[self fileURL] path] stringByDeletingLastPathComponent] stringByAppendingPathComponent:filename];
NSLog(@"I'm now exporting %@ to %@", s, path);
id oldDelegate = [Playback playback].delegate;
[Playback playback].delegate = nil;
NSError *error;
if(![[Playback playback] export:s to:path error:&error])
[self presentError:error];
[Playback playback].delegate = oldDelegate;
}
- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
{
if(returnCode == NSCancelButton) return;
id oldDelegate = [Playback playback].delegate;
[Playback playback].delegate = nil;
NSError *error;
if(![[Playback playback] export:contextInfo to:sheet.filename error:&error])
NSRunAlertPanel(@"Export failed.", [error localizedDescription], @"Bummer", nil, nil);
[Playback playback].delegate = oldDelegate;
}
-(IBAction)takeMasterVolumeFrom:(id)sender;
{
[Playback playback].masterVolume = [sender floatValue]/100;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
[self play:nil];
}
- (IBAction) copy:(id) sender
{
NSArray *selectedObjects = [soundsController selectedObjects];
NSUInteger count = [selectedObjects count];
if (count == 0) return;
NSMutableArray *copyObjectsArray = [NSMutableArray arrayWithCapacity:count];
NSMutableArray *copyStringsArray = [NSMutableArray arrayWithCapacity:count];
for (Sound *sound in selectedObjects)
{
[copyObjectsArray addObject:[sound dictionaryRepresentation]];
[copyStringsArray addObject:[sound description]];
}
NSPasteboard *generalPasteboard = [NSPasteboard generalPasteboard];
[generalPasteboard declareTypes:[NSArray arrayWithObjects:CfxrSoundPBoardType, NSStringPboardType, nil] owner:self];
NSData *copyData = [NSKeyedArchiver archivedDataWithRootObject:copyObjectsArray];
[generalPasteboard setData:copyData forType:CfxrSoundPBoardType];
[generalPasteboard setString:[copyStringsArray componentsJoinedByString:@"\n"] forType:NSStringPboardType];
}
- (IBAction) paste:(id) sender
{
NSPasteboard *generalPasteboard = [NSPasteboard generalPasteboard];
NSData *data = [generalPasteboard dataForType:CfxrSoundPBoardType];
if (data == nil)
{
return;
}
NSArray *soundsArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSManagedObjectContext *moc = [self managedObjectContext];
for (NSDictionary *soundDictionary in soundsArray) {
//create a new Expense entity
Sound *sound = (id)[NSEntityDescription insertNewObjectForEntityForName:@"Sound" inManagedObjectContext:moc];
// Dump the values from the dictionary into the new entity
[sound setValuesForKeysWithDictionary:soundDictionary];
}
}
@end