download MKFileWriter.m
Language: ObjectiveC
Copyright: (c) 1988-1992, NeXT Computer, Inc. (c) 1994 Stanford University (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT
LOC: 131
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
/*
  $Id: MKFileWriter.m,v 1.9 2005/05/11 02:13:30 leighsmith Exp $
  Defined In: The MusicKit
  HEADER FILES: MusicKit.h

  Description:
    A MKFileWriter is an MKInstrument that realizes MKNotes by writing them to
    a file on the disk. An abstract superclass, MKFileWriter
    provides common functionality and declares subclass responsibilities
    for the subclasses MKMidifileWriter and MKScorefileWriter.
    Note: In this release, MKMidifileWriter is not provided. Use a MKScore object
    to write a Midifile.
   
    A MKFileWriter is associated with a file, either by the
    file's name or with a file pointer.  If you assoicate
    a MKFileWriter with a file name (through the setFile:
    method) the object will take care of opening and closing
    the file for you:  the file is opened for writing when the
    object first receives the realizeNote: message
    and closed after the performance.  The
    file is overwritten each time it's opened.
   
    The setStream: method associates a MKFileWriter with a file
    pointer.  In this case, opening and closing the file
    is the responsibility of the application.  The MKFileWriter's
    file pointer is set to NULL after each performance.
   
    To design a subclass of MKFileWriter you must implement
    the method realizeNote:fromNoteReceiver:.
   
    Two other methods, initializeFile and finishFile, can
    be redefined in a subclass, although neither
    must be.  initializeFile is invoked
    just before the first MKNote is written to the
    file and should perform any special
    initialization such as writing a file header.
   
    finishFile is invoked after each performance
    and should perform any post-performance cleanup.
    The values returned by initializeFile and finishFile are ignored.

  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 
*/
/* 
Modification history prior to commit to CVS repository:

  10/26/89/daj - Added instance fileExtension method for binary scorefile
                 support.
  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.
  01/02/91/daj - Fixed bug in firstNote: involving file extensions

*/

#import "_musickit.h"
#import "_noteRecorder.h"
#import "_error.h"
#import "MKNote.h"

#import "MKFileWriter.h"
#import "InstrumentPrivate.h" /*sb: moved to here from within implementation...! */

@implementation MKFileWriter

#import "noteRecorderMethods.m"

#define VERSION2 2
#define VERSION3 3 /* Changed Nov 7, 1992 */

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

- (void) encodeWithCoder: (NSCoder *) aCoder
  /* TYPE: Archiving; Writes object to archive file.
     You never send this message directly.  
     Should be invoked with NXWriteRootObject(). 
     Invokes superclass write: method. Then archives timeUnit, filename, 
     and timeShift. 
     */
{
    [super encodeWithCoder: aCoder];
    NSAssert((sizeof(MKTimeUnit) == sizeof(int)), @"write: method error.");
    [aCoder encodeValuesOfObjCTypes: "i@d", &timeUnit, &filename, &timeShift]; //sb: was i*d
    [aCoder encodeValuesOfObjCTypes: "c", &compensatesDeltaT];
}

- (id) initWithCoder: (NSCoder *) aDecoder
  /* TYPE: Archiving; Reads object from archive file.
     You never send this message directly.  
     Should be invoked via NXReadObject(). 
     See write:. */
{
    int version;
    [super initWithCoder: aDecoder];
    version = [aDecoder versionForClassName: @"MKFileWriter"];
    if (version >= VERSION2)
        [aDecoder decodeValuesOfObjCTypes: "i@d", &timeUnit, &filename, &timeShift];//sb: was i*d
    if (version >= VERSION3)
      [aDecoder decodeValuesOfObjCTypes: "c", &compensatesDeltaT];
    return self;
}

- init     
  /* Does instance initialization. Sent by superclass on creation. 
     If subclass overrides this method, it must send [super init]
     before setting its own defaults. */
{
    [super init];
    timeUnit = MK_second;
    stream = nil;
    filename = nil;
    return self;
}

+ (NSString *) fileExtension
  /* Returns default file extension for files managed by the subclass. The
     default implementation returns NULL. Subclass may override this to
     specify a default file extension. */
{
    return nil;
}

- (NSString *) fileExtension
  /* Returns default file extension for files managed by the subclass. The
     default implementation just invokes the fileExtension method.
     A subclass can override this to provide a fileExtension on an instance-
     by-instance basis. For example MKScorefileWriter returns a different
     default extension for binary format scorefiles. */
{
    /* no need to retain/autorelease since class method should do this*/
    return [[self class] fileExtension]; 
}

- (void) dealloc
{
    [filename release];
    [super dealloc];
}

- setFile: (NSString *) aName
  /* TYPE: Modifying; Associates the receiver with file aName.
   * Associates the receiver with file aName. The string is copied.
   * The file is opened when the first MKNote is realized
   * (written to the file) and closed at the end of the
   * performance.
   *
   * sb: NOT TRUE. NSData method of doing things (as opposed to stream)
   * means that file is not written until all finished.
   *
   * It's illegal to invoke this method during a performance. Returns nil
   * in this case. Otherwise, returns the receiver.
   */
{
    if (noteSeen)
	return nil;
    [filename autorelease];
    filename = [aName retain];
    if (stream) {
        [stream autorelease];
        stream = nil;
    }
    return self;
}


- setStream: (NSMutableData *) aStream
  /* TYPE: Modifying; Associates the receiver with file pointer aStream.
   * Associates the receiver with the file pointer aStream.
   * You must open and close the file yourself.
   * Returns the receiver.
   * It's illegal to invoke this method during a performance. Returns nil
   * in this case. Otherwise, returns the receiver. 
   */
{
    if (noteSeen)
	return nil;
    [self setFile: nil];
    [stream autorelease];
    stream = [aStream retain];
    return self;
}

- (NSMutableData *) stream
  /* TYPE: Querying; Returns the receiver's file pointer.
   * Returns the file pointer associated with the receiver
   * or NULL if it isn't set.
   * Note that the receiver's file pointer is set to NULL 
   * after each performance.
   *
   * sb: now returns a NSMutableData object that can be written to.
   */
{
    return [[stream retain] autorelease];
}

- copyWithZone: (NSZone *) zone
  /* The returned value is a copy of the receiver, with a copy of the filename
     and stream set to NULL. */
{
    MKFileWriter *newObj = [super copyWithZone:zone];
    newObj->filename = [filename retain];
    newObj->stream = nil;
    return newObj;
}

- (NSString *) file
  /* TYPE: Querying; Returns the name set through setFile:.
   * If the file associated with the receiver was set through 
   * setFile:,
   * returns the file name, otherwise returns nil.
   */
{
    return [[filename retain] autorelease];
}

- (double) timeShift 
  /* TYPE: Accessing time; Returns the receiver's performance begin time.
   * Returns the receiver's performance begin time, as set through
   * setTimeShift:.
   */
{
	return timeShift;
}

- setTimeShift: (double) shift
  /* TYPE: Accessing time; Delays performance for shift beats.
   * Sets the begin time of the receiver;
   * the receiver's performance is delayed by shift beats.
   * Returns the receiver.
   * Illegal while the receiver is active. Returns nil in this case, else self.
   */
{	
    if ([self inPerformance])
      return nil;
    timeShift = shift;
    return self;
}		

- finishFile
  /* TYPE: Accessing; Cleans up after a performance.
   * This can be overridden by a subclass to perform any cleanup
   * needed after a performance.  You shouldn't 
   * close the file pointer as part of this method.
   * The return value is ignored; the default returns the receiver.
   *
   * You never send the finishFile message directly to a
   * MKFileWriter; it's invoked automatically after each performance.
   */
{
    return self;
}

- initializeFile
  /* TYPE: Accessing; Prepares the file for writing.
   * This can be overriden by a subclass to perform
   * file initialization, such as writing a file header..
   * The return value is ignored; the default returns the receiver.
   *
   * You never send the initializeFile message 
   * directly to a MKFileWriter; it's invoked when the first realizeNote:
   * message is received.
   */
{
    return self;
}

- firstNote: (MKNote *) aNote
  /* You never send this message.  Overrides superclass method to initialize
     file. */
{
    if (filename) {
        [stream autorelease]; /* get rid of old one */
        stream = [[NSMutableData alloc] initWithCapacity: 0];
    }
/* sb: now defers writing and opening until finished */
/*      _MKOpenFileStream(filename,&_fd,NX_WRITEONLY,
				 [self fileExtension],YES);
 */
//    if (!stream) 
//      return nil;
    [self initializeFile];
    return self;
}

- afterPerformance
    /* You never send this message. Overrides superclass method to finish up */
{
    [self finishFile];
    if (filename) {
        if (![stream writeToFile:[filename stringByAppendingPathExtension:[self fileExtension]] atomically:YES])
            MKErrorCode(MK_cantCloseFileErr,filename);
/*
	if (close(_fd) == -1)
	  MKErrorCode(MK_cantCloseFileErr,filename);
 */
    }
    [stream release];
    stream = nil;
    return self;
}

@end

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