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 DoImportMIDI (AudacityProject &project, const FilePath &fileName)
 
bool ImportMIDI (const FilePath &fName, NoteTrack *dest)
 

Function Documentation

◆ DoImportMIDI()

bool DoImportMIDI ( AudacityProject project,
const FilePath fileName 
)

Definition at line 39 of file ImportMIDI.cpp.

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

References FileHistory::Append(), fn, ProjectFileIO::Get(), ProjectHistory::Get(), TrackList::Get(), ProjectWindow::Get(), PlayableTrack::GetSolo(), FileHistory::Global(), ImportMIDI(), ProjectHistory::PushState(), SelectUtilities::SelectNone(), AudacityProject::SetInitialImportPath(), AudacityProject::SetProjectName(), XO(), and ProjectWindow::ZoomAfterImport().

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 85 of file ImportMIDI.cpp.

86{
87 if (fName.length() <= 4){
89 XO("Could not open file %s: Filename too short.").Format( fName ) );
90 return false;
91 }
92
93 bool is_midi = false;
94 if (fName.Right(4).CmpNoCase(wxT(".mid")) == 0 || fName.Right(5).CmpNoCase(wxT(".midi")) == 0)
95 is_midi = true;
96 else if(fName.Right(4).CmpNoCase(wxT(".gro")) != 0) {
98 XO("Could not open file %s: Incorrect filetype.").Format( fName ) );
99 return false;
100 }
101
102 wxFFile mf(fName, wxT("rb"));
103 if (!mf.IsOpened()) {
105 XO("Could not open file %s.").Format( fName ) );
106 return false;
107 }
108
109 double offset = 0.0;
110 auto new_seq = std::make_unique<Alg_seq>(fName.mb_str(), is_midi, &offset);
111
112 //Should we also check if(seq->tracks() == 0) ?
113 if(new_seq->get_read_error() == alg_error_open){
115 XO("Could not open file %s.").Format( fName ) );
116 mf.Close();
117 return false;
118 }
119
120 dest->SetSequence(std::move(new_seq));
121 dest->SetOffset(offset);
122 wxString trackNameBase = fName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
123 dest->SetName(trackNameBase);
124 mf.Close();
125
126 dest->ZoomAllNotes();
127 return true;
128}
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:379
void ZoomAllNotes()
Zooms so that all notes are visible.
Definition: NoteTrack.cpp:1087
virtual void SetOffset(double o)
Definition: Track.h:484
void SetName(const wxString &n)
Definition: Track.cpp:80

References AudacityMessageBox(), Track::SetName(), Track::SetOffset(), NoteTrack::SetSequence(), wxT(), XO(), and NoteTrack::ZoomAllNotes().

Referenced by DoImportMIDI().

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