Menu

[6e755d]: / PlayerPROKit / PlayerPROCoreAdditions.swift  Maximize  Restore  History

Download this file

492 lines (409 with data), 12.4 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
//
// PlayerPROCoreAdditions.swift
// PPMacho
//
// Created by C.W. Betts on 7/24/14.
//
//
import CoreFoundation
import Foundation
import PlayerPROCore.PlugIns
import SwiftAdditions
public func ==(lhs: Cmd, rhs: Cmd) -> Bool {
// Don't worry about the "unused" variable for now
if lhs.ins != rhs.ins {
return false
}
if lhs.note != rhs.note {
return false
}
if lhs.cmd != rhs.cmd {
return false
}
if lhs.arg != rhs.arg {
return false
}
if lhs.vol != rhs.vol {
return false
}
return true
}
public func ==(lhs: IntPcmd, rhs: IntPcmd) -> Bool {
if lhs.tracks != rhs.tracks {
return false
} else if lhs.length != rhs.length {
return false
} else if lhs.trackStart != rhs.trackStart {
return false
} else if lhs.posStart != rhs.posStart {
return false
} else if lhs.cmdCount != rhs.cmdCount {
return false
} else {
let lhsCmds = UnsafeMutableBufferPointer(start: lhs.myCmd, count: Int(lhs.cmdCount))
let rhsCmds = UnsafeMutableBufferPointer(start: rhs.myCmd, count: Int(rhs.cmdCount))
for (lhsCmd, rhsCmd) in zip(lhsCmds, rhsCmds) {
if lhsCmd != rhsCmd {
return false
}
}
}
return true
}
public func ==(lhs: EnvRec, rhs: EnvRec) -> Bool {
if lhs.pos != rhs.pos {
return false
}
if lhs.val != rhs.val {
return false
}
return true
}
public func ==(lhs: FXBus, rhs: FXBus) -> Bool {
if lhs.Active != rhs.Active {
return false
}
if lhs.ByPass != rhs.ByPass {
return false
}
if lhs.copyId != rhs.copyId {
return false
}
return true
}
public func ==(lhs: MADDriverSettings, rhs: MADDriverSettings) -> Bool {
if lhs.driverMode != rhs.driverMode {
return false
}
if lhs.numChn != rhs.numChn {
return false
}
if lhs.outPutBits != rhs.outPutBits {
return false
}
if lhs.outPutMode != rhs.outPutMode {
return false
}
if lhs.outPutRate != rhs.outPutRate {
return false
}
if lhs.MicroDelaySize != rhs.MicroDelaySize {
return false
}
if lhs.ReverbSize != rhs.ReverbSize {
return false
}
if lhs.ReverbStrength != rhs.ReverbStrength {
return false
}
if lhs.oversampling != rhs.oversampling {
return false
}
if lhs.TickRemover != rhs.TickRemover {
return false
}
if lhs.surround != rhs.surround {
return false
}
if lhs.Reverb != rhs.Reverb {
return false
}
if lhs.repeatMusic != rhs.repeatMusic {
return false
}
return true
}
public func ==(lhs: FXSets, rhs: FXSets) -> Bool {
if lhs.track != rhs.track {
return false
}
if lhs.id != rhs.id {
return false
}
if lhs.FXID != rhs.FXID {
return false
}
if lhs.noArg != rhs.noArg {
return false
}
let lhsName = String(pascalString: lhs.name) ?? ""
let rhsName = String(pascalString: rhs.name) ?? ""
if lhsName != rhsName {
return false
}
let lhsValuesArray: [Float] = try! arrayFromObject(reflecting: lhs.values)
let rhsValuesArray: [Float] = try! arrayFromObject(reflecting: rhs.values)
// Ignore values that aren't accessed
for i in 0..<Int(lhs.noArg) {
if lhsValuesArray[i] != rhsValuesArray[i] {
return false
}
}
return true
}
public let MadID = toOSType(string: "MADK")
// MARK: PlayerPRO MAD data types
extension MADOutputChannel: CustomStringConvertible {
public var stringValue: String {
switch self {
case .MonoOutPut:
return "mono"
case .StereoOutPut:
return "stereo"
case .DeluxeStereoOutPut:
return "deluxe stereo"
case .PolyPhonic:
return "Polyphonic"
}
}
public var description: String {
return self.stringValue
}
}
extension MADSoundOutput: CustomStringConvertible {
public var stringValue: String {
switch self {
case .CoreAudioDriver:
return "CoreAudio"
case .DirectSound95NT:
return "DirectSound"
case .Wave95NT:
return "Windows WaveOut"
case .PortAudioDriver:
return "PortAudio"
case .PulseAudioDriver:
return "PulseAudio"
case .ESDDriver:
return "ESound Daemon"
case .BeOSSoundDriver:
return "BeOS/Haiku"
case .MIDISoundDriver:
return "MIDI"
case .ASIOSoundManager:
return "ASIO"
case .SoundManagerDriver:
return "Carbon Sound Manager"
case .NoHardwareDriver:
return "none"
}
}
public var description: String {
return self.stringValue
}
public var available: Bool {
return MADSoundDriverIsAvalable(self)
}
}
extension MADDriverSettings: CustomDebugStringConvertible, Equatable {
public static func new() -> MADDriverSettings {
var mdriverSettings = MADDriverSettings()
mdriverSettings.resetToBestDriver()
return mdriverSettings
}
public mutating func resetToBestDriver() {
MADGetBestDriver(&self)
}
public var debugDescription: String {
let onVal = "on"
let offVal = "off"
return "Driver Mode: \(driverMode.description), output mode: \(outPutMode.description.capitalized); Channel count: \(numChn), output Rate: \(outPutRate), surround: \(surround == true ? onVal : offVal); micro-delay size: \(MicroDelaySize), reverb, is \(Reverb == true ? onVal: offVal), size: \(ReverbSize), strength: \(ReverbStrength); oversampling \(oversampling); repeat music: \(repeatMusic == true ? onVal : offVal); "
}
}
public let maximumPanning: MADByte = 64
public let minimumVolume: MADByte = 0
public let noFineTune: UInt16 = 8363
public let maximumVolume: MADByte = 64
public let maximumChannelVolume: MADByte = 128
public let maximumArpeggio: Int32 = 3
public let equalizerPacketElements = 512
/// PlayerPROCore's internal debugger function, wrapped to include the line and file in Swift.
/// - parameter text: Developer text that is used to help debug the issue. **Cannot be NULL**, but may be an empty string.
/// - parameter file: The file that has the problem.
/// - parameter line: the line of the source file that's having a problem.
///
/// **Normally** it is never called, only when a FATAL error has occured. <br>
/// This function is automatically invoked using the macros `#line` and
/// `#file` for the `line` and `file` paramaters.
public func MADDebug(string text: String, line: UInt = #line, file: StaticString = #file) {
MADDebugStr(Int16(line), (String(describing: file) as NSString).fileSystemRepresentation, text)
}
private var BlankNameChar32: (Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8,
Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8,
Int8, Int8, Int8, Int8, Int8, Int8) = {
return (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}()
extension sData32 {
private init() {
self.init(size: 0, loopBeg: 0, loopSize: 0, vol: maximumVolume, c2spd: noFineTune, loopType: .classic, amp: 8, realNote: 0, name: BlankNameChar32, stereo: false, data: 0)
}
public static func new() -> sData32 {
return sData32()
}
public var toSData : sData {
return sData(self)
}
public init(_ fromSData32: sData) {
self.init(size: fromSData32.size, loopBeg: fromSData32.loopBeg, loopSize: fromSData32.loopSize, vol: fromSData32.vol, c2spd: fromSData32.c2spd, loopType: fromSData32.loopType, amp: fromSData32.amp, realNote: fromSData32.realNote, name: fromSData32.name, stereo: fromSData32.stereo, data: 0)
}
}
extension sData {
public static func new() -> sData {
return sData()
}
public var toSData32 : sData32 {
return sData32(self)
}
public init(_ fromSData32: sData32) {
self.init(size: fromSData32.size, loopBeg: fromSData32.loopBeg, loopSize: fromSData32.loopSize, vol: fromSData32.vol, c2spd: fromSData32.c2spd, loopType: fromSData32.loopType, amp: fromSData32.amp, realNote: fromSData32.realNote, name: fromSData32.name, stereo: fromSData32.stereo, data: nil)
}
private init() {
self.init(size: 0, loopBeg: 0, loopSize: 0, vol: maximumVolume, c2spd: noFineTune, loopType: .classic, amp: 8, realNote: 0, name: BlankNameChar32, stereo: false, data: nil)
}
}
extension EnvRec: Hashable {
public var hashValue: Int {
var aHi = UInt(pos)
aHi |= UInt(val) << 16
return Int(bitPattern: aHi)
}
}
extension FXBus: Hashable {
public var hashValue: Int {
var aVar = Int(copyId)
aVar |= Active.boolValue ? 1 << 16 : 0
aVar |= ByPass.boolValue ? 1 << 17 : 0
return aVar
}
}
extension FXSets: Equatable {
}
extension Cmd: Equatable {
private init() {
self.init(ins: 0, note: 0xFF, cmd: .arpeggio, arg: 0, vol: 0xFF, unused: 0)
}
/// Returns a `Cmd` with all bytes to zero, except `note`
/// and `vol`, which are `0xFF`.
public static func blankCmd() -> Cmd {
return Cmd()
}
/// Returns a `Cmd` with all bytes to zero, except `note`
/// and `vol`, which are `0xFF`.
public static func new() -> Cmd {
return Cmd()
}
/// Reset the command
public mutating func kill() {
MADKillCmd(&self)
}
}
public func getCommand(position: Int16, channel: Int16, aPat: UnsafeMutablePointer<PatData>) -> Cmd {
return getCommand(position: position, channel: channel, aPat: aPat).pointee
}
private func getCommand(position: Int16, channel: Int16, aPat: UnsafeMutablePointer<PatData>) -> UnsafeMutablePointer<Cmd> {
return GetMADCommand(position, channel, aPat)
}
public func replaceCmd(position: Int16, channel: Int16, command: Cmd, aPat: UnsafeMutablePointer<PatData>) {
let aCmd: UnsafeMutablePointer<Cmd> = getCommand(position: position, channel: channel, aPat: aPat)
aCmd.pointee = command
}
public func modifyCmdAtRow(position: Int16, channel: Int16, aPat: UnsafeMutablePointer<PatData>, commandBlock: (inout Cmd)-> ()) {
var aCmd: Cmd = getCommand(position: position, channel: channel, aPat: aPat)
commandBlock(&aCmd)
replaceCmd(position: position, channel: channel, command: aCmd, aPat: aPat)
}
public func getCommand(row: Int16, track: Int16, aPcmd: UnsafeMutablePointer<Pcmd>) -> Cmd {
return MADGetCmd(row, track, aPcmd).pointee
}
public func replaceCommand(row: Int16, track: Int16, command: Cmd, aPcmd: UnsafeMutablePointer<Pcmd>) {
let aCmd = MADGetCmd(row, track, aPcmd)!
aCmd.pointee = command
}
public func modifyCommand(row: Int16, track: Int16, aPcmd: UnsafeMutablePointer<Pcmd>, commandBlock: (inout Cmd)-> ()) {
var aCmd = getCommand(row: row, track: track, aPcmd: aPcmd)
commandBlock(&aCmd)
replaceCommand(row: row, track: track, command: aCmd, aPcmd: aPcmd)
}
extension IntPcmd: CommandIterator, Equatable {
public var commandLength: Int16 {
return length
}
private func getCommandIndex(atRow arow: Int16, track atrack: Int16) -> Int {
var row = arow
var track = atrack
if row < 0 {
row = 0
} else if row >= length {
row = length - 1
}
if track < 0 {
track = 0
} else if track >= tracks {
track = tracks - 1
}
return Int(length) * Int(track) + Int(row)
}
public func getCommand(atRow row: Int16, track: Int16) -> Cmd {
let ourAddr = getCommandIndex(atRow: row, track: track)
return myCmd[ourAddr]
}
public mutating func modifyCommand(atRow row: Int16, track: Int16, commandBlock: (inout Cmd) -> ()) {
let ourAddr = getCommandIndex(atRow: row, track: track)
commandBlock(&myCmd[ourAddr])
}
public mutating func replaceCommand(atRow row: Int16, track: Int16, with command: Cmd) {
modifyCommand(atRow: row, track: track, commandBlock: {( aCmd: inout Cmd) -> () in
aCmd = command
})
}
}
extension MADChannel {
public var arpeggio: (values: [Int32], index: Int32, enabled: Bool) {
return (try! arrayFromObject(reflecting: arp), arpindex, arpUse)
}
public var vibrato: (offset: Int8, depth: Int32, rate: Int32, type: Int32) {
return (viboffset, vibdepth, vibrate, vibtype)
}
}
extension MADBool: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
if value {
self = .true
} else {
self = .false
}
}
public init(_ value: Bool) {
self.init(booleanLiteral: value)
}
public var boolValue: Bool {
if rawValue == 0 {
return false
} else {
return true
}
}
}
extension MADBool: CustomReflectable, Equatable {
public var customMirror: Mirror {
return Mirror(reflecting: boolValue)
}
static public func ==(lhs: MADBool, rhs: MADBool) -> Bool {
return lhs.boolValue == rhs.boolValue
}
}
extension MADBool: CustomPlaygroundDisplayConvertible {
public var playgroundDescription: Any {
return self.boolValue
}
}
// MARK: MADFourChar
// TODO: find out how Apple does this with CGFloat...
/*
public struct MADFourChar {
let value: OSType
typealias internalValue = OSType
init(_ type: OSType) {
value = type
}
}
*/