## Classes
DataBufferSP404Pattern - Roland SP-404SX / SP-404 MKii Pattern Utility
A utility to read, modify and write pattern files from a Roland SP-404SX / SP-404 MKii PTN files.
Can also convert patterns to MIDI or convert from MIDI to pattern.
Several values are not saved into the pattern but are configured when recording a pattern:
Substep on Sequencer Mode actually generates multiple notes depending on the substep type offset by a set number of ticks, no other designation is set on the note.
Notes in the pattern grid will typically line up, but if the last note or a note near the end plays a sample beyond the length of the bar, there needs to be place holder notes for however long that is.
Optional diagnostics, disabled by default so unrelated imports can discard this module.
Validate before quantization or byte writes can hide invalid numeric input.
Select a fresh device map without import-time allocations or shared mutable defaults.
Narrow the MIDI event's union payload without relying on labels or legacy string note values.
DataBuffer
SP404Pattern - Roland SP-404SX / SP-404 MKii Pattern Utility
A utility to read, modify and write pattern files from a Roland SP-404SX / SP-404 MKii `PTN` files.
Can also convert patterns to MIDI or convert from MIDI to pattern.
Several values are not saved into the pattern but are configured when recording a pattern:
- BPM
- Quantization Strength / Shuffle Rate
- Quantization Grid Size
- Metronome Volume
Substep on Sequencer Mode actually generates multiple notes depending on the substep type offset by a set number of ticks, no other designation is set on the note.
Notes in the pattern grid will typically line up, but if the last note or a note near the end plays a sample beyond the length of the bar, there needs to be place holder notes for however long that is.
**Kind**: global class
**Extends**: DataBuffer
* [SP404Pattern](#SP404Pattern) ⇐ DataBuffer
* [new SP404Pattern([input], options)](#new_SP404Pattern_new)
* _instance_
* [.bars](#SP404Pattern+bars)
* [.timeSignature](#SP404Pattern+timeSignature)
* [.notes](#SP404Pattern+notes)
* [.defaultMap](#SP404Pattern+defaultMap)
* [.options](#SP404Pattern+options)
* [.validateOptions()](#SP404Pattern+validateOptions)
* [.parse(options)](#SP404Pattern+parse)
* [.toMidi(options)](#SP404Pattern+toMidi) ⇒ AudioMIDI
* [.getUsedPads()](#SP404Pattern+getUsedPads) ⇒ Array.<string>
* _static_
* [.defaultPPQOG](#SP404Pattern.defaultPPQOG)
* [.defaultPPQ](#SP404Pattern.defaultPPQ)
* [.defaultMap](#SP404Pattern.defaultMap) ⇒ Record.<string, SP404PadMapping>
* [.defaultMapOG](#SP404Pattern.defaultMapOG)
* [.fromMidi(audioMIDI, noteMap, patternPPQN, [og])](#SP404Pattern.fromMidi) ⇒ DataBuffer
### new SP404Pattern([input], options)
Creates a new SP404Pattern.
| Param | Type | Description |
| --- | --- | --- |
| [input] | Array.<number> \| ArrayBuffer \| Buffer \| DataBuffer \| Int8Array \| Int16Array \| Int32Array \| number \| string \| Uint8Array \| Uint16Array \| Uint32Array \| undefined | The data to process. |
| options | object | The options for parsing the pattern. |
| [options.bytesPerNote] | number | The number of bytes for each note; default is 8. |
| [options.padsPerBank] | number | The number of pads per bank, 12 or 16 for the MKii; default is 12 for OG and 16 for MKii. |
| [options.og] | boolean | When true, process for the original SP404s, when false for the MKii; default is false. Omitted input creates an empty instance. Partial records or footers are rejected. |
**Example** *(SP404Pattern)*
```js
const data = fs.readFileSync('./PTN00025.BIN');
const file = new SP404Pattern(data);
console.log('Notes:', file.notes);
```
### sP404Pattern.bars
Footer bar count (normally 1–64), an empty instance starts at zero.
**Kind**: instance property of [SP404Pattern](#SP404Pattern)
### sP404Pattern.timeSignature
The time signature of the pattern: 0 = 4/4, 1 = 3/4, 2 = 2/4, 3 = 1/4, 4 = 5/4, 5 = 6/4, 7 = 7/4. Unknown codes are retained.
**Kind**: instance property of [SP404Pattern](#SP404Pattern)
### sP404Pattern.notes
Includes timing placeholders so silent gaps can survive conversion.
**Kind**: instance property of [SP404Pattern](#SP404Pattern)
### sP404Pattern.defaultMap
Address map for the selected device, owned by this instance.
**Kind**: instance property of [SP404Pattern](#SP404Pattern)
### sP404Pattern.options
Validated layout reused by subsequent parse() calls.
**Kind**: instance property of [SP404Pattern](#SP404Pattern)
### sP404Pattern.validateOptions()
Reject layouts that would desynchronize event reads from the fixed sixteen-byte footer.
**Kind**: instance method of [SP404Pattern](#SP404Pattern)
### sP404Pattern.parse(options)
Parse the pattern into notes and extract the bar count from the footer.
Reparse from byte zero. Incomplete notes and footers throw before state changes.
**Kind**: instance method of [SP404Pattern](#SP404Pattern)
| Param | Type | Description |
| --- | --- | --- |
| options | object | The options for parsing the pattern. |
| [options.bytesPerNote] | number | The number of bytes for each note; default is 8. |
| [options.padsPerBank] | number | The number of pads per bank, 12 or 16 for the MKii; default is 12 for OG and 16 for MKii. |
| [options.og] | boolean | When true, process for the original SP404s, when false for the MKii; default is false. |
### sP404Pattern.toMidi(options) ⇒ AudioMIDI
Convert the parsed notes to a AudioMidi instance ready to be saved as MIDI file or manipulated further.
**Kind**: instance method of [SP404Pattern](#SP404Pattern)
**Returns**: AudioMIDI - A new AudioMIDI instance populated from the pattern.
| Param | Type | Description |
| --- | --- | --- |
| options | object | The options |
| [options.bpm] | number | The BPM of the track, when undefined no tempo event will be added. |
| [options.ppq] | number | The pulses per quarter note; defaults to 96 for OG or 480 for MKii. |
| [options.fileName] | string | The name of the pattern file being converted. |
| options.noteMap | Record.<string, number> | A map of Pads `A1` to `J16` that correspond to which MIDI note. Timing is rescaled to the destination PPQ; placeholders and unmapped pads retain their delays. Known time signatures are exported as MIDI meta events; unknown codes throw. The footer bar count also preserves the pattern duration when final padding is absent. |
### sP404Pattern.getUsedPads() ⇒ Array.<string>
Gathers all pads used in this pattern in first-use order, excluding placeholders and unknown pad addresses.
**Kind**: instance method of [SP404Pattern](#SP404Pattern)
**Returns**: Array.<string> - An array of distinct pad labels.
### SP404Pattern.defaultPPQOG
Original device ticks per quarter note.
**Kind**: static property of [SP404Pattern](#SP404Pattern)
### SP404Pattern.defaultPPQ
MKII ticks per quarter note.
**Kind**: static property of [SP404Pattern](#SP404Pattern)
### SP404Pattern.defaultMap ⇒ Record.<string, SP404PadMapping>
The default mapping of pads `A1` to `J16` to MIDI notes.
Returns a fresh map owned by the caller.
**Kind**: static property of [SP404Pattern](#SP404Pattern)
**Returns**: Record.<string, SP404PadMapping> - The default mapping of pads `A1` to `J16` to MIDI notes.
### SP404Pattern.defaultMapOG
Native SX twelve-pad addresses, shared by A–E and F–J with separate bank selectors.
These are pattern-file addresses, independent of the MIDI notes used to trigger the device.
Returns a fresh map owned by the caller.
**Kind**: static property of [SP404Pattern](#SP404Pattern)
### SP404Pattern.fromMidi(audioMIDI, noteMap, patternPPQN, [og]) ⇒ DataBuffer
Converts a AudioMIDI structure back into a pad file format.
Pads that play all the way through will have 2 on notes, one to start the sound and one to end it.
**Kind**: static method of [SP404Pattern](#SP404Pattern)
**Returns**: DataBuffer - A committed DataBuffer representing the pad file.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| audioMIDI | default | | The AudioMIDI instance to convert back to a pad file. |
| noteMap | Record.<number, string> | | A map of MIDI note numbers to pad labels `A1` to `J16`. |
| patternPPQN | number | | The pulses per quarter note of the pattern; OG is 96, MKii is 480. |
| [og] | boolean | false | When true, process for the original SP404s, when false for the MKii; default is false. Native SX output uses following delays, big-endian durations, and footer byte 9 for bars. Durations come from Note On length or matching FIFO Note Off events within each track/channel. Missing durations become zero; unmapped notes, SMPTE timing, non-4/4 signatures, independent format-2 tracks and unrepresentable lengths throw. Patterns are limited to 64 bars. |
## debug()
Optional diagnostics, disabled by default so unrelated imports can discard this module.
**Kind**: global function
## integer()
Validate before quantization or byte writes can hide invalid numeric input.
**Kind**: global function
## padMap()
Select a fresh device map without import-time allocations or shared mutable defaults.
**Kind**: global function
## isNoteData()
Narrow the MIDI event's union payload without relying on labels or legacy string note values.
**Kind**: global function