musicxml-io 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,285 @@
1
+ import { S as Score, M as Measure, T as TimeSignature, P as Part, a as Pitch } from './types-DC_TnJu_.js';
2
+ export { A as Accidental, i as AccidentalInfo, B as BackupEntry, p as Barline, k as BeamInfo, s as Chord, C as Clef, w as Credit, v as Defaults, D as DirectionEntry, m as DirectionType, n as DynamicsValue, F as ForwardEntry, K as KeySignature, L as Lyric, f as MeasureAttributes, g as MeasureEntry, l as Notation, N as NoteEntry, t as NoteIteratorItem, h as NoteType, r as NoteWithPosition, d as PartGroup, c as PartInfo, e as PartListEntry, u as Print, b as ScoreMetadata, q as StaffGroup, j as TieInfo, o as Transpose, V as VoiceGroup } from './types-DC_TnJu_.js';
3
+ export { NormalizedPositionOptions, VoiceFilter, getAbsolutePosition, getAllNotes, getChords, getNormalizedDuration, getNormalizedPosition, getNotesForStaff, getNotesForVoice, getStaves, getVoices, groupByStaff, groupByVoice, hasNotes, isRestMeasure, iterateNotes, withAbsolutePositions } from './accessors/index.js';
4
+ export { FindNotesFilter, PitchRange, RoundtripMetrics, countNotes, findNotes, getAttributesAtMeasure, getDivisions, getDuration, getMeasure, getMeasureByIndex, getMeasureCount, getPartById, getPartIndex, getStaveCount, hasMultipleStaves, measureRoundtrip, scoresEqual } from './query/index.js';
5
+ export { AddNoteOptions, addChordNote, addNote, changeKey, changeTime, deleteMeasure, deleteNote, insertMeasure, modifyNoteDuration, modifyNotePitch, setDivisions, transpose } from './operations/index.js';
6
+
7
+ declare function parse(xmlString: string): Score;
8
+
9
+ /**
10
+ * Parse a compressed MusicXML (.mxl) file
11
+ * @param data - The compressed file data as Uint8Array or Buffer
12
+ * @returns The parsed Score
13
+ */
14
+ declare function parseCompressed(data: Uint8Array): Score;
15
+ /**
16
+ * Check if data is a compressed MusicXML file
17
+ * @param data - The file data
18
+ * @returns true if the data appears to be a ZIP file
19
+ */
20
+ declare function isCompressed(data: Uint8Array): boolean;
21
+ /**
22
+ * Parse either compressed (.mxl) or uncompressed (.xml/.musicxml) MusicXML
23
+ * Automatically detects the format
24
+ * @param data - The file data as Uint8Array or string
25
+ * @returns The parsed Score
26
+ */
27
+ declare function parseAuto(data: Uint8Array | string): Score;
28
+
29
+ type ValidationErrorCode = 'MISSING_DIVISIONS' | 'INVALID_DIVISIONS' | 'MEASURE_DURATION_MISMATCH' | 'MEASURE_DURATION_OVERFLOW' | 'MEASURE_DURATION_UNDERFLOW' | 'NEGATIVE_POSITION' | 'BACKUP_EXCEEDS_POSITION' | 'TIE_START_WITHOUT_STOP' | 'TIE_STOP_WITHOUT_START' | 'TIE_PITCH_MISMATCH' | 'BEAM_BEGIN_WITHOUT_END' | 'BEAM_END_WITHOUT_BEGIN' | 'SLUR_START_WITHOUT_STOP' | 'SLUR_STOP_WITHOUT_START' | 'TUPLET_START_WITHOUT_STOP' | 'TUPLET_STOP_WITHOUT_START' | 'PART_ID_NOT_IN_PART_LIST' | 'PART_LIST_ID_NOT_IN_PARTS' | 'PART_MEASURE_COUNT_MISMATCH' | 'PART_MEASURE_NUMBER_MISMATCH' | 'PART_GROUP_START_WITHOUT_STOP' | 'PART_GROUP_STOP_WITHOUT_START' | 'DUPLICATE_PART_ID' | 'INVALID_VOICE_NUMBER' | 'INVALID_STAFF_NUMBER' | 'STAFF_EXCEEDS_STAVES' | 'MISSING_STAVES_DECLARATION' | 'STAVES_DECLARATION_MISMATCH' | 'MISSING_CLEF_FOR_STAFF' | 'CLEF_STAFF_EXCEEDS_STAVES' | 'INVALID_DURATION' | 'EMPTY_MEASURE';
30
+ type ValidationLevel = 'error' | 'warning' | 'info';
31
+ interface ValidationLocation {
32
+ partIndex?: number;
33
+ partId?: string;
34
+ measureIndex?: number;
35
+ measureNumber?: string;
36
+ entryIndex?: number;
37
+ voice?: number;
38
+ staff?: number;
39
+ }
40
+ interface ValidationError {
41
+ code: ValidationErrorCode;
42
+ level: ValidationLevel;
43
+ message: string;
44
+ location: ValidationLocation;
45
+ details?: Record<string, unknown>;
46
+ }
47
+ interface ValidationResult {
48
+ valid: boolean;
49
+ errors: ValidationError[];
50
+ warnings: ValidationError[];
51
+ infos: ValidationError[];
52
+ }
53
+ interface ValidateOptions {
54
+ /** Check divisions consistency (default: true) */
55
+ checkDivisions?: boolean;
56
+ /** Check measure durations match time signature (default: true) */
57
+ checkMeasureDuration?: boolean;
58
+ /** Check backup/forward position consistency (default: true) */
59
+ checkPosition?: boolean;
60
+ /** Check tie start/stop pairing (default: true) */
61
+ checkTies?: boolean;
62
+ /** Check beam begin/end pairing (default: true) */
63
+ checkBeams?: boolean;
64
+ /** Check slur start/stop pairing (default: true) */
65
+ checkSlurs?: boolean;
66
+ /** Check tuplet start/stop pairing (default: true) */
67
+ checkTuplets?: boolean;
68
+ /** Check part ID references (default: true) */
69
+ checkPartReferences?: boolean;
70
+ /** Check part structure (measure count, numbers) (default: true) */
71
+ checkPartStructure?: boolean;
72
+ /** Check voice/staff numbers (default: true) */
73
+ checkVoiceStaff?: boolean;
74
+ /** Check staff structure (staves declaration, clefs) (default: true) */
75
+ checkStaffStructure?: boolean;
76
+ /** Tolerance for measure duration (in divisions, default: 0) */
77
+ durationTolerance?: number;
78
+ }
79
+ /**
80
+ * Validate a Score for internal consistency
81
+ */
82
+ declare function validate(score: Score, options?: ValidateOptions): ValidationResult;
83
+ /**
84
+ * Validate that divisions are defined and consistent
85
+ */
86
+ declare function validateDivisions(score: Score): ValidationError[];
87
+ /**
88
+ * Validate measure duration matches time signature
89
+ */
90
+ declare function validateMeasureDuration(measure: Measure, divisions: number, time: TimeSignature, location: ValidationLocation, tolerance?: number): ValidationError[];
91
+ /**
92
+ * Validate backup/forward position consistency
93
+ */
94
+ declare function validateBackupForward(measure: Measure, location: ValidationLocation): ValidationError[];
95
+ /**
96
+ * Validate tie start/stop pairing
97
+ */
98
+ declare function validateTies(measure: Measure, location: ValidationLocation): ValidationError[];
99
+ /**
100
+ * Validate beam begin/end pairing
101
+ */
102
+ declare function validateBeams(measure: Measure, location: ValidationLocation): ValidationError[];
103
+ /**
104
+ * Validate slur start/stop pairing
105
+ */
106
+ declare function validateSlurs(measure: Measure, _location: ValidationLocation): ValidationError[];
107
+ /**
108
+ * Validate tuplet start/stop pairing
109
+ */
110
+ declare function validateTuplets(measure: Measure, location: ValidationLocation): ValidationError[];
111
+ /**
112
+ * Validate part ID references between partList and parts
113
+ */
114
+ declare function validatePartReferences(score: Score): ValidationError[];
115
+ /**
116
+ * Validate voice and staff numbers
117
+ */
118
+ declare function validateVoiceStaff(measure: Measure, staves: number, location: ValidationLocation): ValidationError[];
119
+ /**
120
+ * Validate part structure (measure counts and numbers match across parts)
121
+ */
122
+ declare function validatePartStructure(score: Score): ValidationError[];
123
+ /**
124
+ * Validate staff structure within a part
125
+ */
126
+ declare function validateStaffStructure(part: Part, partIndex: number): ValidationError[];
127
+ /**
128
+ * Context needed to validate a single measure
129
+ */
130
+ interface MeasureValidationContext {
131
+ /** Current divisions value (from previous attributes) */
132
+ divisions: number;
133
+ /** Current time signature */
134
+ time?: TimeSignature;
135
+ /** Current staves count */
136
+ staves: number;
137
+ /** Part index (for error location) */
138
+ partIndex: number;
139
+ /** Part ID (for error location) */
140
+ partId: string;
141
+ /** Measure index (for error location) */
142
+ measureIndex: number;
143
+ }
144
+ /**
145
+ * Options for local measure validation
146
+ */
147
+ interface LocalValidateOptions {
148
+ checkMeasureDuration?: boolean;
149
+ checkPosition?: boolean;
150
+ checkBeams?: boolean;
151
+ checkTuplets?: boolean;
152
+ checkVoiceStaff?: boolean;
153
+ durationTolerance?: number;
154
+ }
155
+ /**
156
+ * Validate a single measure with provided context.
157
+ * This is useful for validating after local operations like addNote, deleteNote.
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const context = getMeasureContext(score, partIndex, measureIndex);
162
+ * const errors = validateMeasureLocal(measure, context);
163
+ * if (errors.length > 0) {
164
+ * throw new Error('Operation created invalid state');
165
+ * }
166
+ * ```
167
+ */
168
+ declare function validateMeasureLocal(measure: Measure, context: MeasureValidationContext, options?: LocalValidateOptions): ValidationError[];
169
+ /**
170
+ * Get the validation context for a measure by traversing previous attributes.
171
+ * This collects divisions, time, and staves from measure 0 to the target measure.
172
+ */
173
+ declare function getMeasureContext(score: Score, partIndex: number, measureIndex: number): MeasureValidationContext;
174
+ /**
175
+ * Validate a measure after an operation, throwing if invalid.
176
+ * Convenience wrapper around validateMeasureLocal.
177
+ */
178
+ declare function assertMeasureValid(score: Score, partIndex: number, measureIndex: number, options?: LocalValidateOptions): void;
179
+ /**
180
+ * Check if a score is valid (no errors)
181
+ */
182
+ declare function isValid(score: Score, options?: ValidateOptions): boolean;
183
+ /**
184
+ * Validate and throw if invalid
185
+ */
186
+ declare function assertValid(score: Score, options?: ValidateOptions): void;
187
+ /**
188
+ * Format a validation location for display
189
+ */
190
+ declare function formatLocation(location: ValidationLocation): string;
191
+ /**
192
+ * Validation exception with structured error information
193
+ */
194
+ declare class ValidationException extends Error {
195
+ readonly errors: ValidationError[];
196
+ constructor(errors: ValidationError[], message: string);
197
+ }
198
+ /**
199
+ * Validate ties across measures
200
+ * This is more complex as ties can span multiple measures
201
+ */
202
+ declare function validateTiesAcrossMeasures(part: Part): ValidationError[];
203
+ /**
204
+ * Validate slurs across measures
205
+ */
206
+ declare function validateSlursAcrossMeasures(part: Part): ValidationError[];
207
+
208
+ interface SerializeOptions {
209
+ version?: '3.1' | '4.0';
210
+ indent?: string;
211
+ /** Validate score before serializing (default: false) */
212
+ validate?: boolean;
213
+ /** Options for validation (if validate is true) */
214
+ validateOptions?: ValidateOptions;
215
+ /** Throw error if validation fails (default: false, will only warn) */
216
+ throwOnValidationError?: boolean;
217
+ /** Callback to receive validation result */
218
+ onValidation?: (result: ValidationResult) => void;
219
+ }
220
+ declare function serialize(score: Score, options?: SerializeOptions): string;
221
+
222
+ /**
223
+ * Serialize a Score to compressed MusicXML (.mxl) format
224
+ * @param score - The Score to serialize
225
+ * @param options - Serialization options
226
+ * @returns The compressed file data as Uint8Array
227
+ */
228
+ declare function serializeCompressed(score: Score, options?: SerializeOptions): Uint8Array;
229
+
230
+ /**
231
+ * MIDI export options
232
+ */
233
+ interface MidiExportOptions {
234
+ /** Ticks per quarter note (default: 480) */
235
+ ticksPerQuarterNote?: number;
236
+ /** Default tempo in BPM (default: 120) */
237
+ defaultTempo?: number;
238
+ /** Default velocity for notes (default: 80) */
239
+ defaultVelocity?: number;
240
+ }
241
+ /**
242
+ * Export a Score to Standard MIDI File format (SMF Type 1)
243
+ * @param score - The Score to export
244
+ * @param options - Export options
245
+ * @returns The MIDI file data as Uint8Array
246
+ */
247
+ declare function exportMidi(score: Score, options?: MidiExportOptions): Uint8Array;
248
+
249
+ /**
250
+ * Parse a MusicXML file from disk
251
+ * Automatically handles both .xml/.musicxml and .mxl formats
252
+ * @param filePath - Path to the file
253
+ * @returns The parsed Score
254
+ */
255
+ declare function parseFile(filePath: string): Promise<Score>;
256
+ /**
257
+ * Detect encoding from BOM and decode buffer to string
258
+ * Supports UTF-8, UTF-16BE, UTF-16LE
259
+ */
260
+ declare function decodeBuffer(buffer: Buffer): string;
261
+ /**
262
+ * Export options combining all format options
263
+ */
264
+ interface ExportOptions extends SerializeOptions, MidiExportOptions {
265
+ }
266
+ /**
267
+ * Serialize a Score to a file
268
+ * Format is determined by file extension:
269
+ * - .mxl: Compressed MusicXML
270
+ * - .xml/.musicxml: Uncompressed MusicXML
271
+ * - .mid/.midi: Standard MIDI File
272
+ * @param score - The Score to serialize
273
+ * @param filePath - Path to write the file
274
+ * @param options - Serialization options
275
+ */
276
+ declare function serializeToFile(score: Score, filePath: string, options?: ExportOptions): Promise<void>;
277
+
278
+ declare const STEPS: Pitch['step'][];
279
+ declare const STEP_SEMITONES: Record<Pitch['step'], number>;
280
+ /** Convert pitch to semitone value (MIDI-like) */
281
+ declare function pitchToSemitone(pitch: Pitch): number;
282
+ /** Get position at end of measure */
283
+ declare function getMeasureEndPosition(measure: Measure): number;
284
+
285
+ export { type LocalValidateOptions, Measure, type MeasureValidationContext, type MidiExportOptions, Part, Pitch, STEPS, STEP_SEMITONES, Score, type SerializeOptions, TimeSignature, type ValidateOptions, type ValidationError, type ValidationErrorCode, ValidationException, type ValidationLevel, type ValidationLocation, type ValidationResult, assertMeasureValid, assertValid, decodeBuffer, exportMidi, formatLocation, getMeasureContext, getMeasureEndPosition, isCompressed, isValid, parse, parseAuto, parseCompressed, parseFile, pitchToSemitone, serialize, serializeCompressed, serializeToFile, validate, validateBackupForward, validateBeams, validateDivisions, validateMeasureDuration, validateMeasureLocal, validatePartReferences, validatePartStructure, validateSlurs, validateSlursAcrossMeasures, validateStaffStructure, validateTies, validateTiesAcrossMeasures, validateTuplets, validateVoiceStaff };