Join/Login

Business Software

Open Source Software

For Vendors

Blog

About

More

Articles

Create

SourceForge Podcast

Site Documentation

Subscribe to our Newsletter

Support Request

For Vendors Help Create Join Login

Business Software

Open Source Software

SourceForge Podcast

Resources

Articles

Case Studies

Blog

Menu

Help

Create

Join

Login

Home

Browse

PlayerPro music editor and SoundTracker

Code

PlayerPro music editor and SoundTracker Code

Play SoundTracker media on your computer.

Brought to you by: madd_the_sane, rossetantoine, sandrobilbeisi

Summary

Files

Reviews

Support

Mailing Lists

Tickets ▾

Bugs

Support Requests

Patches

Feature Requests

News

Discussion

Code

Menu ▾ ▴

Browse Commits

Fork

Merge Requests 0

Branches

master

Classic

NSPreferences

PP5-CPlusPlusRewrite

PPPcmd

PlayerPRO5

SDLAudio

XPCMIDI

XPCMIDI-noObjC

audioToolbox-plugrewrite

More Branches

Tags

5.10-Developer-Preview-1

5.10-Developer-Preview-2

5.10-Developer-Preview-3

5.10-Developer-Preview-4

5.10-Developer-Preview-5

5.10-Developer-Preview-6

5.10-Developer-Preview-7

PP6-pre_split

PPlayer-1.0

PPlayer-1.0.1

More Tags

[b65532]: / PlayerPROKit / PPMusicObject.m  Maximize  Restore  History

Download this file

909 lines (761 with data), 24.6 kB

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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908

//

// PPMusicObject.m

// PPMacho

//

// Created by C.W. Betts on 12/1/12.

//

//

#import <PlayerPROKit/PlayerPROKit-Swift.h>

#import "PPMusicObject.h"

#import "PPLibrary_PPKPrivate.h"

#import "PPDriver.h"

#import "PPDriver_Private.h"

#import "PPMusicObject_PPKPrivate.h"

#include <PlayerPROCore/PlayerPROCore.h>

#include <PlayerPROCore/RDriverInt.h>

#include "PPByteswap.h"

#import "PPInstrumentObject.h"

#import "PPInstrumentObject_PPKPrivate.h"

#import "PPPatternObject.h"

#import "PPErrors.h"

static NSString *utf8OrMacRoman(const char *text) NS_RETURNS_RETAINED;

static NSString *utf8OrMacRoman(const char *text)

{

if (memcmp(text, "\xEF\xBB\xBF", 3) == 0) {

NSString *uniStr = [[NSString alloc] initWithUTF8String:text];

if (uniStr) {

return uniStr;

}

}

return [[NSString alloc] initWithCString:text encoding:NSMacOSRomanStringEncoding];

}

static MADMusic *DeepCopyMusic(MADMusic* oldMus)

{

if (!oldMus) {

return NULL;

}

MADMusic *toreturn = calloc(sizeof(MADMusic), 1);

if (toreturn == NULL) {

return NULL;

}

memcpy(toreturn, oldMus, sizeof(MADMusic));

short i, x;

size_t inOutCount, OffSetToSample = 0;

PatHeader tempPatHeader;

/**** HEADER ****/

inOutCount = sizeof(MADSpec);

toreturn->header = (MADSpec*)calloc(inOutCount, 1);

memcpy(toreturn->header, oldMus->header, inOutCount);

OffSetToSample += inOutCount;

/**** PARTITION ****/

for (int iTmp = 0; iTmp < MAXPATTERN - toreturn->header->numPat; iTmp++) {

size_t i = iTmp + toreturn->header->numPat;

toreturn->partition[i] = NULL;

}

for (i = 0; i < toreturn->header->numPat; i++) {

/** Lecture du header de la partition **/

inOutCount = sizeof(PatHeader);

memcpy(&tempPatHeader, &oldMus->partition[i]->header, inOutCount);

inOutCount = sizeof(PatHeader) + toreturn->header->numChn * tempPatHeader.size * sizeof(Cmd);

toreturn->partition[i] = (PatData*)malloc(inOutCount);

if (toreturn->partition[i] == NULL) {

for (x = 0; x < i; x++) {

if (toreturn->partition[x] != NULL)

free(toreturn->partition[x]);

}

free(toreturn->header);

free(toreturn);

return NULL;

}

memcpy(toreturn->partition[i], oldMus->partition[i], inOutCount);

OffSetToSample += inOutCount;

}

/**** INSTRUMENTS ****/

toreturn->fid = (InstrData*)calloc(sizeof(InstrData), MAXINSTRU);

if (!toreturn->fid) {

for (x = 0; x < toreturn->header->numPat; x++) {

if (toreturn->partition[x] != NULL)

free(toreturn->partition[x]);

}

free(toreturn->header);

free(toreturn);

return NULL;

}

inOutCount = sizeof(InstrData) * toreturn->header->numInstru;

memcpy(toreturn->fid, oldMus->fid, inOutCount);

OffSetToSample += inOutCount;

for (i = toreturn->header->numInstru - 1; i >= 0 ; i--) {

InstrData *curIns = &toreturn->fid[i];

if (i != curIns->no) {

toreturn->fid[curIns->no] = *curIns;

MADResetInstrument(curIns);

}

}

toreturn->header->numInstru = MAXINSTRU;

/**** SAMPLES ****/

toreturn->sample = (sData**) calloc(sizeof(sData*), MAXINSTRU * MAXSAMPLE);

if (!toreturn->sample) {

for (x = 0; x < MAXINSTRU ; x++)

MADKillInstrument(toreturn, x);

for (x = 0; x < toreturn->header->numPat; x++) {

if (toreturn->partition[x] != NULL)

free(toreturn->partition[x]);

}

free(toreturn->header);

free(toreturn);

return NULL;

}

for (i = 0; i < MAXINSTRU ; i++) {

for (x = 0; x < toreturn->fid[i].numSamples ; x++) {

sData *curData;

sData *oldData = oldMus->sample[i * MAXSAMPLE + x];

// ** Read Sample header **

curData = toreturn->sample[i * MAXSAMPLE + x] = (sData*)malloc(sizeof(sData));

if (curData == NULL) {

for (x = 0; x < MAXINSTRU ; x++)

MADKillInstrument(toreturn, x);

for (x = 0; x < toreturn->header->numPat; x++) {

if (toreturn->partition[x] != NULL)

free(toreturn->partition[x]);

}

free(toreturn->header);

free(toreturn);

return NULL;

}

inOutCount = sizeof(sData);

memcpy(curData, oldMus, inOutCount);

OffSetToSample += inOutCount;

// ** Read Sample DATA

curData->data = (char*)malloc(curData->size);

if (curData->data == NULL) {

for (x = 0; x < MAXINSTRU ; x++)

MADKillInstrument(toreturn, x);

for (x = 0; x < toreturn->header->numPat; x++) {

if (toreturn->partition[x] != NULL)

free(toreturn->partition[x]);

}

free(toreturn->header);

free(toreturn);

return NULL;

}

inOutCount = curData->size;

memcpy(curData->data, oldData->data, inOutCount);

OffSetToSample += inOutCount;

}

}

for (int ixi = 0; ixi < MAXINSTRU; ixi++) {

toreturn->fid[ixi].firstSample = ixi * MAXSAMPLE;

}

// EFFECTS *** *** *** *** *** *** *** *** *** *** *** ***

{

short alpha;

toreturn->sets = (FXSets*)calloc(sizeof(FXSets), MAXTRACK);

alpha = 0;

for (i = 0; i < 10 ; i++) { // Global Effects

if (toreturn->header->globalEffect[i]) {

inOutCount = sizeof(FXSets);

toreturn->sets[alpha] = oldMus->sets[alpha];

OffSetToSample += inOutCount;

alpha++;

}

}

for (i = 0; i < toreturn->header->numChn ; i++) { // Channel Effects

for (x = 0; x < 4; x++) {

if (toreturn->header->chanEffect[i][x]) {

inOutCount = sizeof(FXSets);

toreturn->sets[alpha] = oldMus->sets[alpha];

OffSetToSample += inOutCount;

alpha++;

}

}

}

}

toreturn->header->MAD = 'MADK';

return toreturn;

}

@interface PPMusicObject ()

{

NSMutableArray<PPInstrumentObject*> *_instruments;

}

@property (readwrite, strong, nonatomic) NSMutableArray *patterns;

@property (readwrite, strong, nonatomic) NSMutableArray *buses;

@end

@implementation PPMusicObject

@synthesize information = madInfo;

@synthesize attachedDriver;

@synthesize _currentMusic = currentMusic;

@synthesize title = internalFileName;

- (BOOL)usesLinearPitchTable

{

return currentMusic->header->XMLinear;

}

- (void)setUsesLinearPitchTable:(BOOL)usesLinearPitchTable

{

currentMusic->header->XMLinear = usesLinearPitchTable;

}

- (BOOL)limitPitchToMODTable

{

return currentMusic->header->MODMode;

}

- (void)setLimitPitchToMODTable:(BOOL)limitPitchToMODTable

{

currentMusic->header->MODMode = limitPitchToMODTable;

}

- (BOOL)showsCopyright

{

return currentMusic->header->showCopyright;

}

- (void)setShowsCopyright:(BOOL)showsCopyright

{

currentMusic->header->showCopyright = showsCopyright;

}

- (int)newPitch

{

return currentMusic->header->EPitch;

}

- (void)setNewPitch:(int)newPitch

{

currentMusic->header->EPitch = newPitch;

}

- (int)newSpeed

{

return currentMusic->header->ESpeed;

}

- (void)setNewSpeed:(int)newSpeed

{

currentMusic->header->ESpeed = newSpeed;

}

- (MADByte)generalPitch

{

return currentMusic->header->generalPitch;

}

- (void)setGeneralPitch:(MADByte)generalPitch

{

currentMusic->header->generalPitch = generalPitch;

}

- (MADByte)generalSpeed

{

return currentMusic->header->generalSpeed;

}

- (void)setGeneralSpeed:(MADByte)generalSpeed

{

currentMusic->header->generalSpeed = generalSpeed;

}

- (MADByte)generalVolume

{

return currentMusic->header->generalVol;

}

- (void)setGeneralVolume:(MADByte)generalVolume

{

currentMusic->header->generalVol = generalVolume;

}

- (MADErr)exportInstrumentListToURL:(NSURL*)outURL

{

NSFileHandle *outData = [NSFileHandle fileHandleForWritingToURL:outURL error:nil];

if (!outData) {

return MADNeedMemory;

}

int i, x;

{

InstrData *tempInstrData = calloc(sizeof(InstrData), MAXINSTRU);

if (!tempInstrData) {

return MADNeedMemory;

}

memcpy(tempInstrData, [self internalMadMusicStruct]->fid, sizeof(InstrData) * MAXINSTRU);

for (int x = 0; x < MAXINSTRU; x++) {

ByteSwapInstrData(&tempInstrData[x]);

}

[outData writeData:[[NSData alloc] initWithBytesNoCopy:tempInstrData length:sizeof(InstrData)* MAXINSTRU]];

}

for (i = 0; i < MAXINSTRU ; i++) {

for (x = 0; x < [self internalMadMusicStruct]->fid[i].numSamples ; x++) {

sData tempData, *curData = [self internalMadMusicStruct]->sample[i * MAXSAMPLE + x];

sData32 writeData;

memcpy(&tempData, curData, sizeof(sData));

ByteSwapsData(&tempData);

memcpy(&writeData, &tempData, sizeof(sData32));

writeData.data = 0;

[outData writeData:[[NSData alloc] initWithBytes:&writeData length:sizeof(sData32)]];

#ifdef __LITTLE_ENDIAN__

{

Ptr dataData = malloc(curData->size);

if (!dataData)

return MADNeedMemory;

memcpy(dataData, curData->data, curData->size);

if (curData->amp == 16) {

short *shortPtr = (short*) dataData;

for (int y = 0; y < curData->size / 2; y++) {

MADBE16(&shortPtr[y]);

}

}

[outData writeData:[[NSData alloc] initWithBytesNoCopy:dataData length:curData->size]];

}

#else

[outdata writeData:[[NSData alloc] initWithBytes:curData->data length: curData->size]];

#endif

}

}

return MADNoErr;

}

- (NSArray *)instruments

{

if (!_instruments) {

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:MAXINSTRU];

for (NSInteger i = 0; i < MAXINSTRU; i++) {

PPInstrumentObject *immIns = [[PPInstrumentObject alloc] initWithMusicStruct:currentMusic atIndex:i];

[array addObject:immIns];

}

_instruments = array;

}

return _instruments;

}

- (void)setInstruments:(NSArray<PPInstrumentObject*> *)instruments

{

//TODO: write back to struct

_instruments = [instruments mutableCopy];

}

+ (MADErr)info:(MADInfoRec*)theInfo fromTrackerAtURL:(NSURL*)thURL usingLibrary:(PPLibrary*)theLib

{

char filetype[5];

MADErr theErr = MADNoErr;

if (!theInfo || !thURL || !theLib) {

return MADParametersErr;

}

if ((theErr = MADMusicIdentifyCFURL(theLib.theLibrary, filetype, (__bridge CFURLRef)(thURL))) != MADNoErr)

return theErr;

return MADMusicInfoCFURL(theLib.theLibrary, filetype, (__bridge CFURLRef)(thURL), theInfo);

}

- (NSString *)title

{

if (!internalFileName) {

internalFileName = [[NSString alloc] initWithCString:currentMusic->header->name encoding:NSMacOSRomanStringEncoding];

}

if (!internalFileName) {

internalFileName = @"";

}

return internalFileName;

}

- (void)setTitle:(NSString*)newInfo

{

internalFileName = nil;

NSData *outMacRoman = [newInfo dataUsingEncoding:NSMacOSRomanStringEncoding allowLossyConversion:YES];

if (!outMacRoman || outMacRoman.length == 0) {

memset(currentMusic->header->name, 0, sizeof(currentMusic->header->name));

} else {

char fileNameInt[32] = {0};

[outMacRoman getBytes:fileNameInt length:MIN(outMacRoman.length, sizeof(fileNameInt) - 1)];

strlcpy(currentMusic->header->name, fileNameInt, sizeof(currentMusic->header->name));

}

}

- (void)setInformation:(NSString*)newInfo

{

madInfo = nil;

// Prefer Mac OS Roman for backwards compatibility.

NSData *outMacRoman = [newInfo dataUsingEncoding:NSMacOSRomanStringEncoding allowLossyConversion:NO];

if (!outMacRoman || outMacRoman.length == 0) {

NSMutableData *utf8BOM = [NSMutableData dataWithBytes:"\xEF\xBB\xBF" length:3];

NSData *outUTF8 = [[newInfo precomposedStringWithCanonicalMapping] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

[utf8BOM appendData:outUTF8];

outMacRoman = utf8BOM;

}

char fileNameInt[239] = {0};

[outMacRoman getBytes:fileNameInt length:MIN(outMacRoman.length, sizeof(fileNameInt) - 1)];

strlcpy(currentMusic->header->infos, fileNameInt, sizeof(currentMusic->header->infos));

}

- (NSString*)information

{

if (!madInfo) {

madInfo = utf8OrMacRoman(currentMusic->header->infos);

}

if (!madInfo) {

madInfo = @"";

}

return madInfo;

}

- (instancetype)initWithMusicStruct:(MADMusic*)theStruct

{

return [self initWithMusicStruct:theStruct copy:YES];

}

- (instancetype)initWithMusicStruct:(MADMusic*)theStruct copy:(BOOL)copyData

{

if (self = [super init]) {

if (copyData) {

currentMusic = DeepCopyMusic(theStruct);

} else {

currentMusic = theStruct;

}

}

return self;

}

- (instancetype)initWithURL:(NSURL *)url error:(out NSError* __autoreleasing*)error

{

if (self = [super init]) {

MADErr iErr = MADLoadMusicCFURLFile(NULL, &currentMusic, "MADK", (__bridge CFURLRef)url);

if (iErr != MADNoErr) {

if (error) {

*error = PPCreateErrorFromMADErrorType(iErr);

}

return nil;

}

}

return self;

}

- (instancetype)initWithPath:(NSString *)url error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:[NSURL fileURLWithPath:url] error:error];

}

- (instancetype)initWithURL:(NSURL *)url library:(PPLibrary *)theLib error:(out NSError* __autoreleasing*)error

{

char type[5];

MADErr iErr = MADMusicIdentifyCFURL(theLib.theLibrary, type, (__bridge CFURLRef)(url));

if (iErr != MADNoErr) {

if (error) {

*error = PPCreateErrorFromMADErrorType(iErr);

}

return nil;

}

return [self initWithURL:url type:type library:theLib error:error];

}

- (instancetype)initWithPath:(NSString *)path type:(in const char*)type library:(PPLibrary *)theLib error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:[NSURL fileURLWithPath:path] type:type library:theLib error:error];

}

- (instancetype)initWithURL:(NSURL *)url stringType:(NSString*)type library:(PPLibrary *)theLib error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:url type:[type cStringUsingEncoding:NSMacOSRomanStringEncoding] library:theLib error:error];

}

- (instancetype)initWithPath:(NSString *)url stringType:(NSString*)type library:(PPLibrary *)theLib error:(out NSError* __autoreleasing*)error

{

return [self initWithPath:url type:[type cStringUsingEncoding:NSMacOSRomanStringEncoding] library:theLib error:error];

}

- (instancetype)initWithURL:(NSURL *)url type:(in const char*)type library:(PPLibrary *)theLib error:(out NSError* __autoreleasing*)error

{

if (self = [super init]) {

MADErr iErr = MADLoadMusicCFURLFile(theLib.theLibrary, &currentMusic, (char*)type, (__bridge CFURLRef)(url));

if (iErr != MADNoErr) {

if (error) {

*error = PPCreateErrorFromMADErrorType(iErr);

}

return nil;

}

}

return self;

}

- (instancetype)initWithPath:(NSString *)url library:(PPLibrary *)theLib error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:[NSURL fileURLWithPath:url] library:theLib error:error];

}

- (instancetype)init

{

return [self initWithMusicStruct:CreateFreeMADK() copy:NO];

}

- (instancetype)initWithURL:(NSURL *)url stringType:(NSString*)type driver:(PPDriver *)theDriv error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:url type:[type cStringUsingEncoding:NSMacOSRomanStringEncoding] driver:theDriv error:error];

}

- (instancetype)initWithPath:(NSString *)url stringType:(NSString*)type driver:(PPDriver *)theDriv error:(out NSError* __autoreleasing*)error

{

return [self initWithPath:url type:[type cStringUsingEncoding:NSMacOSRomanStringEncoding] driver:theDriv error:error];

}

- (instancetype)initWithURL:(NSURL *)url type:(in const char*)type driver:(PPDriver *)theLib error:(out NSError* __autoreleasing*)error

{

if (self = [self initWithURL:url type:type library:theLib.theLibrary error:error]) {

[self attachToDriver:theLib];

}

return self;

}

- (instancetype)initWithURL:(NSURL *)url driver:(PPDriver *)theLib error:(out NSError* __autoreleasing*)error

{

if (self = [self initWithURL:url library:theLib.theLibrary error:error]) {

[self attachToDriver:theLib];

}

return self;

}

- (instancetype)initWithPath:(NSString *)url type:(in const char*)type driver:(PPDriver *)theLib error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:[NSURL fileURLWithPath:url] type:type driver:theLib error:error];

}

- (instancetype)initWithPath:(NSString *)url driver:(PPDriver *)theLib error:(out NSError* __autoreleasing*)error

{

return [self initWithURL:[NSURL fileURLWithPath:url] driver:theLib error:error];

}

- (void)attachToDriver:(PPDriver *)theDriv

{

self.attachedDriver = theDriv;

theDriv.currentMusic = self;

}

- (void)dealloc

{

if (self.attachedDriver && self.attachedDriver.currentMusic == self) {

self.attachedDriver.currentMusic = nil;

}

if (currentMusic) {

MADDisposeMusic(&currentMusic, NULL);

currentMusic = NULL;

}

}

- (BOOL)saveMusicToURL:(NSURL *)tosave error:(out NSError**)error;

{

return [self saveMusicToURL:tosave compress:NO error:error];

}

- (BOOL)saveMusicToURL:(NSURL *)tosave compress:(BOOL)mad1Comp error:(out NSError**)error;

{

MADErr retErr;

if ((retErr = MADMusicSaveCFURL(currentMusic, (__bridge CFURLRef)tosave, mad1Comp)) == MADNoErr) {

currentMusic->hasChanged = false;

}

if (retErr != MADNoErr && error) {

*error = PPCreateErrorFromMADErrorType(retErr);

}

return retErr == MADNoErr;

}

- (BOOL)exportMusicToURL:(NSURL *)tosave format:(NSString*)form library:(PPLibrary*)otherLib error:(out NSError**)error;

{

MADErr theErr = MADNoErr;

if ([form isEqualToString:@"MADK"]) {

return [self saveMusicToURL:tosave error:error];

} else {

theErr = MADMusicExportCFURL(otherLib.theLibrary, self._currentMusic, (char*)[form cStringUsingEncoding:NSMacOSRomanStringEncoding], (__bridge CFURLRef)tosave);

}

if (error) {

*error = PPCreateErrorFromMADErrorType(theErr);

}

return theErr == MADNoErr;

}

- (MADMusic *)copyMadMusicStruct

{

return DeepCopyMusic(currentMusic);

}

- (MADMusic *)internalMadMusicStruct

{

return currentMusic;

}

- (id)copyWithZone:(NSZone *)zone

{

PPMusicObject *copyWrap = [[PPMusicObject alloc] initWithMusicStruct:currentMusic];

copyWrap.information = self.information;

copyWrap.title = self.title;

//copyWrap.instruments = [[NSMutableArray alloc] initWithArray:_instruments copyItems:YES];

//copyWrap.patterns = [[NSMutableArray alloc] initWithArray:_patterns copyItems:YES];

return copyWrap;

}

#if 0

- (void)setUpObjCStructures

{

int i;

self.internalFileName = [NSString stringWithCString:currentMusic->header->name encoding:NSMacOSRomanStringEncoding];

self.madInfo = [NSString stringWithCString:currentMusic->header->infos encoding:NSMacOSRomanStringEncoding];

self.madType = currentMusic->header->MAD;

self.instruments = [[NSMutableArray alloc] initWithCapacity:MAXINSTRU];

for (i = 0; i < MAXINSTRU; i++) {

PPInstrumentObject *insObj = [[PPInstrumentObject alloc] initWithMusic:self instrumentIndex:i];

[self.instruments addObject:insObj];

}

self.patterns = [[NSMutableArray alloc] initWithCapacity:currentMusic->header->numPat];

for (i = 0; i < currentMusic->header->numPat; i++) {

PPPatternObject *obj = [[PPPatternObject alloc] initWithMusic:self patternAtIndex:i];

[self.patterns addObject:obj];

}

}

#endif

- (NSArray *)sDatas {

NSMutableArray *ourArray = [[NSMutableArray alloc] initWithCapacity:MAXINSTRU * MAXSAMPLE];

for (PPInstrumentObject *instrument in self.instruments) {

for (PPSampleObject *sample in instrument.samples) {

[ourArray addObject:sample];

}

}

return [ourArray copy];

}

- (BOOL)addInstrument:(PPInstrumentObject*)theIns

{

if ([[self instruments] count] >= MAXINSTRU) {

return NO;

}

[self addInstrumentObject:theIns];

return YES;

}

- (BOOL)importInstrumentListFromURL:(NSURL *)insURL error:(out NSError *__autoreleasing*)theErr

{

NSData *fileData = [NSData dataWithContentsOfURL:insURL];

if (!fileData) {

if (theErr) {

*theErr = PPCreateErrorFromMADErrorType(MADReadingErr);

}

return NO;

}

int x, i;

long inOutCount, filePos = 0;

InstrData *tempInstrData;

{

tempInstrData = calloc(sizeof(InstrData), MAXINSTRU);

if (tempInstrData == NULL) {

if (theErr) {

*theErr = PPCreateErrorFromMADErrorType(MADIncompatibleFile);

}

return NO;

}

// **** HEADER ***

inOutCount = sizeof(InstrData) * MAXINSTRU;

if ([fileData length] <= inOutCount) {

if (theErr) {

*theErr = PPCreateErrorFromMADErrorType(MADIncompatibleFile);

}

free(tempInstrData);

return NO;

}

[fileData getBytes:tempInstrData range:NSMakeRange(filePos, inOutCount)];

for (int x = 0; x < MAXINSTRU; x++) {

ByteSwapInstrData(&tempInstrData[x]);

}

filePos += inOutCount;

}

//Clean up old instruments

sData **tmpsData = calloc(sizeof(sData*), MAXINSTRU * MAXSAMPLE);

// **** INSTRUMENTS ***

for (i = 0; i < MAXINSTRU ; i++) {

for (x = 0; x < tempInstrData[i].numSamples ; x++) {

sData *curData;

// ** Read Sample header **

curData = tmpsData[i * MAXSAMPLE + x] = (sData*)malloc(sizeof(sData));

if (curData == NULL) {

if (theErr) {

*theErr = PPCreateErrorFromMADErrorType(MADNeedMemory);

}

free(tempInstrData);

for (int x = 0; x < MAXSAMPLE * MAXINSTRU; x++) {

if (tmpsData[x]) {

if (tmpsData[x]->data) {

free(tmpsData[x]->data);

}

free(tmpsData[x]);

}

}

free(tmpsData);

return NO;

}

inOutCount = sizeof(sData32);

[fileData getBytes:curData range:NSMakeRange(filePos, inOutCount)];

ByteSwapsData(curData);

filePos += inOutCount;

// ** Read Sample DATA

curData->data = malloc(curData->size);

if (curData->data == NULL) {

if (theErr) {

*theErr = PPCreateErrorFromMADErrorType(MADNeedMemory);

}

free(tempInstrData);

for (int x = 0; x < MAXSAMPLE * MAXINSTRU; x++) {

if (tmpsData[x]) {

if (tmpsData[x]->data) {

free(tmpsData[x]->data);

}

free(tmpsData[x]);

}

}

free(tmpsData);

return NO;

}

inOutCount = curData->size;

[fileData getBytes:curData->data range:NSMakeRange(filePos, inOutCount)];

filePos += inOutCount;

if (curData->amp == 16) {

short *shortPtr = (short*)curData->data;

for (int y = 0; y < inOutCount / 2; y++) {

MADBE16(&shortPtr[y]);

}

}

}

}

// *********************

for (x = 0; x < MAXINSTRU ; x++) MADKillInstrument(currentMusic, x);

memcpy(currentMusic->fid, tempInstrData, inOutCount);

free(tempInstrData);

for (int x = 0; x < MAXSAMPLE * MAXINSTRU; x++) {

if (currentMusic->sample[x]) {

if (currentMusic->sample[x]->data) {

free(currentMusic->sample[x]->data);

}

free(currentMusic->sample[x]);

}

}

free(currentMusic->sample);

currentMusic->sample = tmpsData;

for (i = 0; i < MAXINSTRU; i++) {

PPInstrumentObject *insObj = [[PPInstrumentObject alloc] initWithMusic:self instrumentIndex:i];

[self replaceObjectInInstrumentsAtIndex:i withObject:insObj];

}

return YES;

}

#pragma mark KVO/KVC helpers

+ (NSSet*)keyPathsForValuesAffectingInternalMadMusicStruct

{

return [NSSet setWithObjects:@"title", @"information", @"usesLinearPitcchTable", @"limitPitchToMODTable", @"showsCopyright", @"newPitch", @"newSpeed", @"generalPitch", @"generalSpeed", @"generalVolume", @"instruments", @"patterns", @"buses", nil];

}

+ (NSSet*)keyPathsForValuesAffectingSDatas

{

return [NSSet setWithObject:@"instruments"];

}

- (void)addInstrumentObject:(PPInstrumentObject *)object

{

NSInteger indexOfAddedInstrument = -1;

for (NSInteger i = 0; i > MAXINSTRU; i++) {

if ([_instruments[i] isBlankInstrument]) {

indexOfAddedInstrument = i;

break;

}

}

if (indexOfAddedInstrument == -1) {

return;

}

NSIndexSet *ourIdxSet = [NSIndexSet indexSetWithIndex:indexOfAddedInstrument];

[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:ourIdxSet forKey:@"instruments"];

_instruments[indexOfAddedInstrument] = [object copy];

_instruments[indexOfAddedInstrument].number = indexOfAddedInstrument * MAXSAMPLE;

[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:ourIdxSet forKey:@"instruments"];

}

- (void)replaceObjectInInstrumentsAtIndex:(NSInteger)index withObject:(PPInstrumentObject *)object

{

NSParameterAssert(index < MAXINSTRU);

_instruments[index] = object;

}

- (NSInteger)countOfInstruments

{

return [_instruments count];

}

- (PPInstrumentObject*)instrumentsObjectAtIndex:(NSInteger)idx

{

NSParameterAssert(idx < MAXINSTRU);

return _instruments[idx];

}

- (PPInstrumentObject*)instrumentObjectAtIndex:(NSInteger)idx

{

return [self instrumentsObjectAtIndex:idx];

}

- (void)clearInstrumentsAtIndexes:(NSIndexSet *)indexes

{

[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"instruments"];

NSUInteger currentIndex = [indexes firstIndex];

while (currentIndex != NSNotFound) {

[(PPInstrumentObject*)_instruments[currentIndex] resetInstrument];

currentIndex = [indexes indexGreaterThanIndex:currentIndex];

}

[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"instruments"];

}

- (void)clearInstrumentObjectAtIndex:(NSInteger)index

{

[self clearInstrumentsAtIndexes:[NSIndexSet indexSetWithIndex:index]];

}

- (NSInteger)countOfPatterns

{

return [_patterns count];

}

- (NSInteger)lengthOfPartitions

{

return currentMusic->header->numPointers;

}

- (NSInteger)totalTracks

{

return currentMusic->header->numChn;

}

@end

SourceForge

Create a Project

Open Source Software

Business Software

Top Downloaded Projects

Company

About

Team

SourceForge Headquarters
225 Broadway Suite 1600
San Diego, CA 92101
+1 (858) 422-6466

Resources

Support

Site Documentation

Site Status

SourceForge Reviews

© 2025 Slashdot Media. All Rights Reserved.

Terms Privacy Opt Out Advertise

×
Want the latest updates on software, tech news, and AI?

Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.

Submit