download MKInstrument.m
Language: ObjectiveC
Copyright: (c) 1988-1992, NeXT Computer, Inc. (c) 1999-2004, The MusicKit Project. (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT (c) 1994 Stanford University
LOC: 174
Project Info
The MusicKit(musickit)
Server: SourceForge
Type: cvs
...sicKit\Frameworks\MusicKit\
   _ArielQP.h
   _DSPMK.h
   _error.h
   _midi.h
   _MKAppProxy.h
   _MKAppProxy.m
   _MKNameTable.h
   _MKNameTable.m
   _MKParameter.h
   _MTCHelper.h
   _MTCHelper.m
   _musickit.h
   _noteRecorder.h
   _OrchloopbeginUG.h
   _OrchloopbeginUG.m
   _ParName.h
   _ParName.m
   _scorefile.h
   _ScorefileVar.h
   _ScorefileVar.m
   _SharedSynthInfo.h
   _SharedSynthInfo.m
   _synthElementMethods.m
   _time.h
   ArielQP.h
   ArielQP.m
   classFuncs.h
   ConductorPrivate.h
   dsp_types.h
   DSPSerialPortDevice.h
   dspwrap.h
   EnvelopePrivate.h
   equalTempered.m
   errors.h
   fastFFT.c
   fastFFT.h
   GNUmakefile.postamble
   GNUmakefile.preamble.in
   InstrumentPrivate.h
   keynums.h
   libMusicKit.def
   Localized.strings
   make.sh
   Makefile.postamble
   Makefile.preamble
   midi_spec.h
   midifile.h
   midifile.m
   MidiPrivate.h
   midiTranslation.h
   MKConductor.h
   MKConductor.m
   MKConductorDelegate.h
   MKDeviceStatus.h
   MKEnvelope.h
   MKEnvelope.m
   MKFilePerformer.h
   MKFilePerformer.m
   MKFileWriter.h
   MKFileWriter.m
   MKInstrument.h
   MKInstrument.m
   MKMidi.h
   MKMidi.m
   MKMixerInstrument.h
   MKMixerInstrument.m
   MKMTCPerformer.h
   MKMTCPerformer.m
   MKNote.h
   MKNoteFilter.h
   MKNoteFilter.m
   MKNoteReceiver.h
   MKNoteReceiver.m
   MKNoteSender.h
   MKNoteSender.m
   MKOrchestra.h
   MKPart.m
   MKPartials.h
   MKPartials.m
   ...tialsWaveshapingTable.m
   MKPartPerformer.h
   MKPartPerformer.m
   MKPartRecorder.h
   MKPartRecorder.m
   MKPatch.h
   MKPatch.m
   MKPatchConnection.h
   MKPatchConnection.m
   MKPatchEntry.h
   MKPatchEntry.m
   MKPatchTemplate.h
   MKPatchTemplate.m
   MKPerformer.h
   MKPerformer.m
   MKPerformerDelegate.h
   MKPlugin.h
   MKSamplerInstrument.h
   MKSamplerInstrument.m
   MKSamples.h
   MKScore.h
   MKScore.m
   MKScorefileObject.h
   MKScorefilePerformer.h
   MKScorefilePerformer.m
   MKScorefileWriter.m
   MKScorePerformer.h
   MKScorePerformer.m
   MKScoreRecorder.h
   MKScoreRecorder.m
   MKSynthData.h
   MKSynthData.m
   MKSynthInstrument.h
   MKSynthPatch.h
   MKTimbre.h
   MKTimbre.m
   MKTuningSystem.h
   MKTuningSystem.m
   MKUnitGenerator.h
   MKUnitGenerator.m
   MKWaveTable.h
   MKWaveTable.m
   mtcMidi.m
   mtcMidiPrivate.m
   MusicKit-Info.plist
   MusicKit.h
   MusicKitConfig.h.in
   names.h
   noDVal.h
   noteDispatcherMethods.m
   NotePrivate.h
   noteRecorderCFuncs.m
   noteRecorderMethods.m
   orch.h
   OrchestraPrivate.h
   OrchloopbeginUG.h
   OrchloopbeginUG.m
   orchloopbeginUGInclude.m
   params.h
   parNames.m
   partialsDBInclude.m
   PartialsPrivate.h
   PartPerformerPrivate.h
   PartPrivate.h
   PartRecorderPrivate.h
   PatchTemplatePrivate.h
   PB.project
   PerformerPrivate.h
   pitches.h
   platform.make
   ...efilePerformerPrivate.h
   ScorePerformerPrivate.h
   ScorePrivate.h
   ScoreRecorderPrivate.h
   SynthDataPrivate.h
   synthElementCFuncs.m
   synthElementMethods.m
   SynthInstrumentPrivate.h
   SynthPatchList.h
   SynthPatchList.m
   SynthPatchPrivate.h
   timetagInclude.m
   timeunits.h
   tokenNames.m
   tokens.h
   trigonometry.c
   trigonometry.h
   TuningSystemPrivate.h
   UnitGeneratorPrivate.h
   win32-def.top
   writeBinary.m
   writeMidi.m
   writeScore.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
  $Id: MKInstrument.m,v 1.15 2004/12/13 02:49:47 leighsmith Exp $
  Defined In: The MusicKit

  Description: 
    See MKInstrument.h

  Original Author: David A. Jaffe

  Copyright (c) 1988-1992, NeXT Computer, Inc.
  Portions Copyright (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT
  Portions Copyright (c) 1994 Stanford University
  Portions Copyright (c) 1999-2004, The MusicKit Project.
*/
/* Modification history prior to committing to CVS:

  03/13/90/daj - Changes to support category for private methods.
  03/21/90/daj - Added archiving.
  04/21/90/daj - Small mods to get rid of -W compiler warnings.
  08/23/90/daj - Changed to zone API.
  09/26/90/daj & lbj - Added check for [owner inPerformance] in 
                       addNoteReceiver and check for noteSeen in 
		       freeNoteReceivers.
  02/25/91/daj - Added removeFromPerformance and disconnectNoteReceivers.
                 Also flushed class description comment.
  10/7/93/daj - Added check in addNoteReceiver for existance of MKNoteReceivers list.
*/

#import "_musickit.h"

#import "ConductorPrivate.h"
#import "InstrumentPrivate.h"

@implementation MKInstrument

#define VERSION2 2

+ (void) initialize
{
    if (self != [MKInstrument class])
	return;
    _MKCheckInit();
    [MKInstrument setVersion:VERSION2];//sb: suggested by Stone conversion guide (replaced self)
}

- init
  /* TYPE: Creating; Initializes the receiver.
   * Initializes the receiver.
   * You never invoke this method directly,
   * it's sent by the superclass when the receiver is created.
   * An overriding subclass method should send [super\ init]
   * before setting its own defaults. 
   */
{
    noteReceivers = [[NSMutableArray alloc] init];
    return self;
}

- realizeNote: (MKNote *) aNote fromNoteReceiver: (MKNoteReceiver *) aNoteReceiver
  /* TYPE: Performing; Realizes aNote.
   * Realizes aNote in the manner defined by the subclass.  
   * You never send this message; it's sent to an MKInstrument
   * as its MKNoteReceivers receive MKNote objects.
   */
{
    return self;
}


- firstNote: (MKNote *) aNote
  /* TYPE: Performing; Received just before the first MKNote is realized.
   * You never invoke this method directly; it's sent by the receiver to 
   * itself just before it realizes its first MKNote.
   * You can subclass the method to perform pre-realization initialization.
   * aNote, the MKNote that the MKInstrument is about to realize,
   * is provided as a convenience and can be ignored in a subclass 
   * implementation.  The MKInstrument isn't considered to be in performance 
   * until after this method returns.
   * The default implementation does nothing and returns the receiver.
   */
{
    return self;
}

- (NSArray *) noteReceivers	
  /* TYPE: Querying; Returns a copy of the Array of MKNoteReceivers.
   * Returns a copy of the Array of MKNoteReceivers. The MKNoteReceivers themselves
   * are not copied.	
   */
{
    return _MKLightweightArrayCopy(noteReceivers);
    // return [_MKLightweightArrayCopy(noteReceivers) autorelease];
}

- (int) indexOfNoteReceiver: (MKNoteReceiver *) aNoteReceiver
{
    return [noteReceivers indexOfObject: aNoteReceiver];
}

- (BOOL) isNoteReceiverPresent: (MKNoteReceiver *) aNoteReceiver
  /* TYPE: Querying; Returns YES if aNoteReceiver is present.
   * Returns YES if aNoteReceiver is a member of the receiver's 
   * MKNoteReceiver collection.  Otherwise returns NO.
   */
{
    return ([self indexOfNoteReceiver: aNoteReceiver] == -1) ? NO : YES;
}

- addNoteReceiver: (MKNoteReceiver *) aNoteReceiver
   /* TYPE: Modifying; Adds aNoteReceiver to the receiver.
    * Removes aNoteReceiver from its current owner and adds it to the 
    * receiver. 
    * You can't add a MKNoteReceiver to an MKInstrument that's in performance.
    * If the receiver is in a performance, this message is ignored and nil is
    * returned. Otherwise aNoteReceiver is returned.
    */
{
    id owner = [aNoteReceiver owner];
    
    if (noteSeen ||  /* in performance */
	(owner && (![owner removeNoteReceiver: aNoteReceiver]))) /* owner might be in performance */
	return nil;
    if (!noteReceivers) /* Just in case init wasn't called */
	noteReceivers = [[NSMutableArray alloc] init];
    [noteReceivers addObject: aNoteReceiver];
    [aNoteReceiver _setOwner: self];
    return aNoteReceiver;
}

- removeNoteReceiver: (MKNoteReceiver *) aNoteReceiver
  /* TYPE: Modifying; Removes aNoteReceiver from the receiver.
   * Removes aNoteReceiver from the receiver and returns it
   * (the MKNoteReceiver) or nil if it wasn't owned by the receiver.
   * You can't remove a MKNoteReceiver from an MKInstrument that's in
   * performance. Returns nil in this case.
   */ 
{
    if ([aNoteReceiver owner] != self)
	return nil;
    if (noteSeen)
	return nil;
    [aNoteReceiver _setOwner: nil];
    [noteReceivers removeObject: aNoteReceiver];
    return aNoteReceiver;
}

/* TYPE: Creating; Releases the receiver and its MKNoteReceivers.
 * Removes and releases the receiver's MKNoteReceivers and then releases the receiver itself.  
 * The MKNoteReceiver's connections are severed.
 * If the receiving instance is in a performance, we don't release the noteReceivers
 * ivars although this a leak and is probably the wrong thing to do.
 */
- (void) dealloc
{
    if (!noteSeen) {
	// TODO is this necessary to create a copy of the array?
	NSMutableArray *aList = _MKLightweightArrayCopy(noteReceivers);
	
	[self disconnectNoteReceivers];
	[self removeNoteReceivers];
	[aList removeAllObjects];  // Split this up because elements may try
				   // and remove themselves from noteReceivers when they are freed.
	// [aList release]; // don't release as aList is autoreleased.
	[noteReceivers release];
	noteReceivers = nil;
	[super dealloc];
    }
    else {
	NSLog(@"Assertion failed %@ -dealloc while still in performance!\n", self);
    }
}

/* Broadcasts "disconnect" to MKNoteReceivers. */ 
- disconnectNoteReceivers
{
    [noteReceivers makeObjectsPerformSelector: @selector(disconnect)];
    return self;
}

- removeFromPerformance
    /* Invokes [self disconnectNoteReceivers].  If the receiver is 
       in performance, also sends [self _afterPerformance] */
{
    if (!noteSeen) 
	return nil;
    [self disconnectNoteReceivers];	
    MKCancelMsgRequest(_afterPerfMsgPtr);
    [self _afterPerformance];
    return self;
}

- removeNoteReceivers
  /* Empties noteReceivers by repeatedly sending removeNoteSender:
     with each element of the collection as the argument. */
{
    /* Need to use seq because we may be modifying the List. */
    unsigned i;
    if (!noteReceivers)
      return self;
    i = [noteReceivers count]; 
    while (i--) 
      [self removeNoteReceiver: [noteReceivers objectAtIndex: i]];
    return self;
}

- (BOOL) inPerformance
  /* TYPE: Querying; Returns YES if first MKNote has been seen.
   * Returns NO if the receiver has yet to receive a MKNote object.
   * Otherwise returns YES.
   */
{
    return (noteSeen);
}    

- afterPerformance 
  /* TYPE: Performing; Sent after performance is finished.
   * You never invoke this method; it's automatically
   * invoked when the performance is finished.
   * A subclass can implement this method to do post-performance
   * cleanup.  The default implementation does nothing and
   * returns the receiver.
   */
{
    return self;
}

- copyWithZone: (NSZone *) zone
  /* TYPE: Creating; Creates and returns a copy of the receiver.
   * Creates and returns a new MKInstrument as a copy of the receiver.  
   * The new object has its own MKNoteReceiver collection that contains
   * copies of the receiver's MKNoteReceivers.  The new MKNoteReceivers'
   * connections (see the PerfLink class) are copied from 
   * those in the receiver.
   */
{
    MKNoteReceiver *el, *el_copy;
    int noteReceiverIndex;
    int count;
    MKInstrument *newObj = [[[self class] allocWithZone: zone] init];
    
    newObj->noteSeen = NO;
    newObj->noteReceivers = [[NSMutableArray alloc] initWithCapacity: [noteReceivers count]];
    
    count = [noteReceivers count];
    for (noteReceiverIndex = 0; noteReceiverIndex < count; noteReceiverIndex++) {
	el = [noteReceivers objectAtIndex: noteReceiverIndex];
	el_copy = [el copy];
	[newObj addNoteReceiver: el_copy];
	[el_copy release]; /* since we held retain from the -copy */
    }
    return newObj;
}

- (MKNoteReceiver *) noteReceiver
  /* TYPE: Querying; Returns the receiver's first MKNoteReceiver.
   * Returns the first MKNoteReceiver in the receiver's Array.
   * This is particularly useful for Instruments that have only
   * one MKNoteReceiver.
   */
{
    if ([noteReceivers count] == 0)
        [self addNoteReceiver: [[MKNoteReceiver alloc] init]];
    return [noteReceivers objectAtIndex: 0];
}

- (void) encodeWithCoder: (NSCoder *) aCoder
{    
    // Check if decoding a newer keyed coding archive
    if([aCoder allowsKeyedCoding]) {
	[aCoder encodeObject: noteReceivers forKey: @"MKInstrument_noteReceivers"];
    }
    else {
	[aCoder encodeObject: noteReceivers];	
    }
}

- (id) initWithCoder: (NSCoder *) aDecoder
{
    // Check if decoding a newer keyed coding archive
    if([aDecoder allowsKeyedCoding]) {
	[noteReceivers release];
	noteReceivers = [[aDecoder decodeObjectForKey: @"MKInstrument_noteReceivers"] retain];
    }
    else {
	if ([aDecoder versionForClassName: @"MKInstrument"] == VERSION2) 
	    noteReceivers = [[aDecoder decodeObject] retain];	
    }
    return self;
}

// Immediately stops playing any sounding notes. The default is to do nothing.
- allNotesOff
{
    return self;
}

@end

@implementation MKInstrument(Private)

- _realizeNote: (MKNote *) aNote fromNoteReceiver: (MKNoteReceiver *) aNoteReceiver
{
    if (!noteSeen) {
	// Note this will put the instrument on the after performance queue which implies it will be retained until
	// the end of the MKConductor's performance, which could be forever, i.e it won't actually be released.
	_afterPerfMsgPtr = [MKConductor _afterPerformanceSel: @selector(_afterPerformance) 
							  to: self
						    argCount: 0];
	[self firstNote: aNote];
	noteSeen = YES;
    }
    return [self realizeNote: aNote fromNoteReceiver: aNoteReceiver];
}

- _afterPerformance
  /* Sent by conductor at end of performance. Private */
{
    [self afterPerformance];
    noteSeen = NO;
    return self;
}

@end

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us