Audacity  3.0.3
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 37 of file ImportMIDI.cpp.

38 {
39  auto &projectFileIO = ProjectFileIO::Get( project );
40  auto &tracks = TrackList::Get( project );
41  auto newTrack = std::make_shared<NoteTrack>();
42  bool initiallyEmpty = tracks.empty();
43 
44  if (::ImportMIDI(fileName, newTrack.get())) {
45 
46  SelectUtilities::SelectNone( project );
47  auto pTrack = tracks.Add( newTrack );
48  pTrack->SetSelected(true);
49 
50  // Fix the bug 2109.
51  // In case the project had soloed tracks before importing,
52  // the newly imported track is muted.
53  const bool projectHasSolo =
54  !(tracks.Any<PlayableTrack>() + &PlayableTrack::GetSolo).empty();
55 #ifdef EXPERIMENTAL_MIDI_OUT
56  if (projectHasSolo)
57  pTrack->SetMute(true);
58 #endif
59 
60  ProjectHistory::Get( project )
61  .PushState(
62  XO("Imported MIDI from '%s'").Format( fileName ),
63  XO("Import MIDI")
64  );
65 
66  ProjectWindow::Get( project ).ZoomAfterImport(pTrack);
67  FileHistory::Global().Append(fileName);
68 
69  // If the project was clean and temporary (not permanently saved), then set
70  // the filename to the just imported path.
71  if (initiallyEmpty && projectFileIO.IsTemporary()) {
72  wxFileName fn(fileName);
73  project.SetProjectName(fn.GetName());
74  project.SetInitialImportPath(fn.GetPath());
75  projectFileIO.SetProjectTitle();
76  }
77  return true;
78  }
79  else
80  return false;
81 }

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

Referenced by DoImportMIDIProject(), FileActions::Handler::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 83 of file ImportMIDI.cpp.

84 {
85  if (fName.length() <= 4){
87  XO("Could not open file %s: Filename too short.").Format( fName ) );
88  return false;
89  }
90 
91  bool is_midi = false;
92  if (fName.Right(4).CmpNoCase(wxT(".mid")) == 0 || fName.Right(5).CmpNoCase(wxT(".midi")) == 0)
93  is_midi = true;
94  else if(fName.Right(4).CmpNoCase(wxT(".gro")) != 0) {
96  XO("Could not open file %s: Incorrect filetype.").Format( fName ) );
97  return false;
98  }
99 
100  wxFFile mf(fName, wxT("rb"));
101  if (!mf.IsOpened()) {
103  XO("Could not open file %s.").Format( fName ) );
104  return false;
105  }
106 
107  double offset = 0.0;
108  auto new_seq = std::make_unique<Alg_seq>(fName.mb_str(), is_midi, &offset);
109 
110  //Should we also check if(seq->tracks() == 0) ?
111  if(new_seq->get_read_error() == alg_error_open){
113  XO("Could not open file %s.").Format( fName ) );
114  mf.Close();
115  return false;
116  }
117 
118  dest->SetSequence(std::move(new_seq));
119  dest->SetOffset(offset);
120  wxString trackNameBase = fName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
121  dest->SetName(trackNameBase);
122  mf.Close();
123 
124  dest->ZoomAllNotes();
125  return true;
126 }

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

Referenced by DoImportMIDI().

Here is the call graph for this function:
Here is the caller graph for this function:
PlayableTrack::GetSolo
bool GetSolo() const
Definition: Track.h:861
AudacityMessageBox
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
Definition: AudacityMessageBox.cpp:17
fn
static const auto fn
Definition: WaveformView.cpp:1108
Format
Abstract base class used in importing a file.
XO
#define XO(s)
Definition: Internat.h:31
ProjectFileIO::Get
static ProjectFileIO & Get(AudacityProject &project)
Definition: ProjectFileIO.cpp:266
AudacityProject::SetInitialImportPath
void SetInitialImportPath(const FilePath &path)
Definition: Project.cpp:107
NoteTrack::SetSequence
void SetSequence(std::unique_ptr< Alg_seq > &&seq)
Definition: NoteTrack.cpp:386
ProjectWindow::Get
static ProjectWindow & Get(AudacityProject &project)
Definition: ProjectWindow.cpp:535
Track::SetOffset
virtual void SetOffset(double o)
Definition: Track.h:445
FileHistory::Global
static FileHistory & Global()
Definition: FileHistory.cpp:37
NoteTrack::ZoomAllNotes
void ZoomAllNotes()
Zooms so that all notes are visible.
Definition: NoteTrack.cpp:1076
FileHistory::Append
void Append(const FilePath &file)
Definition: FileHistory.h:42
PlayableTrack
AudioTrack subclass that can also be audibly replayed by the program.
Definition: Track.h:854
ProjectHistory::PushState
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
Definition: ProjectHistory.cpp:90
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:506
Track::SetName
void SetName(const wxString &n)
Definition: Track.cpp:82
ProjectWindow::ZoomAfterImport
void ZoomAfterImport(Track *pTrack)
Definition: ProjectWindow.cpp:1551
SelectUtilities::SelectNone
void SelectNone(AudacityProject &project)
Definition: SelectUtilities.cpp:78
AudacityProject::SetProjectName
void SetProjectName(const wxString &name)
Definition: Project.cpp:97
ProjectHistory::Get
static ProjectHistory & Get(AudacityProject &project)
Definition: ProjectHistory.cpp:26
ImportMIDI
bool ImportMIDI(const FilePath &fName, NoteTrack *dest)
Definition: ImportMIDI.cpp:83