File: NoteTrack.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (993 lines) | stat: -rw-r--r-- 30,242 bytes parent folder | download | duplicates (2)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/**********************************************************************

  Audacity: A Digital Audio Editor

  NoteTrack.cpp

  Dominic Mazzoni

*******************************************************************//*!

\class NoteTrack
\brief A Track that is used for Midi notes.  (Somewhat old code).

*//*******************************************************************/
#include "NoteTrack.h"

#include <wx/wxcrtvararg.h>

#if defined(USE_MIDI)
#include "WrapAllegro.h"

#include <sstream>

#define ROUND(x) ((int) ((x) + 0.5))

#include "Prefs.h"
#include "Project.h"
#include "InconsistencyException.h"
#include "TimeWarper.h"

#ifdef SONIFY
#include <portmidi.h>

#define SON_PROGRAM 0
#define SON_AutoSave 67
#define SON_ModifyState 60
#define SON_NoteBackground 72
#define SON_NoteForeground 74
#define SON_Measures 76 /* "bar line" */
#define SON_Serialize 77
#define SON_Unserialize 79
#define SON_VEL 100


PmStream *sonMidiStream;
bool sonificationStarted = false;

void SonifyBeginSonification()
{
   PmError err = Pm_OpenOutput(&sonMidiStream, Pm_GetDefaultOutputDeviceID(),
                               NULL, 0, NULL, NULL, 0);
   if (err) sonMidiStream = NULL;
   if (sonMidiStream)
      Pm_WriteShort(sonMidiStream, 0, Pm_Message(0xC0, SON_PROGRAM, 0));
   sonificationStarted = true;
}


void SonifyEndSonification()
{
   if (sonMidiStream) Pm_Close(sonMidiStream);
   sonificationStarted = false;
}




void SonifyNoteOnOff(int p, int v)
{
   if (!sonificationStarted)
      SonifyBeginSonification();
   if (sonMidiStream)
      Pm_WriteShort(sonMidiStream, 0, Pm_Message(0x90, p, v));
}

#define SONFNS(name) \
   void SonifyBegin ## name() { SonifyNoteOnOff(SON_ ## name, SON_VEL); } \
   void SonifyEnd ## name() { SonifyNoteOnOff(SON_ ## name, 0); }

SONFNS(NoteBackground)
SONFNS(NoteForeground)
SONFNS(Measures)
SONFNS(Serialize)
SONFNS(Unserialize)
SONFNS(ModifyState)
SONFNS(AutoSave)

#undef SONFNS

#endif


NoteTrack::Interval::Interval(const NoteTrack &track)
   : mpTrack{ track.SharedPointer<const NoteTrack>() }
{}

NoteTrack::Interval::~Interval() = default;

double NoteTrack::Interval::Start() const
{
   return mpTrack->mOrigin;
}

double NoteTrack::Interval::End() const
{
   return Start() + mpTrack->GetSeq().get_real_dur();
}

size_t NoteTrack::Interval::NChannels() const
{
   return 1;
}

std::shared_ptr<ChannelInterval>
NoteTrack::Interval::DoGetChannel(size_t iChannel)
{
   if (iChannel == 0)
      return std::make_shared<ChannelInterval>();
   return {};
}

NoteTrackAttachment::~NoteTrackAttachment() = default;

void NoteTrackAttachment::WriteXML(XMLWriter &xmlFile) const
{}

bool NoteTrackAttachment::HandleAttribute(const Attribute &attribute)
{
   return false;
}

static ProjectFileIORegistry::ObjectReaderEntry readerEntry{
   "notetrack",
   NoteTrack::New
};

NoteTrack *NoteTrack::New( AudacityProject &project )
{
   auto &tracks = TrackList::Get( project );
   auto result = tracks.Add( std::make_shared<NoteTrack>());
   result->AttachedTrackObjects::BuildAll();
   return result;
}

NoteTrack::NoteTrack()
   : UniqueChannelTrack{}
{
   SetName(_("Note Track"));

   mSeq = NULL;
   mSerializationLength = 0;
}

NoteTrack::~NoteTrack()
{
}

Alg_seq &NoteTrack::GetSeq() const
{
   if (!mSeq) {
      if (!mSerializationBuffer)
         mSeq = std::make_unique<Alg_seq>();
      else {
         std::unique_ptr<Alg_track> alg_track
         { Alg_seq::unserialize
            ( mSerializationBuffer.get(), mSerializationLength ) };
         wxASSERT(alg_track->get_type() == 's');
         mSeq.reset( static_cast<Alg_seq*>(alg_track.release()) );

         // Preserve the invariant that at most one of the representations is
         // valid
         mSerializationBuffer.reset();
         mSerializationLength = 0;
      }
   }
   wxASSERT(mSeq);
   return *mSeq;
}

Track::Holder NoteTrack::Clone(bool) const
{
   auto duplicate = std::make_shared<NoteTrack>();
   duplicate->Init(*this);
   // The duplicate begins life in serialized state.  Often the duplicate is
   // pushed on the Undo stack.  Then we want to un-serialize it (or a further
   // copy) only on demand after an Undo.
   if (mSeq) {
      SonifyBeginSerialize();
      wxASSERT(!mSerializationBuffer);
      // serialize from this to duplicate's mSerializationBuffer
      void *buffer;
      mSeq->serialize(&buffer,
                      &duplicate->mSerializationLength);
      duplicate->mSerializationBuffer.reset( (char*)buffer );
      SonifyEndSerialize();
   }
   else if (mSerializationBuffer) {
      // Copy already serialized data.
      wxASSERT(!mSeq);
      duplicate->mSerializationLength = this->mSerializationLength;
      duplicate->mSerializationBuffer.reset
         ( safenew char[ this->mSerializationLength ] );
      memcpy( duplicate->mSerializationBuffer.get(),
              this->mSerializationBuffer.get(), this->mSerializationLength );
   }
   else {
      // We are duplicating a default-constructed NoteTrack, and that's okay
   }

   // copy some other fields here
   Attachments &attachments = *duplicate;
   attachments = *this;

   duplicate->SetVisibleChannels(GetVisibleChannels());
   duplicate->MoveTo(mOrigin);
   duplicate->SetVelocity(GetVelocity());
   return duplicate;
}

void NoteTrack::ShiftBy(double t0, double delta)
{
   if(t0 <= mOrigin)
      mOrigin += delta;
}

void NoteTrack::WarpAndTransposeNotes(double t0, double t1,
                                      const TimeWarper &warper,
                                      double semitones)
{
   double offset = this->mOrigin; // track is shifted this amount
   auto &seq = GetSeq();
   seq.convert_to_seconds(); // make sure time units are right
   t1 -= offset; // adjust time range to compensate for track offset
   t0 -= offset;
   if (t1 > seq.get_dur()) { // make sure t0, t1 are within sequence
      t1 = seq.get_dur();
      if (t0 >= t1) return;
   }
   Alg_iterator iter(mSeq.get(), false);
   iter.begin();
   Alg_event_ptr event;
   while (0 != (event = iter.next()) && event->time < t1) {
      if (event->is_note() && event->time >= t0) {
         event->set_pitch(event->get_pitch() + semitones);
      }
   }
   iter.end();
   // now, use warper to warp the tempo map
   seq.convert_to_beats(); // beats remain the same
   Alg_time_map_ptr map = seq.get_time_map();
   map->insert_beat(t0, map->time_to_beat(t0));
   map->insert_beat(t1, map->time_to_beat(t1));
   int i, len = map->length();
   for (i = 0; i < len; i++) {
      Alg_beat &beat = map->beats[i];
      beat.time = warper.Warp(beat.time + offset) - offset;
   }
   // about to redisplay, so might as well convert back to time now
   seq.convert_to_seconds();
}

void NoteTrack::SetSequence(std::unique_ptr<Alg_seq> &&seq)
{
   mSeq = std::move(seq);
}

void NoteTrack::PrintSequence()
{
   FILE *debugOutput;

   debugOutput = fopen("debugOutput.txt", "wt");
   wxFprintf(debugOutput, "Importing MIDI...\n");

   // This is called for debugging purposes.  Do not compute mSeq on demand
   // with GetSeq()
   if (mSeq) {
      int i = 0;

      while(i < mSeq->length()) {
         wxFprintf(debugOutput, "--\n");
         wxFprintf(debugOutput, "type: %c\n",
            ((Alg_event_ptr)mSeq->track_list.tracks[i])->get_type());
         wxFprintf(debugOutput, "time: %f\n",
            ((Alg_event_ptr)mSeq->track_list.tracks[i])->time);
         wxFprintf(debugOutput, "channel: %li\n",
            ((Alg_event_ptr)mSeq->track_list.tracks[i])->chan);

         if(((Alg_event_ptr)mSeq->track_list.tracks[i])->get_type() == wxT('n'))
         {
            wxFprintf(debugOutput, "pitch: %f\n",
               ((Alg_note_ptr)mSeq->track_list.tracks[i])->pitch);
            wxFprintf(debugOutput, "duration: %f\n",
               ((Alg_note_ptr)mSeq->track_list.tracks[i])->dur);
            wxFprintf(debugOutput, "velocity: %f\n",
               ((Alg_note_ptr)mSeq->track_list.tracks[i])->loud);
         }
         else if(((Alg_event_ptr)mSeq->track_list.tracks[i])->get_type() == wxT('n'))
         {
            wxFprintf(debugOutput, "key: %li\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->get_identifier());
            wxFprintf(debugOutput, "attribute type: %c\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type());
            wxFprintf(debugOutput, "attribute: %s\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_name());

            if(((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type() == wxT('r'))
            {
               wxFprintf(debugOutput, "value: %f\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.r);
            }
            else if(((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type() == wxT('i')) {
               wxFprintf(debugOutput, "value: %li\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.i);
            }
            else if(((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.attr_type() == wxT('s')) {
               wxFprintf(debugOutput, "value: %s\n", ((Alg_update_ptr)mSeq->track_list.tracks[i])->parameter.s);
            }
            else {}
         }

         i++;
      }
   }
   else {
      wxFprintf(debugOutput, "No sequence defined!\n");
   }

   fclose(debugOutput);
}

Track::Holder NoteTrack::Cut(double t0, double t1)
{
   if (t1 < t0)
      THROW_INCONSISTENCY_EXCEPTION;

   double len = t1-t0;
   //auto delta = -(
      //( std::min( t1, GetEndTime() ) ) - ( std::max( t0, GetStartTime() ) )
   //);

   auto newTrack = std::make_shared<NoteTrack>();

   newTrack->Init(*this);

   auto &seq = GetSeq();
   seq.convert_to_seconds();
   newTrack->mSeq.reset(seq.cut(t0 - mOrigin, len, false));
   newTrack->MoveTo(0);

   // Not needed
   // Alg_seq::cut seems to handle this
   //AddToDuration( delta );

   // What should be done with the rest of newTrack's members?
   //(mBottomNote,
   // mSerializationBuffer, mSerializationLength, mVisibleChannels)

   return newTrack;
}

Track::Holder NoteTrack::Copy(double t0, double t1, bool) const
{
   if (t1 < t0)
      THROW_INCONSISTENCY_EXCEPTION;

   double len = t1-t0;

   auto newTrack = std::make_shared<NoteTrack>();

   newTrack->Init(*this);

   auto &seq = GetSeq();
   seq.convert_to_seconds();
   newTrack->mSeq.reset(seq.copy(t0 - mOrigin, len, false));
   newTrack->MoveTo(0);

   // What should be done with the rest of newTrack's members?
   // (mBottomNote, mSerializationBuffer,
   // mSerializationLength, mVisibleChannels)

   return newTrack;
}

bool NoteTrack::Trim(double t0, double t1)
{
   if (t1 < t0)
      return false;
   auto &seq = GetSeq();
   //auto delta = -(
      //( GetEndTime() - std::min( GetEndTime(), t1 ) ) +
      //( std::max(t0, GetStartTime()) - GetStartTime() )
   //);
   seq.convert_to_seconds();
   // DELETE way beyond duration just in case something is out there:
   seq.clear(t1 - mOrigin, seq.get_dur() + 10000.0, false);
   // Now that stuff beyond selection is cleared, clear before selection:
   seq.clear(0.0, t0 - mOrigin, false);
   // want starting time to be t0
   MoveTo(t0);

   // Not needed
   // Alg_seq::clear seems to handle this
   //AddToDuration( delta );

   return true;
}

void NoteTrack::Clear(double t0, double t1)
{
   if (t1 < t0)
      THROW_INCONSISTENCY_EXCEPTION;

   double len = t1-t0;

   auto &seq = GetSeq();

   auto offset = mOrigin;
   auto start = t0 - offset;
   if (start < 0.0) {
      // AlgSeq::clear will shift the cleared interval, not changing len, if
      // start is negative.  That's not what we want to happen.
      if (len > -start) {
         seq.clear(0, len + start, false);
         MoveTo(t0);
      }
      else
         MoveTo(offset - len);
   }
   else {
      //auto delta = -(
      //( std::min( t1, GetEndTime() ) ) - ( std::max( t0, GetStartTime() ) )
      //);
      seq.clear(start, len, false);

      // Not needed
      // Alg_seq::clear seems to handle this
      // AddToDuration( delta );
   }
}

void NoteTrack::Paste(double t, const Track &src)
{
   // Paste inserts src at time t. If src has a positive offset,
   // the offset is treated as silence which is also inserted. If
   // the offset is negative, the offset is ignored and the ENTIRE
   // src is inserted (otherwise, we would either lose data from
   // src by not inserting things at negative times, or inserting
   // things at negative times could overlap things already in
   // the destination track).

   //Check that src is a non-NULL NoteTrack
   bool bOk = src.TypeSwitch<bool>( [&](const NoteTrack &other) {

      auto myOffset = this->mOrigin;
      if (t < myOffset) {
         // workaround strange behavior described at
         // http://bugzilla.audacityteam.org/show_bug.cgi?id=1735#c3
         MoveTo(t);
         InsertSilence(t, myOffset - t);
      }

      double delta = 0.0;
      auto &seq = GetSeq();
      auto offset = other.mOrigin;
      if (offset > 0) {
         seq.convert_to_seconds();
         seq.insert_silence(t - mOrigin, offset);
         t += offset;
         // Is this needed or does Alg_seq::insert_silence take care of it?
         //delta += offset;
      }

      // This seems to be needed:
      delta += std::max(0.0, t - GetEndTime());

      // This, not:
      //delta += other.GetSeq().get_real_dur();

      seq.paste(t - mOrigin, &other.GetSeq());

      AddToDuration(delta);

      return true;
   });

   if (!bOk)
      // THROW_INCONSISTENCY_EXCEPTION; // ?
      (void)0;// intentionally do nothing
}

void NoteTrack::Silence(double t0, double t1, ProgressReporter)
{
   if (t1 < t0)
      THROW_INCONSISTENCY_EXCEPTION;

   auto len = t1 - t0;

   auto &seq = GetSeq();
   seq.convert_to_seconds();
   // XXX: do we want to set the all param?
   // If it's set, then it seems like notes are silenced if they start or end in the range,
   // otherwise only if they start in the range. --Poke
   seq.silence(t0 - mOrigin, len, false);
}

void NoteTrack::InsertSilence(double t, double len)
{
   if (len < 0)
      THROW_INCONSISTENCY_EXCEPTION;

   auto &seq = GetSeq();
   seq.convert_to_seconds();
   seq.insert_silence(t - mOrigin, len);

   // is this needed?
   // AddToDuration( len );
}

void NoteTrack::SetVelocity(float velocity)
{
   if (GetVelocity() != velocity) {
      DoSetVelocity(velocity);
      Notify(false);
   }
}

void NoteTrack::DoSetVelocity(float velocity)
{
   mVelocity.store(velocity, std::memory_order_relaxed);
}

// Call this function to manipulate the underlying sequence data. This is
// NOT the function that handles horizontal dragging.
bool NoteTrack::Shift(double t) // t is always seconds
{
   if (t > 0) {
      auto &seq = GetSeq();
      // insert an even number of measures
      seq.convert_to_beats();
      // get initial tempo
      double tempo = seq.get_tempo(0.0);
      double beats_per_measure = seq.get_bar_len(0.0);
      int m = ROUND(t * tempo / beats_per_measure);
      // need at least 1 measure, so if we rounded down to zero, fix it
      if (m == 0) m = 1;
      // compute NEW tempo so that m measures at NEW tempo take t seconds
      tempo = beats_per_measure * m / t; // in beats per second
      seq.insert_silence(0.0, beats_per_measure * m);
      seq.set_tempo(tempo * 60.0 /* bpm */, 0.0, beats_per_measure * m);
      seq.write("afterShift.gro");
   } else if (t < 0) {
      auto &seq = GetSeq();
      seq.convert_to_seconds();
      seq.clear(0, t, true);
   } else { // offset is zero, no modifications
      return false;
   }
   return true;
}

QuantizedTimeAndBeat NoteTrack::NearestBeatTime( double time ) const
{
   // Alg_seq knows nothing about offset, so remove offset time
   double seq_time = time - mOrigin;
   double beat;
   auto &seq = GetSeq();
   seq_time = seq.nearest_beat_time(seq_time, &beat);
   // add the offset back in to get "actual" audacity track time
   return { seq_time + mOrigin, beat };
}

static const Track::TypeInfo &typeInfo()
{
   static const Track::TypeInfo info{
      { "note", "midi", XO("Note Track") }, true,
      &PlayableTrack::ClassTypeInfo() };
   return info;
}

auto NoteTrack::GetTypeInfo() const -> const TypeInfo &
{
   return typeInfo();
}

auto NoteTrack::ClassTypeInfo() -> const TypeInfo &
{
   return typeInfo();
}

Track::Holder NoteTrack::PasteInto(AudacityProject &, TrackList &list) const
{
   auto pNewTrack = std::make_shared<NoteTrack>();
   pNewTrack->Init(*this);
   pNewTrack->Paste(0.0, *this);
   list.Add(pNewTrack);
   return pNewTrack;
}

size_t NoteTrack::NIntervals() const
{
   return 1;
}

std::shared_ptr<WideChannelGroupInterval>
NoteTrack::DoGetInterval(size_t iInterval)
{
   if (iInterval == 0)
      // Just one, and no extra info in it!
      return std::make_shared<Interval>(*this);
   return {};
}

void NoteTrack::AddToDuration( double delta )
{
   auto &seq = GetSeq();
#if 0
   // PRL:  Would this be better ?
   seq.set_real_dur( seq.get_real_dur() + delta );
#else
   seq.convert_to_seconds();
   seq.set_dur( seq.get_dur() + delta );
#endif
}

bool NoteTrack::StretchRegion
   ( QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur )
{
   auto &seq = GetSeq();
   bool result = seq.stretch_region( t0.second, t1.second, newDur );
   if (result) {
      const auto oldDur = t1.first - t0.first;
      AddToDuration( newDur - oldDur );
   }
   return result;
}

namespace
{
   void swap(std::unique_ptr<Alg_seq> &a, std::unique_ptr<Alg_seq> &b)
   {
      std::unique_ptr<Alg_seq> tmp = std::move(a);
      a = std::move(b);
      b = std::move(tmp);
   }
}

Alg_seq *NoteTrack::MakeExportableSeq(std::unique_ptr<Alg_seq> &cleanup) const
{
   cleanup.reset();
   double offset = mOrigin;
   if (offset == 0)
      return &GetSeq();
   // make a copy, deleting events that are shifted before time 0
   double start = -offset;
   if (start < 0) start = 0;
   // notes that begin before "start" are not included even if they
   // extend past "start" (because "all" parameter is set to false)
   cleanup.reset( GetSeq().copy(start, GetSeq().get_dur() - start, false) );
   auto seq = cleanup.get();
   if (offset > 0) {
      {
         // swap cleanup and mSeq so that Shift operates on the NEW copy
         swap( this->mSeq, cleanup );
         auto cleanup2 = finally( [&] { swap( this->mSeq, cleanup ); } );

         const_cast< NoteTrack *>( this )->Shift(offset);
      }
#ifdef OLD_CODE
      // now shift events by offset. This must be done with an integer
      // number of measures, so first, find the beats-per-measure
      double beats_per_measure = 4.0;
      Alg_time_sig_ptr tsp = NULL;
      if (seq->time_sig.length() > 0 && seq->time_sig[0].beat < ALG_EPS) {
         // there is an initial time signature
         tsp = &(seq->time_sig[0]);
         beats_per_measure = (tsp->num * 4) / tsp->den;
      }
      // also need the initial tempo
      double bps = ALG_DEFAULT_BPM / 60;
      Alg_time_map_ptr map = seq->get_time_map();
      Alg_beat_ptr bp = &(map->beats[0]);
      if (bp->time < ALG_EPS) { // tempo change at time 0
         if (map->beats.len > 1) { // compute slope to get tempo
            bps = (map->beats[1].beat - map->beats[0].beat) /
                  (map->beats[1].time - map->beats[0].time);
         } else if (seq->get_time_map()->last_tempo_flag) {
            bps = seq->get_time_map()->last_tempo;
         }
      }
      // find closest number of measures to fit in the gap
      // number of measures is offset / measure_time
      double measure_time = beats_per_measure / bps; // seconds per measure
      int n = ROUND(offset / measure_time);
      if (n == 0) n = 1;
      // we will insert n measures. Compute the desired duration of each.
      measure_time = offset / n;
      bps = beats_per_measure / measure_time;
      // insert integer multiple of measures at beginning
      seq->convert_to_beats();
      seq->insert_silence(0, beats_per_measure * n);
      // make sure time signature at 0 is correct
      if (tsp) {
         seq->set_time_sig(0, tsp->num, tsp->den);
      }
      // adjust tempo to match offset
      seq->set_tempo(bps * 60.0, 0, beats_per_measure * n);
#endif
   } else {
      auto &mySeq = GetSeq();
      // if offset is negative, it might not be a multiple of beats, but
      // we want to preserve the relative positions of measures. I.e. we
      // should shift barlines and time signatures as well as notes.
      // Insert a time signature at the first bar-line if necessary.

      // Translate start from seconds to beats and call it beat:
      double beat = mySeq.get_time_map()->time_to_beat(start);
      // Find the time signature in mySeq in effect at start (beat):
      int i = mySeq.time_sig.find_beat(beat);
      // i is where you would insert a NEW time sig at beat,
      // Case 1: beat coincides with a time sig at i. Time signature
      // at beat means that there is a barline at beat, so when beat
      // is shifted to 0, the relative barline positions are preserved
      if (mySeq.time_sig.length() > 0 &&
          within(beat, mySeq.time_sig[i].beat, ALG_EPS)) {
         // beat coincides with time signature change, so offset must
         // be a multiple of beats
         /* do nothing */ ;
      // Case 2: there is no time signature before beat.
      } else if (i == 0 && (mySeq.time_sig.length() == 0 ||
                            mySeq.time_sig[i].beat > beat)) {
         // If beat does not fall on an implied barline, we need to
         // insert a time signature.
         double measures = beat / 4.0;
         double imeasures = ROUND(measures);
         if (!within(measures, imeasures, ALG_EPS)) {
            double bar_offset = ((int)(measures) + 1) * 4.0 - beat;
            seq->set_time_sig(bar_offset, 4, 4);
         }
      // This case should never be true because if i == 0, either there
      // are no time signatures before beat (Case 2),
      // or there is one time signature at beat (Case 1)
      } else if (i == 0) {
         /* do nothing (might be good to assert(false)) */ ;
      // Case 3: i-1 must be the effective time sig position
      } else {
         i -= 1; // index the time signature in effect at beat
         Alg_time_sig_ptr tsp = &(mySeq.time_sig[i]);
         double beats_per_measure = (tsp->num * 4) / tsp->den;
         double measures = (beat - tsp->beat) / beats_per_measure;
         int imeasures = ROUND(measures);
         if (!within(measures, imeasures, ALG_EPS)) {
            // beat is not on a measure, so we need to insert a time sig
            // to force a bar line at the first measure location after
            // beat
            double bar = tsp->beat + beats_per_measure * ((int)(measures) + 1);
            double bar_offset = bar - beat;
            // insert NEW time signature at bar_offset in NEW sequence
            // It will have the same time signature, but the position will
            // force a barline to match the barlines in mSeq
            seq->set_time_sig(bar_offset, tsp->num, tsp->den);
         }
         // else beat coincides with a barline, so no need for an extra
         // time signature to force barline alignment
      }
   }
   return seq;
}


bool NoteTrack::ExportMIDI(const wxString &f) const
{
   std::unique_ptr<Alg_seq> cleanup;
   auto seq = MakeExportableSeq(cleanup);
   bool rslt = seq->smf_write(f.mb_str());
   return rslt;
}

EnumSetting<bool> NoteTrack::AllegroStyleSetting{
   wxT("/FileFormats/AllegroStyleChoice"),
   {
      EnumValueSymbol{ wxT("Seconds"), XXO("&Seconds") },
      /* i18n-hint: The music theory "beat" */
      EnumValueSymbol{ wxT("Beats"), XXO("&Beats") },
   },
   0, // true

   // for migrating old preferences:
   {
      true, false,
   },
   wxT("/FileFormats/AllegroStyle"),
};

bool NoteTrack::ExportAllegro(const wxString &f) const
{
   double offset = mOrigin;
   auto in_seconds = AllegroStyleSetting.ReadEnum();
   auto &seq = GetSeq();
   if (in_seconds) {
       seq.convert_to_seconds();
   } else {
       seq.convert_to_beats();
   }
   return seq.write(f.mb_str(), offset);
}


namespace {
bool IsValidVisibleChannels(const int nValue)
{
    return (nValue >= 0 && nValue < (1 << 16));
}
}

bool NoteTrack::HandleXMLTag(const std::string_view& tag, const AttributesList &attrs)
{
   if (tag == "notetrack") {
      for (auto pair : attrs)
      {
         auto attr = pair.first;
         auto value = pair.second;

         long nValue;
         double dblValue;
         if (this->Track::HandleCommonXMLAttribute(attr, value))
            ;
         else if (this->Attachments::FindIf([&](auto &attachment){
            return attachment.HandleAttribute(pair);
         }))
            ;
         else if (this->PlayableTrack::HandleXMLAttribute(attr, value))
         {}
         else if (attr == "offset" && value.TryGet(dblValue))
            MoveTo(dblValue);
         else if (attr == "visiblechannels") {
             if (!value.TryGet(nValue) ||
                 !IsValidVisibleChannels(nValue))
                 return false;
             SetVisibleChannels(nValue);
         }
         else if (attr == "velocity" && value.TryGet(dblValue))
            DoSetVelocity(static_cast<float>(dblValue));
         else if (attr == "data") {
             std::string s(value.ToWString());
             std::istringstream data(s);
             mSeq = std::make_unique<Alg_seq>(data, false);
         }
      } // while
      return true;
   }
   return false;
}

XMLTagHandler *NoteTrack::HandleXMLChild(const std::string_view&  WXUNUSED(tag))
{
   return NULL;
}

void NoteTrack::WriteXML(XMLWriter &xmlFile) const
// may throw
{
   std::ostringstream data;
   Track::Holder holder;
   const NoteTrack *saveme = this;
   if (!mSeq) {
      // replace saveme with an (unserialized) duplicate, which is
      // destroyed at end of function.
      holder = Clone(false);
      saveme = static_cast<NoteTrack*>(holder.get());
   }
   saveme->GetSeq().write(data, true);
   xmlFile.StartTag(wxT("notetrack"));
   saveme->Track::WriteCommonXMLAttributes( xmlFile );
   this->PlayableTrack::WriteXMLAttributes(xmlFile);
   xmlFile.WriteAttr(wxT("offset"), saveme->mOrigin);
   xmlFile.WriteAttr(wxT("visiblechannels"),
      static_cast<int>(saveme->GetVisibleChannels()));

   xmlFile.WriteAttr(wxT("velocity"),
      static_cast<double>(saveme->GetVelocity()));
   saveme->Attachments::ForEach([&](auto &attachment){
      attachment.WriteXML(xmlFile);
   });
   xmlFile.WriteAttr(wxT("data"), wxString(data.str().c_str(), wxConvUTF8));
   xmlFile.EndTag(wxT("notetrack"));
}

#include <wx/log.h>
#include <wx/sstream.h>
#include <wx/txtstrm.h>
#include "AudioIOBase.h"
#include "portmidi.h"

// FIXME: When EXPERIMENTAL_MIDI_IN is added (eventually) this should also be enabled -- Poke
wxString GetMIDIDeviceInfo()
{
   wxStringOutputStream o;
   wxTextOutputStream s(o, wxEOL_UNIX);

   if (AudioIOBase::Get()->IsStreamActive()) {
      return XO("Stream is active ... unable to gather information.\n")
         .Translation();
   }


   // XXX: May need to trap errors as with the normal device info
   int recDeviceNum = Pm_GetDefaultInputDeviceID();
   int playDeviceNum = Pm_GetDefaultOutputDeviceID();
   int cnt = Pm_CountDevices();

   // PRL:  why only into the log?
   wxLogDebug(wxT("PortMidi reports %d MIDI devices"), cnt);

   s << wxT("==============================\n");
   s << XO("Default recording device number: %d\n").Format( recDeviceNum );
   s << XO("Default playback device number: %d\n").Format( playDeviceNum );

   auto recDevice = MIDIRecordingDevice.Read();
   auto playDevice = MIDIPlaybackDevice.Read();

   // This gets info on all available audio devices (input and output)
   if (cnt <= 0) {
      s << XO("No devices found\n");
      return o.GetString();
   }

   for (int i = 0; i < cnt; i++) {
      s << wxT("==============================\n");

      const PmDeviceInfo* info = Pm_GetDeviceInfo(i);
      if (!info) {
         s << XO("Device info unavailable for: %d\n").Format( i );
         continue;
      }

      wxString name = wxSafeConvertMB2WX(info->name);
      wxString hostName = wxSafeConvertMB2WX(info->interf);

      s << XO("Device ID: %d\n").Format( i );
      s << XO("Device name: %s\n").Format( name );
      s << XO("Host name: %s\n").Format( hostName );
      /* i18n-hint: Supported, meaning made available by the system */
      s << XO("Supports output: %d\n").Format( info->output );
      /* i18n-hint: Supported, meaning made available by the system */
      s << XO("Supports input: %d\n").Format( info->input );
      s << XO("Opened: %d\n").Format( info->opened );

      if (name == playDevice && info->output)
         playDeviceNum = i;

      if (name == recDevice && info->input)
         recDeviceNum = i;

      // XXX: This is only done because the same was applied with PortAudio
      // If PortMidi returns -1 for the default device, use the first one
      if (recDeviceNum < 0 && info->input){
         recDeviceNum = i;
      }
      if (playDeviceNum < 0 && info->output){
         playDeviceNum = i;
      }
   }

   bool haveRecDevice = (recDeviceNum >= 0);
   bool havePlayDevice = (playDeviceNum >= 0);

   s << wxT("==============================\n");
   if (haveRecDevice)
      s << XO("Selected MIDI recording device: %d - %s\n").Format( recDeviceNum, recDevice );
   else
      s << XO("No MIDI recording device found for '%s'.\n").Format( recDevice );

   if (havePlayDevice)
      s << XO("Selected MIDI playback device: %d - %s\n").Format( playDeviceNum, playDevice );
   else
      s << XO("No MIDI playback device found for '%s'.\n").Format( playDevice );

   // Mention our conditional compilation flags for Alpha only
#ifdef IS_ALPHA

   // Not internationalizing these alpha-only messages
   s << wxT("==============================\n");
#ifdef EXPERIMENTAL_MIDI_IN
   s << wxT("EXPERIMENTAL_MIDI_IN is enabled\n");
#else
   s << wxT("EXPERIMENTAL_MIDI_IN is NOT enabled\n");
#endif

#endif

   return o.GetString();
}

StringSetting MIDIPlaybackDevice{ L"/MidiIO/PlaybackDevice", L"" };
StringSetting MIDIRecordingDevice{ L"/MidiIO/RecordingDevice", L"" };
IntSetting MIDISynthLatency_ms{ L"/MidiIO/SynthLatency", 5 };

#endif // USE_MIDI