Audacity 3.2.0
NoteTrack.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 NoteTrack.cpp
6
7 Dominic Mazzoni
8
9*******************************************************************//*******************************************************************/
15#include "NoteTrack.h"
16
17#include <wx/wxcrtvararg.h>
18
19#if defined(USE_MIDI)
20#include "WrapAllegro.h"
21
22#include <sstream>
23
24#define ROUND(x) ((int) ((x) + 0.5))
25
26#include "Prefs.h"
27#include "Project.h"
29#include "TimeWarper.h"
30
31#ifdef SONIFY
32#include <portmidi.h>
33
34#define SON_PROGRAM 0
35#define SON_AutoSave 67
36#define SON_ModifyState 60
37#define SON_NoteBackground 72
38#define SON_NoteForeground 74
39#define SON_Measures 76 /* "bar line" */
40#define SON_Serialize 77
41#define SON_Unserialize 79
42#define SON_VEL 100
43
44
45PmStream *sonMidiStream;
46bool sonificationStarted = false;
47
49{
50 PmError err = Pm_OpenOutput(&sonMidiStream, Pm_GetDefaultOutputDeviceID(),
51 NULL, 0, NULL, NULL, 0);
52 if (err) sonMidiStream = NULL;
53 if (sonMidiStream)
54 Pm_WriteShort(sonMidiStream, 0, Pm_Message(0xC0, SON_PROGRAM, 0));
55 sonificationStarted = true;
56}
57
58
60{
61 if (sonMidiStream) Pm_Close(sonMidiStream);
62 sonificationStarted = false;
63}
64
65
66
67
68void SonifyNoteOnOff(int p, int v)
69{
70 if (!sonificationStarted)
72 if (sonMidiStream)
73 Pm_WriteShort(sonMidiStream, 0, Pm_Message(0x90, p, v));
74}
75
76#define SONFNS(name) \
77 void SonifyBegin ## name() { SonifyNoteOnOff(SON_ ## name, SON_VEL); } \
78 void SonifyEnd ## name() { SonifyNoteOnOff(SON_ ## name, 0); }
79
80SONFNS(NoteBackground)
81SONFNS(NoteForeground)
82SONFNS(Measures)
83SONFNS(Serialize)
84SONFNS(Unserialize)
85SONFNS(ModifyState)
86SONFNS(AutoSave)
87
88#undef SONFNS
89
90#endif
91
92
94 : mpTrack{ track.SharedPointer<const NoteTrack>() }
95{}
96
98
100{
101 return mpTrack->mOrigin;
102}
103
105{
106 return Start() + mpTrack->GetSeq().get_real_dur();
107}
108
110{
111 return 1;
112}
113
114std::shared_ptr<ChannelInterval>
116{
117 if (iChannel == 0)
118 return std::make_shared<ChannelInterval>();
119 return {};
120}
121
123
125{}
126
128{
129 return false;
130}
131
133 "notetrack",
135};
136
138{
139 auto &tracks = TrackList::Get( project );
140 auto result = tracks.Add( std::make_shared<NoteTrack>());
141 result->AttachedTrackObjects::BuildAll();
142 return result;
143}
144
147{
148 SetName(_("Note Track"));
149
150 mSeq = NULL;
152}
153
155{
156}
157
158Alg_seq &NoteTrack::GetSeq() const
159{
160 if (!mSeq) {
162 mSeq = std::make_unique<Alg_seq>();
163 else {
164 std::unique_ptr<Alg_track> alg_track
165 { Alg_seq::unserialize
167 wxASSERT(alg_track->get_type() == 's');
168 mSeq.reset( static_cast<Alg_seq*>(alg_track.release()) );
169
170 // Preserve the invariant that at most one of the representations is
171 // valid
172 mSerializationBuffer.reset();
174 }
175 }
176 wxASSERT(mSeq);
177 return *mSeq;
178}
179
181{
182 auto duplicate = std::make_shared<NoteTrack>();
183 duplicate->Init(*this);
184 // The duplicate begins life in serialized state. Often the duplicate is
185 // pushed on the Undo stack. Then we want to un-serialize it (or a further
186 // copy) only on demand after an Undo.
187 if (mSeq) {
189 wxASSERT(!mSerializationBuffer);
190 // serialize from this to duplicate's mSerializationBuffer
191 void *buffer;
192 mSeq->serialize(&buffer,
193 &duplicate->mSerializationLength);
194 duplicate->mSerializationBuffer.reset( (char*)buffer );
196 }
197 else if (mSerializationBuffer) {
198 // Copy already serialized data.
199 wxASSERT(!mSeq);
200 duplicate->mSerializationLength = this->mSerializationLength;
201 duplicate->mSerializationBuffer.reset
202 ( safenew char[ this->mSerializationLength ] );
203 memcpy( duplicate->mSerializationBuffer.get(),
204 this->mSerializationBuffer.get(), this->mSerializationLength );
205 }
206 else {
207 // We are duplicating a default-constructed NoteTrack, and that's okay
208 }
209
210 // copy some other fields here
211 Attachments &attachments = *duplicate;
212 attachments = *this;
213
214 duplicate->SetVisibleChannels(GetVisibleChannels());
215 duplicate->MoveTo(mOrigin);
216 duplicate->SetVelocity(GetVelocity());
217 return duplicate;
218}
219
220void NoteTrack::WarpAndTransposeNotes(double t0, double t1,
221 const TimeWarper &warper,
222 double semitones)
223{
224 double offset = this->mOrigin; // track is shifted this amount
225 auto &seq = GetSeq();
226 seq.convert_to_seconds(); // make sure time units are right
227 t1 -= offset; // adjust time range to compensate for track offset
228 t0 -= offset;
229 if (t1 > seq.get_dur()) { // make sure t0, t1 are within sequence
230 t1 = seq.get_dur();
231 if (t0 >= t1) return;
232 }
233 Alg_iterator iter(mSeq.get(), false);
234 iter.begin();
235 Alg_event_ptr event;
236 while (0 != (event = iter.next()) && event->time < t1) {
237 if (event->is_note() && event->time >= t0) {
238 event->set_pitch(event->get_pitch() + semitones);
239 }
240 }
241 iter.end();
242 // now, use warper to warp the tempo map
243 seq.convert_to_beats(); // beats remain the same
244 Alg_time_map_ptr map = seq.get_time_map();
245 map->insert_beat(t0, map->time_to_beat(t0));
246 map->insert_beat(t1, map->time_to_beat(t1));
247 int i, len = map->length();
248 for (i = 0; i < len; i++) {
249 Alg_beat &beat = map->beats[i];
250 beat.time = warper.Warp(beat.time + offset) - offset;
251 }
252 // about to redisplay, so might as well convert back to time now
253 seq.convert_to_seconds();
254}
255
256void NoteTrack::SetSequence(std::unique_ptr<Alg_seq> &&seq)
257{
258 mSeq = std::move(seq);
259}
260
262{
263 FILE *debugOutput;
264
265 debugOutput = fopen("debugOutput.txt", "wt");
266 wxFprintf(debugOutput, "Importing MIDI...\n");
267
268 // This is called for debugging purposes. Do not compute mSeq on demand
269 // with GetSeq()
270 if (mSeq) {
271 int i = 0;
272
273 while(i < mSeq->length()) {
274 wxFprintf(debugOutput, "--\n");
275 wxFprintf(debugOutput, "type: %c\n",
276 ((Alg_event_ptr)mSeq->track_list.tracks[i])->get_type());
277 wxFprintf(debugOutput, "time: %f\n",
278 ((Alg_event_ptr)mSeq->track_list.tracks[i])->time);
279 wxFprintf(debugOutput, "channel: %li\n",
280 ((Alg_event_ptr)mSeq->track_list.tracks[i])->chan);
281
282 if(((Alg_event_ptr)mSeq->track_list.tracks[i])->get_type() == wxT('n'))
283 {
284 wxFprintf(debugOutput, "pitch: %f\n",
285 ((Alg_note_ptr)mSeq->track_list.tracks[i])->pitch);
286 wxFprintf(debugOutput, "duration: %f\n",
287 ((Alg_note_ptr)mSeq->track_list.tracks[i])->dur);
288 wxFprintf(debugOutput, "velocity: %f\n",
289 ((Alg_note_ptr)mSeq->track_list.tracks[i])->loud);
290 }
291 else if(((Alg_event_ptr)mSeq->track_list.tracks[i])->get_type() == wxT('n'))
292 {
293 wxFprintf(debugOutput, "key: %li\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->get_identifier());
294 wxFprintf(debugOutput, "attribute type: %c\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type());
295 wxFprintf(debugOutput, "attribute: %s\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_name());
296
297 if(((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type() == wxT('r'))
298 {
299 wxFprintf(debugOutput, "value: %f\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.r);
300 }
301 else if(((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type() == wxT('i')) {
302 wxFprintf(debugOutput, "value: %li\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.i);
303 }
304 else if(((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type() == wxT('s')) {
305 wxFprintf(debugOutput, "value: %s\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.s);
306 }
307 else {}
308 }
309
310 i++;
311 }
312 }
313 else {
314 wxFprintf(debugOutput, "No sequence defined!\n");
315 }
316
317 fclose(debugOutput);
318}
319
320Track::Holder NoteTrack::Cut(double t0, double t1)
321{
322 if (t1 < t0)
324
325 double len = t1-t0;
326 //auto delta = -(
327 //( std::min( t1, GetEndTime() ) ) - ( std::max( t0, GetStartTime() ) )
328 //);
329
330 auto newTrack = std::make_shared<NoteTrack>();
331
332 newTrack->Init(*this);
333
334 auto &seq = GetSeq();
335 seq.convert_to_seconds();
336 newTrack->mSeq.reset(seq.cut(t0 - mOrigin, len, false));
337 newTrack->MoveTo(0);
338
339 // Not needed
340 // Alg_seq::cut seems to handle this
341 //AddToDuration( delta );
342
343 // What should be done with the rest of newTrack's members?
344 //(mBottomNote,
345 // mSerializationBuffer, mSerializationLength, mVisibleChannels)
346
347 return newTrack;
348}
349
350Track::Holder NoteTrack::Copy(double t0, double t1, bool) const
351{
352 if (t1 < t0)
354
355 double len = t1-t0;
356
357 auto newTrack = std::make_shared<NoteTrack>();
358
359 newTrack->Init(*this);
360
361 auto &seq = GetSeq();
362 seq.convert_to_seconds();
363 newTrack->mSeq.reset(seq.copy(t0 - mOrigin, len, false));
364 newTrack->MoveTo(0);
365
366 // What should be done with the rest of newTrack's members?
367 // (mBottomNote, mSerializationBuffer,
368 // mSerializationLength, mVisibleChannels)
369
370 return newTrack;
371}
372
373bool NoteTrack::Trim(double t0, double t1)
374{
375 if (t1 < t0)
376 return false;
377 auto &seq = GetSeq();
378 //auto delta = -(
379 //( GetEndTime() - std::min( GetEndTime(), t1 ) ) +
380 //( std::max(t0, GetStartTime()) - GetStartTime() )
381 //);
382 seq.convert_to_seconds();
383 // DELETE way beyond duration just in case something is out there:
384 seq.clear(t1 - mOrigin, seq.get_dur() + 10000.0, false);
385 // Now that stuff beyond selection is cleared, clear before selection:
386 seq.clear(0.0, t0 - mOrigin, false);
387 // want starting time to be t0
388 MoveTo(t0);
389
390 // Not needed
391 // Alg_seq::clear seems to handle this
392 //AddToDuration( delta );
393
394 return true;
395}
396
397void NoteTrack::Clear(double t0, double t1)
398{
399 if (t1 < t0)
401
402 double len = t1-t0;
403
404 auto &seq = GetSeq();
405
406 auto offset = mOrigin;
407 auto start = t0 - offset;
408 if (start < 0.0) {
409 // AlgSeq::clear will shift the cleared interval, not changing len, if
410 // start is negative. That's not what we want to happen.
411 if (len > -start) {
412 seq.clear(0, len + start, false);
413 MoveTo(t0);
414 }
415 else
416 MoveTo(offset - len);
417 }
418 else {
419 //auto delta = -(
420 //( std::min( t1, GetEndTime() ) ) - ( std::max( t0, GetStartTime() ) )
421 //);
422 seq.clear(start, len, false);
423
424 // Not needed
425 // Alg_seq::clear seems to handle this
426 // AddToDuration( delta );
427 }
428}
429
430void NoteTrack::Paste(double t, const Track &src)
431{
432 // Paste inserts src at time t. If src has a positive offset,
433 // the offset is treated as silence which is also inserted. If
434 // the offset is negative, the offset is ignored and the ENTIRE
435 // src is inserted (otherwise, we would either lose data from
436 // src by not inserting things at negative times, or inserting
437 // things at negative times could overlap things already in
438 // the destination track).
439
440 //Check that src is a non-NULL NoteTrack
441 bool bOk = src.TypeSwitch<bool>( [&](const NoteTrack &other) {
442
443 auto myOffset = this->mOrigin;
444 if (t < myOffset) {
445 // workaround strange behavior described at
446 // http://bugzilla.audacityteam.org/show_bug.cgi?id=1735#c3
447 MoveTo(t);
448 InsertSilence(t, myOffset - t);
449 }
450
451 double delta = 0.0;
452 auto &seq = GetSeq();
453 auto offset = other.mOrigin;
454 if (offset > 0) {
455 seq.convert_to_seconds();
456 seq.insert_silence(t - mOrigin, offset);
457 t += offset;
458 // Is this needed or does Alg_seq::insert_silence take care of it?
459 //delta += offset;
460 }
461
462 // This seems to be needed:
463 delta += std::max(0.0, t - GetEndTime());
464
465 // This, not:
466 //delta += other.GetSeq().get_real_dur();
467
468 seq.paste(t - mOrigin, &other.GetSeq());
469
470 AddToDuration(delta);
471
472 return true;
473 });
474
475 if (!bOk)
476 // THROW_INCONSISTENCY_EXCEPTION; // ?
477 (void)0;// intentionally do nothing
478}
479
480void NoteTrack::Silence(double t0, double t1, ProgressReporter)
481{
482 if (t1 < t0)
484
485 auto len = t1 - t0;
486
487 auto &seq = GetSeq();
488 seq.convert_to_seconds();
489 // XXX: do we want to set the all param?
490 // If it's set, then it seems like notes are silenced if they start or end in the range,
491 // otherwise only if they start in the range. --Poke
492 seq.silence(t0 - mOrigin, len, false);
493}
494
495void NoteTrack::InsertSilence(double t, double len)
496{
497 if (len < 0)
499
500 auto &seq = GetSeq();
501 seq.convert_to_seconds();
502 seq.insert_silence(t - mOrigin, len);
503
504 // is this needed?
505 // AddToDuration( len );
506}
507
508void NoteTrack::SetVelocity(float velocity)
509{
510 if (GetVelocity() != velocity) {
511 DoSetVelocity(velocity);
512 Notify(false);
513 }
514}
515
516void NoteTrack::DoSetVelocity(float velocity)
517{
518 mVelocity.store(velocity, std::memory_order_relaxed);
519}
520
521// Call this function to manipulate the underlying sequence data. This is
522// NOT the function that handles horizontal dragging.
523bool NoteTrack::Shift(double t) // t is always seconds
524{
525 if (t > 0) {
526 auto &seq = GetSeq();
527 // insert an even number of measures
528 seq.convert_to_beats();
529 // get initial tempo
530 double tempo = seq.get_tempo(0.0);
531 double beats_per_measure = seq.get_bar_len(0.0);
532 int m = ROUND(t * tempo / beats_per_measure);
533 // need at least 1 measure, so if we rounded down to zero, fix it
534 if (m == 0) m = 1;
535 // compute NEW tempo so that m measures at NEW tempo take t seconds
536 tempo = beats_per_measure * m / t; // in beats per second
537 seq.insert_silence(0.0, beats_per_measure * m);
538 seq.set_tempo(tempo * 60.0 /* bpm */, 0.0, beats_per_measure * m);
539 seq.write("afterShift.gro");
540 } else if (t < 0) {
541 auto &seq = GetSeq();
542 seq.convert_to_seconds();
543 seq.clear(0, t, true);
544 } else { // offset is zero, no modifications
545 return false;
546 }
547 return true;
548}
549
551{
552 // Alg_seq knows nothing about offset, so remove offset time
553 double seq_time = time - mOrigin;
554 double beat;
555 auto &seq = GetSeq();
556 seq_time = seq.nearest_beat_time(seq_time, &beat);
557 // add the offset back in to get "actual" audacity track time
558 return { seq_time + mOrigin, beat };
559}
560
562{
563 static const Track::TypeInfo info{
564 { "note", "midi", XO("Note Track") }, true,
566 return info;
567}
568
569auto NoteTrack::GetTypeInfo() const -> const TypeInfo &
570{
571 return typeInfo();
572}
573
575{
576 return typeInfo();
577}
578
580{
581 auto pNewTrack = std::make_shared<NoteTrack>();
582 pNewTrack->Init(*this);
583 pNewTrack->Paste(0.0, *this);
584 list.Add(pNewTrack);
585 return pNewTrack;
586}
587
589{
590 return 1;
591}
592
593std::shared_ptr<WideChannelGroupInterval>
595{
596 if (iInterval == 0)
597 // Just one, and no extra info in it!
598 return std::make_shared<Interval>(*this);
599 return {};
600}
601
602void NoteTrack::AddToDuration( double delta )
603{
604 auto &seq = GetSeq();
605#if 0
606 // PRL: Would this be better ?
607 seq.set_real_dur( seq.get_real_dur() + delta );
608#else
609 seq.convert_to_seconds();
610 seq.set_dur( seq.get_dur() + delta );
611#endif
612}
613
615 ( QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur )
616{
617 auto &seq = GetSeq();
618 bool result = seq.stretch_region( t0.second, t1.second, newDur );
619 if (result) {
620 const auto oldDur = t1.first - t0.first;
621 AddToDuration( newDur - oldDur );
622 }
623 return result;
624}
625
626namespace
627{
628 void swap(std::unique_ptr<Alg_seq> &a, std::unique_ptr<Alg_seq> &b)
629 {
630 std::unique_ptr<Alg_seq> tmp = std::move(a);
631 a = std::move(b);
632 b = std::move(tmp);
633 }
634}
635
636Alg_seq *NoteTrack::MakeExportableSeq(std::unique_ptr<Alg_seq> &cleanup) const
637{
638 cleanup.reset();
639 double offset = mOrigin;
640 if (offset == 0)
641 return &GetSeq();
642 // make a copy, deleting events that are shifted before time 0
643 double start = -offset;
644 if (start < 0) start = 0;
645 // notes that begin before "start" are not included even if they
646 // extend past "start" (because "all" parameter is set to false)
647 cleanup.reset( GetSeq().copy(start, GetSeq().get_dur() - start, false) );
648 auto seq = cleanup.get();
649 if (offset > 0) {
650 {
651 // swap cleanup and mSeq so that Shift operates on the NEW copy
652 swap( this->mSeq, cleanup );
653 auto cleanup2 = finally( [&] { swap( this->mSeq, cleanup ); } );
654
655 const_cast< NoteTrack *>( this )->Shift(offset);
656 }
657#ifdef OLD_CODE
658 // now shift events by offset. This must be done with an integer
659 // number of measures, so first, find the beats-per-measure
660 double beats_per_measure = 4.0;
661 Alg_time_sig_ptr tsp = NULL;
662 if (seq->time_sig.length() > 0 && seq->time_sig[0].beat < ALG_EPS) {
663 // there is an initial time signature
664 tsp = &(seq->time_sig[0]);
665 beats_per_measure = (tsp->num * 4) / tsp->den;
666 }
667 // also need the initial tempo
668 double bps = ALG_DEFAULT_BPM / 60;
669 Alg_time_map_ptr map = seq->get_time_map();
670 Alg_beat_ptr bp = &(map->beats[0]);
671 if (bp->time < ALG_EPS) { // tempo change at time 0
672 if (map->beats.len > 1) { // compute slope to get tempo
673 bps = (map->beats[1].beat - map->beats[0].beat) /
674 (map->beats[1].time - map->beats[0].time);
675 } else if (seq->get_time_map()->last_tempo_flag) {
676 bps = seq->get_time_map()->last_tempo;
677 }
678 }
679 // find closest number of measures to fit in the gap
680 // number of measures is offset / measure_time
681 double measure_time = beats_per_measure / bps; // seconds per measure
682 int n = ROUND(offset / measure_time);
683 if (n == 0) n = 1;
684 // we will insert n measures. Compute the desired duration of each.
685 measure_time = offset / n;
686 bps = beats_per_measure / measure_time;
687 // insert integer multiple of measures at beginning
688 seq->convert_to_beats();
689 seq->insert_silence(0, beats_per_measure * n);
690 // make sure time signature at 0 is correct
691 if (tsp) {
692 seq->set_time_sig(0, tsp->num, tsp->den);
693 }
694 // adjust tempo to match offset
695 seq->set_tempo(bps * 60.0, 0, beats_per_measure * n);
696#endif
697 } else {
698 auto &mySeq = GetSeq();
699 // if offset is negative, it might not be a multiple of beats, but
700 // we want to preserve the relative positions of measures. I.e. we
701 // should shift barlines and time signatures as well as notes.
702 // Insert a time signature at the first bar-line if necessary.
703
704 // Translate start from seconds to beats and call it beat:
705 double beat = mySeq.get_time_map()->time_to_beat(start);
706 // Find the time signature in mySeq in effect at start (beat):
707 int i = mySeq.time_sig.find_beat(beat);
708 // i is where you would insert a NEW time sig at beat,
709 // Case 1: beat coincides with a time sig at i. Time signature
710 // at beat means that there is a barline at beat, so when beat
711 // is shifted to 0, the relative barline positions are preserved
712 if (mySeq.time_sig.length() > 0 &&
713 within(beat, mySeq.time_sig[i].beat, ALG_EPS)) {
714 // beat coincides with time signature change, so offset must
715 // be a multiple of beats
716 /* do nothing */ ;
717 // Case 2: there is no time signature before beat.
718 } else if (i == 0 && (mySeq.time_sig.length() == 0 ||
719 mySeq.time_sig[i].beat > beat)) {
720 // If beat does not fall on an implied barline, we need to
721 // insert a time signature.
722 double measures = beat / 4.0;
723 double imeasures = ROUND(measures);
724 if (!within(measures, imeasures, ALG_EPS)) {
725 double bar_offset = ((int)(measures) + 1) * 4.0 - beat;
726 seq->set_time_sig(bar_offset, 4, 4);
727 }
728 // This case should never be true because if i == 0, either there
729 // are no time signatures before beat (Case 2),
730 // or there is one time signature at beat (Case 1)
731 } else if (i == 0) {
732 /* do nothing (might be good to assert(false)) */ ;
733 // Case 3: i-1 must be the effective time sig position
734 } else {
735 i -= 1; // index the time signature in effect at beat
736 Alg_time_sig_ptr tsp = &(mySeq.time_sig[i]);
737 double beats_per_measure = (tsp->num * 4) / tsp->den;
738 double measures = (beat - tsp->beat) / beats_per_measure;
739 int imeasures = ROUND(measures);
740 if (!within(measures, imeasures, ALG_EPS)) {
741 // beat is not on a measure, so we need to insert a time sig
742 // to force a bar line at the first measure location after
743 // beat
744 double bar = tsp->beat + beats_per_measure * ((int)(measures) + 1);
745 double bar_offset = bar - beat;
746 // insert NEW time signature at bar_offset in NEW sequence
747 // It will have the same time signature, but the position will
748 // force a barline to match the barlines in mSeq
749 seq->set_time_sig(bar_offset, tsp->num, tsp->den);
750 }
751 // else beat coincides with a barline, so no need for an extra
752 // time signature to force barline alignment
753 }
754 }
755 return seq;
756}
757
758
759bool NoteTrack::ExportMIDI(const wxString &f) const
760{
761 std::unique_ptr<Alg_seq> cleanup;
762 auto seq = MakeExportableSeq(cleanup);
763 bool rslt = seq->smf_write(f.mb_str());
764 return rslt;
765}
766
768 wxT("/FileFormats/AllegroStyleChoice"),
769 {
770 EnumValueSymbol{ wxT("Seconds"), XXO("&Seconds") },
771 /* i18n-hint: The music theory "beat" */
772 EnumValueSymbol{ wxT("Beats"), XXO("&Beats") },
773 },
774 0, // true
775
776 // for migrating old preferences:
777 {
778 true, false,
779 },
780 wxT("/FileFormats/AllegroStyle"),
781};
782
783bool NoteTrack::ExportAllegro(const wxString &f) const
784{
785 double offset = mOrigin;
786 auto in_seconds = AllegroStyleSetting.ReadEnum();
787 auto &seq = GetSeq();
788 if (in_seconds) {
789 seq.convert_to_seconds();
790 } else {
791 seq.convert_to_beats();
792 }
793 return seq.write(f.mb_str(), offset);
794}
795
796
797namespace {
798bool IsValidVisibleChannels(const int nValue)
799{
800 return (nValue >= 0 && nValue < (1 << 16));
801}
802}
803
804bool NoteTrack::HandleXMLTag(const std::string_view& tag, const AttributesList &attrs)
805{
806 if (tag == "notetrack") {
807 for (auto pair : attrs)
808 {
809 auto attr = pair.first;
810 auto value = pair.second;
811
812 long nValue;
813 double dblValue;
814 if (this->Track::HandleCommonXMLAttribute(attr, value))
815 ;
816 else if (this->Attachments::FindIf([&](auto &attachment){
817 return attachment.HandleAttribute(pair);
818 }))
819 ;
820 else if (this->PlayableTrack::HandleXMLAttribute(attr, value))
821 {}
822 else if (attr == "offset" && value.TryGet(dblValue))
823 MoveTo(dblValue);
824 else if (attr == "visiblechannels") {
825 if (!value.TryGet(nValue) ||
826 !IsValidVisibleChannels(nValue))
827 return false;
828 SetVisibleChannels(nValue);
829 }
830 else if (attr == "velocity" && value.TryGet(dblValue))
831 DoSetVelocity(static_cast<float>(dblValue));
832 else if (attr == "data") {
833 std::string s(value.ToWString());
834 std::istringstream data(s);
835 mSeq = std::make_unique<Alg_seq>(data, false);
836 }
837 } // while
838 return true;
839 }
840 return false;
841}
842
843XMLTagHandler *NoteTrack::HandleXMLChild(const std::string_view& WXUNUSED(tag))
844{
845 return NULL;
846}
847
848void NoteTrack::WriteXML(XMLWriter &xmlFile) const
849// may throw
850{
851 std::ostringstream data;
852 Track::Holder holder;
853 const NoteTrack *saveme = this;
854 if (!mSeq) {
855 // replace saveme with an (unserialized) duplicate, which is
856 // destroyed at end of function.
857 holder = Clone(false);
858 saveme = static_cast<NoteTrack*>(holder.get());
859 }
860 saveme->GetSeq().write(data, true);
861 xmlFile.StartTag(wxT("notetrack"));
862 saveme->Track::WriteCommonXMLAttributes( xmlFile );
864 xmlFile.WriteAttr(wxT("offset"), saveme->mOrigin);
865 xmlFile.WriteAttr(wxT("visiblechannels"),
866 static_cast<int>(saveme->GetVisibleChannels()));
867
868 xmlFile.WriteAttr(wxT("velocity"),
869 static_cast<double>(saveme->GetVelocity()));
870 saveme->Attachments::ForEach([&](auto &attachment){
871 attachment.WriteXML(xmlFile);
872 });
873 xmlFile.WriteAttr(wxT("data"), wxString(data.str().c_str(), wxConvUTF8));
874 xmlFile.EndTag(wxT("notetrack"));
875}
876
877#include <wx/log.h>
878#include <wx/sstream.h>
879#include <wx/txtstrm.h>
880#include "AudioIOBase.h"
881#include "portmidi.h"
882
883// FIXME: When EXPERIMENTAL_MIDI_IN is added (eventually) this should also be enabled -- Poke
885{
886 wxStringOutputStream o;
887 wxTextOutputStream s(o, wxEOL_UNIX);
888
889 if (AudioIOBase::Get()->IsStreamActive()) {
890 return XO("Stream is active ... unable to gather information.\n")
891 .Translation();
892 }
893
894
895 // XXX: May need to trap errors as with the normal device info
896 int recDeviceNum = Pm_GetDefaultInputDeviceID();
897 int playDeviceNum = Pm_GetDefaultOutputDeviceID();
898 int cnt = Pm_CountDevices();
899
900 // PRL: why only into the log?
901 wxLogDebug(wxT("PortMidi reports %d MIDI devices"), cnt);
902
903 s << wxT("==============================\n");
904 s << XO("Default recording device number: %d\n").Format( recDeviceNum );
905 s << XO("Default playback device number: %d\n").Format( playDeviceNum );
906
907 auto recDevice = MIDIRecordingDevice.Read();
908 auto playDevice = MIDIPlaybackDevice.Read();
909
910 // This gets info on all available audio devices (input and output)
911 if (cnt <= 0) {
912 s << XO("No devices found\n");
913 return o.GetString();
914 }
915
916 for (int i = 0; i < cnt; i++) {
917 s << wxT("==============================\n");
918
919 const PmDeviceInfo* info = Pm_GetDeviceInfo(i);
920 if (!info) {
921 s << XO("Device info unavailable for: %d\n").Format( i );
922 continue;
923 }
924
925 wxString name = wxSafeConvertMB2WX(info->name);
926 wxString hostName = wxSafeConvertMB2WX(info->interf);
927
928 s << XO("Device ID: %d\n").Format( i );
929 s << XO("Device name: %s\n").Format( name );
930 s << XO("Host name: %s\n").Format( hostName );
931 /* i18n-hint: Supported, meaning made available by the system */
932 s << XO("Supports output: %d\n").Format( info->output );
933 /* i18n-hint: Supported, meaning made available by the system */
934 s << XO("Supports input: %d\n").Format( info->input );
935 s << XO("Opened: %d\n").Format( info->opened );
936
937 if (name == playDevice && info->output)
938 playDeviceNum = i;
939
940 if (name == recDevice && info->input)
941 recDeviceNum = i;
942
943 // XXX: This is only done because the same was applied with PortAudio
944 // If PortMidi returns -1 for the default device, use the first one
945 if (recDeviceNum < 0 && info->input){
946 recDeviceNum = i;
947 }
948 if (playDeviceNum < 0 && info->output){
949 playDeviceNum = i;
950 }
951 }
952
953 bool haveRecDevice = (recDeviceNum >= 0);
954 bool havePlayDevice = (playDeviceNum >= 0);
955
956 s << wxT("==============================\n");
957 if (haveRecDevice)
958 s << XO("Selected MIDI recording device: %d - %s\n").Format( recDeviceNum, recDevice );
959 else
960 s << XO("No MIDI recording device found for '%s'.\n").Format( recDevice );
961
962 if (havePlayDevice)
963 s << XO("Selected MIDI playback device: %d - %s\n").Format( playDeviceNum, playDevice );
964 else
965 s << XO("No MIDI playback device found for '%s'.\n").Format( playDevice );
966
967 // Mention our conditional compilation flags for Alpha only
968#ifdef IS_ALPHA
969
970 // Not internationalizing these alpha-only messages
971 s << wxT("==============================\n");
972#ifdef EXPERIMENTAL_MIDI_IN
973 s << wxT("EXPERIMENTAL_MIDI_IN is enabled\n");
974#else
975 s << wxT("EXPERIMENTAL_MIDI_IN is NOT enabled\n");
976#endif
977
978#endif
979
980 return o.GetString();
981}
982
983StringSetting MIDIPlaybackDevice{ L"/MidiIO/PlaybackDevice", L"" };
984StringSetting MIDIRecordingDevice{ L"/MidiIO/RecordingDevice", L"" };
985IntSetting MIDISynthLatency_ms{ L"/MidiIO/SynthLatency", 5 };
986
987#endif // USE_MIDI
wxT("CloseDown"))
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
MessageBoxException for violation of preconditions or assertions.
#define THROW_INCONSISTENCY_EXCEPTION
Throw InconsistencyException, using C++ preprocessor to identify the source code location.
#define _(s)
Definition: Internat.h:73
void PmStream
Definition: MIDIPlay.h:19
#define safenew
Definition: MemoryX.h:9
IntSetting MIDISynthLatency_ms
Definition: NoteTrack.cpp:985
StringSetting MIDIRecordingDevice
Definition: NoteTrack.cpp:984
static ProjectFileIORegistry::ObjectReaderEntry readerEntry
Definition: NoteTrack.cpp:132
wxString GetMIDIDeviceInfo()
Definition: NoteTrack.cpp:884
StringSetting MIDIPlaybackDevice
Definition: NoteTrack.cpp:983
#define ROUND(x)
Definition: NoteTrack.cpp:24
static const Track::TypeInfo & typeInfo()
Definition: NoteTrack.cpp:561
#define SonifyEndSerialize()
Definition: NoteTrack.h:238
#define SonifyBeginSerialize()
Definition: NoteTrack.h:237
#define SonifyEndSonification()
Definition: NoteTrack.h:230
#define SonifyBeginSonification()
Definition: NoteTrack.h:229
std::pair< double, double > QuantizedTimeAndBeat
Definition: NoteTrack.h:50
const auto tracks
const auto project
Contains declarations for TimeWarper, IdentityTimeWarper, ShiftTimeWarper, LinearTimeWarper,...
std::function< void(double)> ProgressReporter
Definition: Track.h:48
bool within(A a, B b, DIST d)
Definition: TrackPanel.cpp:170
std::pair< std::string_view, XMLAttributeValueView > Attribute
Definition: XMLTagHandler.h:39
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:94
double GetEndTime() const
Get the maximum of End() values of intervals, or 0 when none.
Definition: Channel.cpp:61
Utility to register hooks into a host class that attach client data.
Definition: ClientData.h:229
ClientData * FindIf(const Function &function)
Return pointer to first attachment in this that is not null and satisfies a predicate,...
Definition: ClientData.h:497
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Enum ReadEnum() const
Definition: Prefs.h:534
Specialization of Setting for int.
Definition: Prefs.h:356
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:78
void SetSequence(std::unique_ptr< Alg_seq > &&seq)
Definition: NoteTrack.cpp:256
const TypeInfo & GetTypeInfo() const override
Definition: NoteTrack.cpp:569
static const TypeInfo & ClassTypeInfo()
Definition: NoteTrack.cpp:574
bool ExportAllegro(const wxString &f) const
Definition: NoteTrack.cpp:783
Track::Holder Cut(double t0, double t1) override
Create tracks and modify this track.
Definition: NoteTrack.cpp:320
virtual ~NoteTrack()
Definition: NoteTrack.cpp:154
void AddToDuration(double delta)
Definition: NoteTrack.cpp:602
size_t NIntervals() const override
Report the number of intervals.
Definition: NoteTrack.cpp:588
std::shared_ptr< WideChannelGroupInterval > DoGetInterval(size_t iInterval) override
Retrieve an interval.
Definition: NoteTrack.cpp:594
std::unique_ptr< char[]> mSerializationBuffer
Definition: NoteTrack.h:208
Track::Holder PasteInto(AudacityProject &project, TrackList &list) const override
Definition: NoteTrack.cpp:579
bool ExportMIDI(const wxString &f) const
Definition: NoteTrack.cpp:759
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:98
std::atomic< float > mVelocity
Atomic because it may be read by worker threads in playback.
Definition: NoteTrack.h:212
void SetVisibleChannels(unsigned value)
Definition: NoteTrack.h:152
void WriteXML(XMLWriter &xmlFile) const override
Definition: NoteTrack.cpp:848
void Paste(double t, const Track &src) override
Weak precondition allows overrides to replicate one channel into many.
Definition: NoteTrack.cpp:430
void PrintSequence()
Definition: NoteTrack.cpp:261
float GetVelocity() const
Definition: NoteTrack.h:124
void Silence(double t0, double t1, ProgressReporter reportProgress={}) override
Definition: NoteTrack.cpp:480
double mOrigin
Definition: NoteTrack.h:216
std::unique_ptr< Alg_seq > mSeq
Definition: NoteTrack.h:207
void InsertSilence(double t, double len) override
Definition: NoteTrack.cpp:495
XMLTagHandler * HandleXMLChild(const std::string_view &tag) override
Definition: NoteTrack.cpp:843
bool StretchRegion(QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur)
Definition: NoteTrack.cpp:615
Alg_seq * MakeExportableSeq(std::unique_ptr< Alg_seq > &cleanup) const
Definition: NoteTrack.cpp:636
static EnumSetting< bool > AllegroStyleSetting
Definition: NoteTrack.h:81
static NoteTrack * New(AudacityProject &project)
Definition: NoteTrack.cpp:137
bool Shift(double t)
Definition: NoteTrack.cpp:523
Alg_seq & GetSeq() const
Definition: NoteTrack.cpp:158
unsigned GetVisibleChannels() const
Definition: NoteTrack.h:149
QuantizedTimeAndBeat NearestBeatTime(double time) const
Definition: NoteTrack.cpp:550
void WarpAndTransposeNotes(double t0, double t1, const TimeWarper &warper, double semitones)
Definition: NoteTrack.cpp:220
void SetVelocity(float velocity)
Definition: NoteTrack.cpp:508
bool Trim(double t0, double t1)
Definition: NoteTrack.cpp:373
Track::Holder Clone(bool backup) const override
Definition: NoteTrack.cpp:180
bool HandleXMLTag(const std::string_view &tag, const AttributesList &attrs) override
Definition: NoteTrack.cpp:804
void Clear(double t0, double t1) override
Definition: NoteTrack.cpp:397
long mSerializationLength
Definition: NoteTrack.h:209
void DoSetVelocity(float velocity)
Definition: NoteTrack.cpp:516
Track::Holder Copy(double t0, double t1, bool forClipboard=true) const override
Create new tracks and don't modify this track.
Definition: NoteTrack.cpp:350
bool HandleXMLAttribute(const std::string_view &attr, const XMLAttributeValueView &value)
static const TypeInfo & ClassTypeInfo()
void WriteXMLAttributes(XMLWriter &xmlFile) const
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:207
Specialization of Setting for strings.
Definition: Prefs.h:370
Transforms one point in time to another point. For example, a time stretching effect might use one to...
Definition: TimeWarper.h:62
virtual double Warp(double originalTime) const =0
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
void Notify(bool allChannels, int code=-1)
Definition: Track.cpp:234
R TypeSwitch(const Functions &...functions)
Definition: Track.h:381
std::shared_ptr< Track > Holder
Definition: Track.h:202
bool HandleCommonXMLAttribute(const std::string_view &attr, const XMLAttributeValueView &valueView)
Definition: Track.cpp:819
void SetName(const wxString &n)
Definition: Track.cpp:69
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:850
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
TrackKind * Add(const std::shared_ptr< TrackKind > &t, bool assignIds=true)
Definition: Track.h:1048
Generates overrides of channel-related functions.
Definition: Track.h:450
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:25
bool IsValidVisibleChannels(const int nValue)
Definition: NoteTrack.cpp:798
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:628
std::string Serialize(const ProjectForm &form)
void copy(const T *src, T *dst, int32_t n)
Definition: VectorOps.h:40
Interval(const NoteTrack &track)
Definition: NoteTrack.cpp:93
std::shared_ptr< ChannelInterval > DoGetChannel(size_t iChannel) override
Retrieve a channel.
Definition: NoteTrack.cpp:115
~Interval() override
double End() const override
Definition: NoteTrack.cpp:104
double Start() const override
Definition: NoteTrack.cpp:99
size_t NChannels() const override
Report the number of channels.
Definition: NoteTrack.cpp:109
virtual void WriteXML(XMLWriter &xmlFile) const
Default implementation does nothing.
Definition: NoteTrack.cpp:124
virtual bool HandleAttribute(const Attribute &attribute)
Return whether the attribute was used; default returns false.
Definition: NoteTrack.cpp:127
~NoteTrackAttachment() override