Audacity 3.2.0
Classes | Functions
ImportMIDI.h File Reference
#include "Identifier.h"
Include dependency graph for ImportMIDI.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  MIDIParser
 Unused class that might someday be used again to read MIDI files into a NoteTrack. More...
 

Functions

bool AUDACITY_DLL_API DoImportMIDI (AudacityProject &project, const FilePath &fileName)
 
bool ImportMIDI (const FilePath &fName, NoteTrack *dest)
 

Function Documentation

◆ DoImportMIDI()

bool AUDACITY_DLL_API DoImportMIDI ( AudacityProject project,
const FilePath fileName 
)

Definition at line 40 of file ImportMIDI.cpp.

41{
42 auto &projectFileIO = ProjectFileIO::Get( project );
43 auto &tracks = TrackList::Get( project );
44 auto newTrack = std::make_shared<NoteTrack>();
45 bool initiallyEmpty = tracks.empty();
46
47 if (::ImportMIDI(fileName, newTrack.get())) {
48
50 auto pTrack = tracks.Add( newTrack );
51 pTrack->SetSelected(true);
52
53 // Fix the bug 2109.
54 // In case the project had soloed tracks before importing,
55 // the newly imported track is muted.
56 const bool projectHasSolo =
57 !(tracks.Any<PlayableTrack>() + &PlayableTrack::GetSolo).empty();
58#ifdef EXPERIMENTAL_MIDI_OUT
59 if (projectHasSolo)
60 pTrack->SetMute(true);
61#endif
62
64 .PushState(
65 XO("Imported MIDI from '%s'").Format( fileName ),
66 XO("Import MIDI")
67 );
68
70 FileHistory::Global().Append(fileName);
71
72 // If the project was clean and temporary (not permanently saved), then set
73 // the filename to the just imported path.
74 if (initiallyEmpty && projectFileIO.IsTemporary()) {
75 wxFileName fn(fileName);
76 project.SetProjectName(fn.GetName());
77 project.SetInitialImportPath(fn.GetPath());
78 projectFileIO.SetProjectTitle();
79 }
80 return true;
81 }
82 else
83 return false;
84}
XO("Cut/Copy/Paste")
bool ImportMIDI(const FilePath &fName, NoteTrack *dest)
Definition: ImportMIDI.cpp:86
const auto tracks
const auto project
static const auto fn
void Append(const FilePath &file)
Definition: FileHistory.h:46
static FileHistory & Global()
Definition: FileHistory.cpp:39
Abstract base class used in importing a file.
AudioTrack subclass that can also be audibly replayed by the program.
Definition: PlayableTrack.h:40
bool GetSolo() const
Definition: PlayableTrack.h:48
static ProjectFileIO & Get(AudacityProject &project)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
static ProjectHistory & Get(AudacityProject &project)
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:347
void ZoomFitHorizontallyAndShowTrack(Track *pTrack)
Definition: Viewport.cpp:459
static Viewport & Get(AudacityProject &project)
Definition: Viewport.cpp:32
void SelectNone(AudacityProject &project)

References FileHistory::Append(), fn, ProjectFileIO::Get(), ProjectHistory::Get(), TrackList::Get(), Viewport::Get(), PlayableTrack::GetSolo(), FileHistory::Global(), ImportMIDI(), project, ProjectHistory::PushState(), SelectUtilities::SelectNone(), tracks, XO(), and Viewport::ZoomFitHorizontallyAndShowTrack().

Referenced by DoImportMIDIProject(), anonymous_namespace{ImportMIDI.cpp}::OnImportMIDI(), and ProjectFileManager::OpenFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ImportMIDI()

bool ImportMIDI ( const FilePath fName,
NoteTrack dest 
)

Definition at line 86 of file ImportMIDI.cpp.

87{
88 if (fName.length() <= 4){
90 XO("Could not open file %s: Filename too short.").Format( fName ) );
91 return false;
92 }
93
94 bool is_midi = false;
95 if (fName.Right(4).CmpNoCase(wxT(".mid")) == 0 || fName.Right(5).CmpNoCase(wxT(".midi")) == 0)
96 is_midi = true;
97 else if(fName.Right(4).CmpNoCase(wxT(".gro")) != 0) {
99 XO("Could not open file %s: Incorrect filetype.").Format( fName ) );
100 return false;
101 }
102
103 wxFFile mf(fName, wxT("rb"));
104 if (!mf.IsOpened()) {
106 XO("Could not open file %s.").Format( fName ) );
107 return false;
108 }
109
110 double offset = 0.0;
111 auto new_seq = std::make_unique<Alg_seq>(fName.mb_str(), is_midi, &offset);
112
113 //Should we also check if(seq->tracks() == 0) ?
114 if(new_seq->get_read_error() == alg_error_open){
116 XO("Could not open file %s.").Format( fName ) );
117 mf.Close();
118 return false;
119 }
120
121 dest->SetSequence(std::move(new_seq));
122 dest->MoveTo(offset);
123 wxString trackNameBase = fName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
124 dest->SetName(trackNameBase);
125 mf.Close();
126
127 NoteTrackRange::Get(*dest).ZoomAllNotes(&dest->GetSeq());
128 return true;
129}
wxT("CloseDown"))
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
void SetSequence(std::unique_ptr< Alg_seq > &&seq)
Definition: NoteTrack.cpp:263
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:106
Alg_seq & GetSeq() const
Definition: NoteTrack.cpp:146
static NoteTrackRange & Get(const NoteTrack &track)
Allow mutative access to attached data of a const track.
void ZoomAllNotes(Alg_seq *pSeq)
Zooms so that all notes are visible.
void SetName(const wxString &n)
Definition: Track.cpp:61

References AudacityMessageBox(), NoteTrackRange::Get(), NoteTrack::GetSeq(), NoteTrack::MoveTo(), Track::SetName(), NoteTrack::SetSequence(), wxT(), XO(), and NoteTrackRange::ZoomAllNotes().

Referenced by DoImportMIDI().

Here is the call graph for this function:
Here is the caller graph for this function: