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 / PPPatternObject.m  Maximize  Restore  History

Download this file

552 lines (462 with data), 14.0 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

//

// PPPatternObject.m

// PPMacho

//

// Created by C.W. Betts on 1/17/13.

//

//

#include <PlayerPROCore/PlayerPROCore.h>

#include <PlayerPROCore/MADPlug.h>

#import <PlayerPROKit/PlayerPROKit-Swift.h>

#import "PPPatternObject.h"

#import "PPPatternObject_PPKPrivate.h"

#import "PPPatternObject_PcmdHandling.h"

#import "PPMusicObject_PPKPrivate.h"

#import "PPSampleObject.h"

#if TARGET_OS_OSX

#import "PPPasteboardHandling.h"

#endif

#define kPPPatternName @"PlayerPROKit Pattern Name"

#define kPPPatternCommands @"PlayerPROKit Pattern Commands"

#define kPPPatternIndex @"PlayerPROKit Pattern Index"

static inline void SwapPcmd(Pcmd *toswap)

{

if (!toswap) {

return;

}

MADBE32(&toswap->structSize);

MADBE16(&toswap->length);

MADBE16(&toswap->posStart);

MADBE16(&toswap->tracks);

MADBE16(&toswap->trackStart);

}

static inline Pcmd* CopyPcmd(const Pcmd* tocopy)

{

size_t structlen = tocopy->structSize;

Pcmd *toret = calloc(structlen, 1);

memcpy(toret, tocopy, structlen);

return toret;

}

@interface PPPatternObject ()

- (instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@end

@implementation PPPatternObject

{

@package

PatHeader patternHeader;

PatData *patternData;

}

- (PPMadCommandObject *)objectAtIndexedSubscript:(NSInteger)index1;

{

return commands[index1];

}

- (NSInteger)lengthOfCommands

{

return commands.count;

}

#if TARGET_OS_OSX

NSString * const kPPKPatternPasteboardUTI = @"net.sourceforge.playerpro.pattern";

static NSArray *UTIArray;

static dispatch_once_t initUTIOnceToken;

static const dispatch_block_t initUTIArray = ^{

UTIArray = @[kPPKPatternPasteboardUTI];

};

+ (NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard

{

dispatch_once(&initUTIOnceToken, initUTIArray);

return UTIArray;

}

- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard

{

dispatch_once(&initUTIOnceToken, initUTIArray);

return UTIArray;

}

- (id)pasteboardPropertyListForType:(NSString *)type

{

if ([type isEqualToString:kPPKPatternPasteboardUTI]) {

return [NSKeyedArchiver archivedDataWithRootObject:self];

} else {

return nil;

}

}

+ (NSPasteboardReadingOptions)readingOptionsForType:(NSString *)type pasteboard:(NSPasteboard *)pasteboard

{

if ([type isEqualToString:kPPKPatternPasteboardUTI])

return NSPasteboardReadingAsKeyedArchive;

else

return NSPasteboardReadingAsData;

}

#endif

@synthesize commands;

@synthesize index;

@synthesize patternHeader;

@synthesize patternName = _patternName;

- (NSString*)patternName

{

if (!_patternName) {

_patternName = [[NSString alloc] initWithCString:patternHeader.name encoding:NSMacOSRomanStringEncoding];

if (!_patternName) {

memset(patternHeader.name, 0, sizeof(patternHeader.name));

_patternName = @"";

}

}

return _patternName;

}

- (void)setPatternName:(NSString *)patternName

{

char theName[32] = {0};

NSData *tmpCStr = [patternName dataUsingEncoding:NSMacOSRomanStringEncoding allowLossyConversion:YES];

NSInteger cStrLen = [tmpCStr length];

if (cStrLen > sizeof(theName) - 1) {

cStrLen = sizeof(theName) - 1;

}

[tmpCStr getBytes:theName length:cStrLen];

strlcpy(patternHeader.name, theName, sizeof(patternHeader.name));

_patternName = [[NSString alloc] initWithCString:patternHeader.name encoding:NSMacOSRomanStringEncoding];

}

- (PatHeader)patternHeader

{

return patternHeader;

}

- (int)patternSize

{

return patternHeader.size;

}

- (void)setPatternSize:(int)patternSize

{

//TODO: more work here!

patternHeader.size = patternSize;

}

- (instancetype)initWithMusic:(PPMusicObject *)mus

{

if (self = [super init]) {

if (!mus) {

return nil;

}

//TODO: add watchers for changes in number of channels

index = -1;

_musicWrapper = mus;

NSInteger size = _musicWrapper._currentMusic->header->numChn * 64;

self.commands = [[NSMutableArray alloc] initWithCapacity:size];

for (int i = 0; i < size; i++) {

PPMadCommandObject *tmpObj = [[PPMadCommandObject alloc] init];

[self.commands addObject:tmpObj];

}

self.patternName = @"";

}

return self;

}

- (instancetype)initWithMusic:(PPMusicObject *)mus patternAtIndex:(short)ptnIdx

{

if (self = [self initWithMusic:mus]) {

patternHeader = _musicWrapper._currentMusic->partition[ptnIdx]->header;

index = ptnIdx;

NSInteger size = _musicWrapper._currentMusic->header->numChn * patternHeader.size;

self.commands = [[NSMutableArray alloc] initWithCapacity:size];

for (int i = 0; i < size; i++) {

PPMadCommandObject *tmpObj = [[PPMadCommandObject alloc] initWithCmdPtr:&_musicWrapper._currentMusic->partition[ptnIdx]->Cmds[i]];

[self.commands addObject:tmpObj];

}

self.patternName = [[NSString alloc] initWithCString:patternHeader.name encoding:NSMacOSRomanStringEncoding];

}

return self;

}

- (PPMadCommandObject*)getCommandFromPosition:(short)PosX channel:(short)TrackIdX

{

if (PosX < 0)

PosX = 0;

else if (PosX >= self.patternSize)

PosX = self.patternSize - 1;

return self.commands[(self.patternSize * TrackIdX) + PosX];

}

- (void)replaceCommandAtPosition:(short)PosX channel:(short)TrackIdX cmd:(Cmd)aCmd

{

if (PosX < 0)

PosX = 0;

else if (PosX >= self.patternSize)

PosX = self.patternSize - 1;

self.commands[(self.patternSize * TrackIdX) + PosX] = [[PPMadCommandObject alloc] initWithCmd:aCmd];

}

- (void)replaceCommandAtPosition:(short)PosX channel:(short)TrackIdX command:(PPMadCommandObject*)aCmd

{

if (PosX < 0)

PosX = 0;

else if (PosX >= self.patternSize)

PosX = self.patternSize - 1;

self.commands[(self.patternSize * TrackIdX) + PosX] = [aCmd copy];

}

- (void)modifyCommandAtPosition:(short)PosX channel:(short)TrackIdX commandBlock:(void (^)(Cmd*))block

{

PPMadCommandObject *tmpMCmd = [self getCommandFromPosition:PosX channel:TrackIdX];

Cmd tmpCmd = tmpMCmd.theCommand;

block(&tmpCmd);

[self replaceCommandAtPosition:PosX channel:TrackIdX cmd:tmpCmd];

}

- (void)modifyCommandAtPosition:(short)PosX channel:(short)TrackIdX madCommandBlock:(void (^)(PPMadCommandObject*))block

{

PPMadCommandObject *tmpMCmd = [self getCommandFromPosition:PosX channel:TrackIdX];

block(tmpMCmd);

}

#if 0

- (void)dealloc

{

}

#endif

#pragma mark NSFastEnumeration protocol

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len

{

return [self.commands countByEnumeratingWithState:state objects:buffer count:len];

}

#pragma mark NSCoding protocol

+ (BOOL)supportsSecureCoding

{

return YES;

}

- (instancetype)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super init]) {

self.patternName = [aDecoder decodeObjectForKey:kPPPatternName];

self.commands = [(NSArray*)[aDecoder decodeObjectForKey:kPPPatternCommands] mutableCopy];

index = [aDecoder decodeIntegerForKey:kPPPatternIndex];

}

return self;

}

- (void)encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject:self.patternName forKey:kPPPatternName];

[aCoder encodeObject:commands forKey:kPPPatternCommands];

[aCoder encodeInteger:index forKey:kPPPatternIndex];

}

#pragma mark NSCopying protocol

- (id)copyWithZone:(NSZone *)zone

{

PPPatternObject *new = [[[self class] alloc] initWithMusic:_musicWrapper];

new.commands = [[NSMutableArray alloc] initWithArray:commands copyItems:YES];

new.index = -1;

new.patternName = self.patternName;

return new;

}

#pragma mark - Pcmd handling

+ (MADErr)testPcmdFileAtURL:(NSURL*)theURL

{

MADErr err = MADNoErr;

Pcmd thePcmd;

NSData *pcmdData = [[NSData alloc] initWithContentsOfURL:theURL];

if (!pcmdData) {

return MADReadingErr;

}

[pcmdData getBytes:&thePcmd length:sizeof(thePcmd)];

SwapPcmd(&thePcmd);

if (thePcmd.structSize != [pcmdData length]) {

err = MADIncompatibleFile;

}

return err;

}

- (BOOL)importPcmdFromURL:(NSURL*)theURL error:(NSError * _Nullable __autoreleasing * _Nullable)error

{

MADErr theErr = MADNoErr;

NSNumber *curNum;

theErr = [[self class] testPcmdFileAtURL:theURL];

if (theErr) {

if (error) {

*error = PPCreateErrorFromMADErrorType(theErr);

}

return NO;

}

const Pcmd *thePcmd;

NSMutableData *pcmdData = [[NSMutableData alloc] initWithContentsOfURL:theURL options:(NSDataReadingOptions)0 error:error];

if (!pcmdData) {

return false;

}

[theURL getResourceValue:&curNum forKey:NSURLFileSizeKey error:NULL];

SwapPcmd([pcmdData mutableBytes]);

thePcmd = [pcmdData bytes];

return [self importPcmdFromPointer:thePcmd error:error];

}

static inline NSString *GetEffectString(short theEffect)

{

short effectChar = theEffect;

char theChar;

switch (theEffect) {

case 16:

case 17:

case 18:

effectChar++;

//fall through

case 10 ... 15:

theChar = 'A' + effectChar - 10;

return [[NSString alloc] initWithFormat:@"%c", theChar];

break;

case 0 ... 9:

return [@(theEffect) stringValue];

break;

default:

break;

}

return @" ";

}

static BOOL CreateNoteString(Cmd *theCommand, NSMutableString *mainStr, BOOL AllStr)

{

BOOL Note = NO;

if (theCommand->ins != 0) {

Note = YES;

[mainStr appendString:[NSString stringWithFormat:@"%03d", theCommand->ins]];

} else {

[mainStr appendString:@" "];

}

[mainStr appendString:@" "];

if (theCommand->note == 0xFE) {

Note = YES;

[mainStr appendString:@"OFF"];

} else if (theCommand->note != 0xFF) {

Note = YES;

[mainStr appendString: [PPSampleObject octaveNameFromNote:theCommand->note]];

} else {

[mainStr appendString:@" "];

}

[mainStr appendString:@" "];

if (theCommand->cmd != 0) {

Note = YES;

[mainStr appendString:GetEffectString(theCommand->cmd)];

} else {

if (theCommand->arg != 0)

[mainStr appendString:GetEffectString(theCommand->cmd)];

else

[mainStr appendString:@" "];

}

[mainStr appendString:@" "];

if (theCommand->arg != 0) {

Note = YES;

[mainStr appendString:[NSString stringWithFormat:@"%02X", theCommand->arg]];

} else {

[mainStr appendString:@" "];

}

[mainStr appendString:@" "];

if (theCommand->vol != 0xFF) {

Note = YES;

[mainStr appendString:[NSString stringWithFormat:@"%02X", theCommand->vol]];

} else {

[mainStr appendString:@" "];

}

return Note;

}

+ (NSString *)stringFromPcmdData:(in const Pcmd*)thePcmd

{

int i, x;

NSMutableString *myText = [[NSMutableString alloc] init];

Pcmd *myPcmd = CopyPcmd(thePcmd);

size_t mSize = 5 + myPcmd->tracks * myPcmd->length * 16;

for (i = 0; i < myPcmd->length; i++) {

for (x = 0; x < myPcmd->tracks; x++) {

NSMutableString *myStr = [[NSMutableString alloc] init];

Cmd *myCmd = MADGetCmd(i, x, myPcmd);

if (CreateNoteString(myCmd, myStr, YES)) {

[myText appendString:myStr];

} else

[myText appendString:@" "];

if (x < myPcmd->tracks - 1)

[myText appendString:@"\t"];

else

[myText appendString:@"\r"];

}

}

free(myPcmd);

if ([myText length] >= mSize)

MADDebugStr(__LINE__, __FILE__, "ZZZ");

return [[NSString alloc] initWithString:myText];

}

static Cmd *GetMADCommandFromPatternObj(short PosX, short TrackIdX, PPPatternObject *tempMusicPat)

{

Cmd *theCmd, tmpCmd;

if (tempMusicPat == NULL)

return NULL;

if (PosX < 0)

PosX = 0;

else if (PosX >= tempMusicPat->patternHeader.size)

PosX = tempMusicPat->patternHeader.size -1;

tmpCmd = [[tempMusicPat commands][(tempMusicPat->patternHeader.size * TrackIdX) + PosX] theCommand];

theCmd = malloc(sizeof(Cmd));

*theCmd = tmpCmd;

return theCmd;

}

+ (NSData*)dataFromPcmd:(in const Pcmd*)thePcmd

{

size_t structSize = thePcmd->structSize;

Pcmd *newPcmd = calloc(structSize, 1);

memcpy(newPcmd, thePcmd, structSize);

SwapPcmd(newPcmd);

return [[NSData alloc] initWithBytesNoCopy:newPcmd length:structSize];

}

- (Pcmd*)newPcmdWithTrackRange:(NSRange)trackRange positionRange:(NSRange)posRange

{

NSInteger count = (trackRange.length) * (posRange.length);

size_t theSize = sizeof(Pcmd) + count * sizeof(Cmd);

Pcmd *thePcmd = calloc(theSize, 1);

if (!thePcmd) {

return NULL;

}

thePcmd->structSize = (int)theSize;

thePcmd->tracks = trackRange.length;

thePcmd->length = posRange.length;

thePcmd->trackStart = trackRange.location;

thePcmd->posStart = posRange.location;

for (NSInteger X = trackRange.location; X <= NSMaxRange(trackRange); X++) {

for (NSInteger Y = posRange.location; Y <= NSMaxRange(posRange); Y++) {

Cmd *cmd, *cmd2;

cmd = GetMADCommandFromPatternObj(Y, X, self);

cmd2 = MADGetCmd(Y - NSMaxRange(posRange), X - NSMaxRange(trackRange), thePcmd);

*cmd2 = *cmd;

free(cmd);

}

}

return thePcmd;

}

- (BOOL)exportPcmdToURL:(NSURL*)theURL withTrackRange:(NSRange)trackRange positionRange:(NSRange)posRange error:(NSError**)error;

{

NSData *datToWrite;

if (trackRange.length == 0 || posRange.length == 0) {

if (error) {

*error = PPCreateErrorFromMADErrorType(MADParametersErr);

}

return false;

}

Pcmd *thePcmd = [self newPcmdWithTrackRange:trackRange positionRange:posRange];

if (!thePcmd) {

if (error) {

*error = PPCreateErrorFromMADErrorType(MADNeedMemory);

}

return false;

}

NSInteger theLength = thePcmd->structSize;

SwapPcmd(thePcmd);

datToWrite = [[NSData alloc] initWithBytesNoCopy:thePcmd length:theLength];

if (![datToWrite writeToURL:theURL options:NSDataWritingAtomic error:error]) {

//The Foundation method will fill out the error data for us.

return false;

}

return true;

}

- (BOOL)importPcmdFromPointer:(in const Pcmd*)thePcmd error:(NSError**)error

{

//TODO: put cmd data into the pattern

return YES;

}

- (BOOL)importIntPcmdFromPointer:(IntPcmd*)theIntPcmd freeCommandsWhenDone:(BOOL)freeCmds error:(NSError**)error;

{

Pcmd *tmpPcmd = MADIntPcmdToPcmd(theIntPcmd, freeCmds);

BOOL iErr = [self importPcmdFromPointer:tmpPcmd error:error];

free(tmpPcmd);

return iErr;

}

#ifndef __MACERRORS__

#define unimpErr (-4)

#endif

- (BOOL)importPcmdFromString:(NSString*)pcmdStr error:(NSError**)error

{

//TODO: implement

if (error) {

*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:nil];

}

return NO;

}

@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