-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDawProjectTest.java
More file actions
485 lines (414 loc) · 16.8 KB
/
DawProjectTest.java
File metadata and controls
485 lines (414 loc) · 16.8 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
package com.bitwig.dawproject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import com.bitwig.dawproject.device.Device;
import com.bitwig.dawproject.device.DeviceRole;
import com.bitwig.dawproject.device.Vst3Plugin;
import com.bitwig.dawproject.timeline.Audio;
import com.bitwig.dawproject.timeline.RealPoint;
import com.bitwig.dawproject.timeline.Clip;
import com.bitwig.dawproject.timeline.Clips;
import com.bitwig.dawproject.timeline.Marker;
import com.bitwig.dawproject.timeline.Markers;
import com.bitwig.dawproject.timeline.Lanes;
import com.bitwig.dawproject.timeline.Note;
import com.bitwig.dawproject.timeline.Notes;
import com.bitwig.dawproject.timeline.Points;
import com.bitwig.dawproject.timeline.TimeUnit;
import com.bitwig.dawproject.timeline.Timeline;
import com.bitwig.dawproject.timeline.Warp;
import com.bitwig.dawproject.timeline.Warps;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class DawProjectTest
{
enum Features
{
CUE_MARKERS,
CLIPS,
AUDIO,
NOTES,
AUTOMATION,
ALIAS_CLIPS,
PLUGINS
}
EnumSet<Features> simpleFeatures = EnumSet.of(Features.CLIPS, Features.NOTES, Features.AUDIO);
private Project createEmptyProject()
{
Referenceable.resetID();
final Project project = new Project();
project.application.name = "Test";
project.application.version = "1.0";
return project;
}
private Project createDummyProject(final int numTracks, final EnumSet<Features> features)
{
final Project project = createEmptyProject();
final Track masterTrack = Utility.createTrack("Master", EnumSet.noneOf(ContentType.class), MixerRole.master, 1, 0.5);
project.structure.add(masterTrack);
if (features.contains(Features.PLUGINS))
{
final Device device = new Vst3Plugin();
device.deviceName = "Limiter";
//device.id = UUID.randomUUID().toString();
device.deviceRole = DeviceRole.audioFX;
device.state = new FileReference();
device.state.path = "plugin-states/12323545.vstpreset";
if (masterTrack.channel.devices == null)
masterTrack.channel.devices = new ArrayList<>();
masterTrack.channel.devices.add(device);
}
project.arrangement = new Arrangement();
final var arrangementLanes = new Lanes();
arrangementLanes.timeUnit = TimeUnit.beats;
project.arrangement.lanes = arrangementLanes;
if (features.contains(Features.CUE_MARKERS))
{
final var cueMarkers = new Markers();
project.arrangement.markers = cueMarkers;
cueMarkers.markers.add(createMarker(0, "Verse"));
cueMarkers.markers.add(createMarker(24, "Chorus"));
}
for (int i = 0; i < numTracks; i++)
{
final var track = Utility.createTrack("Track " + (i+1), EnumSet.of(ContentType.notes), MixerRole.regular,1, 0.5);
project.structure.add(track);
track.color = "#" + i + i + i + i + i +i;
track.channel.destination = masterTrack.channel;
final var trackLanes = new Lanes();
trackLanes.track = track;
arrangementLanes.lanes.add(trackLanes);
if (features.contains(Features.CLIPS))
{
final var clips = new Clips();
trackLanes.lanes.add(clips);
final var clip = new Clip();
clip.name = "Clip " + i;
clip.time = 8 * i;
clip.duration = 4.0;
clips.clips.add(clip);
final var notes = new Notes();
clip.content = notes;
for (int j = 0; j < 8; j++)
{
final var note = new Note();
note.key = 36 + 12 * (j % (1 + i));
note.velocity = 0.8;
note.releaseVelocity = 0.5;
note.time = 0.5 * j;
note.duration = 0.5;
notes.notes.add(note);
}
if (features.contains(Features.ALIAS_CLIPS))
{
final var clip2 = new Clip();
clip2.name = "Alias Clip " + i;
clip2.time = 32 + 8 * i;
clip2.duration = 4.0;
clips.clips.add(clip2);
clip2.reference = notes;
}
if (i == 0 && features.contains(Features.AUTOMATION))
{
final var points = new Points();
points.target.parameter = track.channel.volume;
trackLanes.lanes.add(points);
// fade-in over 8 quarter notes
points.points.add(createPoint(0.0, 0.0, Interpolation.linear));
points.points.add(createPoint(8.0, 1.0, Interpolation.linear));
}
}
}
// Route channel 0 to 1
//project.channels.get(0).destination = project.channels.get(1);
return project;
}
private RealPoint createPoint(final double time, final double value, final Interpolation interpolation)
{
final var point = new RealPoint();
point.time = time;
point.value = value;
point.interpolation = interpolation;
return point;
}
public Marker createMarker(final double time, final String name)
{
final var markerEvent = new Marker();
markerEvent.time = time;
markerEvent.name = name;
return markerEvent;
}
@Test
public void saveDawProject() throws IOException
{
final Project project = createDummyProject(3, simpleFeatures);
final MetaData metadata = new MetaData();
final Map<File, String> embeddedFiles = new HashMap<>();
DawProject.save(project, metadata, embeddedFiles, new File("target/test.dawproject"));
DawProject.saveXML(project, new File("target/test.dawproject.xml"));
}
@Test
public void validateDawProject() throws IOException
{
final Project project = createDummyProject(3, simpleFeatures);
DawProject.validate(project);
}
@Test
public void validateComplexDawProject() throws IOException
{
final Project project = createDummyProject(3, EnumSet.allOf(Features.class));
DawProject.validate(project);
}
@Test
public void saveAndLoadDawProject() throws IOException
{
final Project project = createDummyProject(5, simpleFeatures);
final MetaData metadata = new MetaData();
final var file = File.createTempFile("testfile", ".dawproject");
final Map<File, String> embeddedFiles = new HashMap<>();
DawProject.save(project, metadata, embeddedFiles, file);
final var loadedProject = DawProject.loadProject(file);
Assert.assertEquals(project.structure.size(), loadedProject.structure.size());
Assert.assertEquals(project.scenes.size(), loadedProject.scenes.size());
}
@Test
public void saveComplexDawProject() throws IOException
{
final Project project = createDummyProject(3, EnumSet.allOf(Features.class));
final MetaData metadata = new MetaData();
final Map<File, String> embeddedFiles = new HashMap<>();
DawProject.save(project, metadata, embeddedFiles, new File("target/test-complex.dawproject"));
DawProject.saveXML(project, new File("target/test-complex.dawproject.xml"));
}
@Test
public void saveAndLoadComplexDawProject() throws IOException
{
final Project project = createDummyProject(5, EnumSet.allOf(Features.class));
final MetaData metadata = new MetaData();
final Map<File, String> embeddedFiles = new HashMap<>();
final var file = File.createTempFile("testfile2", ".dawproject");
DawProject.save(project, metadata, embeddedFiles, file);
final var loadedProject = DawProject.loadProject(file);
Assert.assertEquals(project.structure.size(), loadedProject.structure.size());
Assert.assertEquals(project.scenes.size(), loadedProject.scenes.size());
Assert.assertEquals(project.arrangement.lanes.getClass(), loadedProject.arrangement.lanes.getClass());
Assert.assertEquals(project.arrangement.markers.getClass(), loadedProject.arrangement.markers.getClass());
}
@Test
public void writeMetadataSchema() throws IOException
{
DawProject.exportSchema(new File("MetaData.xsd"), MetaData.class);
}
@Test
public void writeProjectSchema() throws IOException
{
DawProject.exportSchema(new File("Project.xsd"), Project.class);
}
@Ignore
@Test
public void loadEmbeddedFile() throws IOException
{
final File file = new File("src/test-data/0.1/bitwig/test3x.dawproject");
Assert.assertTrue(file.exists());
Assert.assertTrue(file.isFile());
// try reading project first
final Project project = DawProject.loadProject(file);
Assert.assertNotNull(project);
final InputStream inputStream = DawProject.streamEmbedded(file, "samples/RC 08 92bpm Break Sp1200.wav");
final byte[] data = inputStream.readAllBytes();
Assert.assertEquals(1380652, data.length);
}
enum AudioScenario
{
Warped,
RawBeats,
RawSeconds,
FileWithAbsolutePath,
FileWithRelativePath,
}
boolean shouldTestOffsetAndFades(final AudioScenario scenario)
{
return switch (scenario)
{
case Warped -> true;
case RawBeats -> true;
case RawSeconds -> true;
default -> false;
};
}
@Test
public void createAudioExample() throws IOException
{
for (AudioScenario scenario : AudioScenario.values())
{
createAudioExample(0, 0, scenario, false);
if (shouldTestOffsetAndFades(scenario))
{
createAudioExample(0, 0, scenario, true);
createAudioExample(1, 0, scenario, false);
createAudioExample(0, 1, scenario, false);
}
}
}
public void createAudioExample(final double playStartOffset, final double clipTime, final AudioScenario scenario, final boolean withFades) throws IOException
{
String name = "Audio" + scenario.name();
if (withFades)
name += "WithFades";
if (playStartOffset != 0)
name += "Offset";
if (clipTime != 0)
name += "Clipstart";
final Project project = createEmptyProject();
final Track masterTrack = Utility.createTrack("Master", EnumSet.noneOf(ContentType.class), MixerRole.master, 1, 0.5);
final var audioTrack = Utility.createTrack("Audio", EnumSet.of(ContentType.audio), MixerRole.regular,1, 0.5);
audioTrack.channel.destination = masterTrack.channel;
project.structure.add(masterTrack);
project.structure.add(audioTrack);
project.arrangement = new Arrangement();
project.transport = new Transport();
project.transport.tempo = new RealParameter();
project.transport.tempo.unit = Unit.bpm;
project.transport.tempo.value = 155.0;
final var arrangementLanes = new Lanes();
project.arrangement.lanes = arrangementLanes;
final var arrangementIsInSeconds = scenario == AudioScenario.RawSeconds;
project.arrangement.lanes.timeUnit = arrangementIsInSeconds ? TimeUnit.seconds : TimeUnit.beats;
final var sample = "white-glasses.wav";
Clip audioClip;
final var sampleDuration = 3.097;
final var audio = Utility.createAudio(sample, 44100, 2, sampleDuration);
if (scenario == AudioScenario.FileWithAbsolutePath)
{
audio.file.external = true;
audio.file.path = new File("test-data", sample).getAbsolutePath();
}
else if (scenario == AudioScenario.FileWithRelativePath)
{
audio.file.external = true;
audio.file.path = "../test-data/" + sample;
}
if (scenario == AudioScenario.Warped)
{
final var warps = new Warps();
warps.content = audio;
warps.contentTimeUnit = TimeUnit.seconds;
warps.events.add(Utility.createWarp(0, 0));
warps.events.add(Utility.createWarp(8, sampleDuration));
audioClip = Utility.createClip(warps, clipTime, 8);
audioClip.contentTimeUnit = TimeUnit.beats;
audioClip.playStart = playStartOffset;
if (withFades)
{
audioClip.fadeTimeUnit = TimeUnit.beats;
audioClip.fadeInTime = 0.25;
audioClip.fadeOutTime = 0.25;
}
}
else
{
audioClip = Utility.createClip(audio, clipTime, arrangementIsInSeconds ? sampleDuration : 8);
audioClip.contentTimeUnit = TimeUnit.seconds;
audioClip.playStart = playStartOffset;
audioClip.playStop = sampleDuration;
if (withFades)
{
audioClip.fadeTimeUnit = TimeUnit.seconds;
audioClip.fadeInTime = 0.1;
audioClip.fadeOutTime = 0.1;
}
}
final var clips = Utility.createClips(audioClip);
clips.track = audioTrack;
arrangementLanes.lanes.add(clips);
saveTestProject(project, name, (meta, files) -> files.put(new File("test-data/" + sample), sample));
}
@Test
public void createMIDIAutomationInClipsExample() throws IOException
{
createMIDIAutomationExample("MIDI-CC1-AutomationOnTrack", false, false);
createMIDIAutomationExample("MIDI-CC1-AutomationInClips", true, false);
createMIDIAutomationExample("MIDI-PitchBend-AutomationOnTrack", false, true);
createMIDIAutomationExample("MIDI-PitchBend-AutomationInClips", true, true);
}
public void createMIDIAutomationExample(final String name, final boolean inClips, final boolean isPitchBend) throws IOException
{
final Project project = createEmptyProject();
final Track masterTrack = Utility.createTrack("Master", EnumSet.noneOf(ContentType.class), MixerRole.master, 1, 0.5);
final var instrumentTrack = Utility.createTrack("Notes", EnumSet.of(ContentType.notes), MixerRole.regular,1, 0.5);
instrumentTrack.channel.destination = masterTrack.channel;
project.structure.add(masterTrack);
project.structure.add(instrumentTrack);
project.arrangement = new Arrangement();
project.transport = new Transport();
project.transport.tempo = new RealParameter();
project.transport.tempo.unit = Unit.bpm;
project.transport.tempo.value = 123.0;
final var arrangementLanes = new Lanes();
project.arrangement.lanes = arrangementLanes;
project.arrangement.lanes.timeUnit = TimeUnit.beats;
// Create some mod-wheel or pitch-bend automation
final var automation = new Points();
automation.unit = Unit.normalized;
if (isPitchBend)
{
automation.target.expression = ExpressionType.pitchBend;
automation.target.channel = 0;
}
else
{
automation.target.expression = ExpressionType.channelController;
automation.target.channel = 0;
automation.target.controller = 1;
}
automation.points.add(createPoint(0, 0.0, Interpolation.linear));
automation.points.add(createPoint(1, 0.0, Interpolation.linear));
automation.points.add(createPoint(2, 0.5, Interpolation.linear));
automation.points.add(createPoint(3, 0.5, Interpolation.linear));
automation.points.add(createPoint(4, 1.0, Interpolation.linear));
automation.points.add(createPoint(5, 1.0, Interpolation.linear));
automation.points.add(createPoint(6, 0.5, Interpolation.linear));
automation.points.add(createPoint(7, 1, Interpolation.hold));
automation.points.add(createPoint(8, 0.5, Interpolation.hold));
if (inClips)
{
final var noteClip = Utility.createClip(automation, 0, 8);
final var clips = Utility.createClips(noteClip);
clips.track = instrumentTrack;
arrangementLanes.lanes.add(clips);
}
else
{
automation.track = instrumentTrack;
arrangementLanes.lanes.add(automation);
}
saveTestProject(project, name, null);
}
@Test
public void testDoubleAdapter() throws Exception
{
final var adapter = new DoubleAdapter();
Assert.assertEquals(adapter.unmarshal("-inf").doubleValue(), Double.NEGATIVE_INFINITY, 0);
Assert.assertEquals(adapter.unmarshal("inf").doubleValue(), Double.POSITIVE_INFINITY, 0);
Assert.assertEquals("inf", adapter.marshal(Double.POSITIVE_INFINITY));
Assert.assertEquals("-inf", adapter.marshal(Double.NEGATIVE_INFINITY));
}
private static void saveTestProject(final Project project, final String name, final BiConsumer<MetaData, Map<File, String>> configurer) throws IOException
{
final MetaData metadata = new MetaData();
final Map<File, String> embeddedFiles = new HashMap<>();
if (configurer != null)
configurer.accept(metadata, embeddedFiles);
DawProject.save(project, metadata, embeddedFiles, new File("target/" + name + ".dawproject"));
DawProject.saveXML(project, new File("target/" + name + ".xml"));
DawProject.validate(project);
}
}