Audacity 3.2.0
Classes | Functions | Variables
anonymous_namespace{ImportMIDI.cpp} Namespace Reference

Classes

struct  MIDIImportFileHandle
 
class  MIDIImportPlugin
 

Functions

bool ImportMIDI (const FilePath &fName, NoteTrack *dest)
 
bool DoImportMIDI (AudacityProject &project, const FilePath &fileName)
 
void OnImportMIDI (const CommandContext &context)
 

Variables

AttachedItem sAttachment
 
constexpr auto exts
 
const auto DESC = XO("MIDI files")
 
Importer::RegisteredImportPlugin registered
 

Function Documentation

◆ DoImportMIDI()

bool anonymous_namespace{ImportMIDI.cpp}::DoImportMIDI ( AudacityProject project,
const FilePath fileName 
)

Definition at line 41 of file ImportMIDI.cpp.

42{
43 auto &projectFileIO = ProjectFileIO::Get( project );
44 auto &tracks = TrackList::Get( project );
45 auto newTrack = std::make_shared<NoteTrack>();
46 bool initiallyEmpty = tracks.empty();
47
48 if (::ImportMIDI(fileName, newTrack.get())) {
49
51 auto pTrack = tracks.Add( newTrack );
52 pTrack->SetSelected(true);
53
54 // Fix the bug 2109.
55 // In case the project had soloed tracks before importing,
56 // the newly imported track is muted.
57 const bool projectHasSolo =
58 !(tracks.Any<PlayableTrack>() + &PlayableTrack::GetSolo).empty();
59 if (projectHasSolo)
60 pTrack->SetMute(true);
61
63 .PushState(
64 XO("Imported MIDI from '%s'").Format( fileName ),
65 XO("Import MIDI")
66 );
67
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")
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:314
void ZoomFitHorizontallyAndShowTrack(Track *pTrack)
Definition: Viewport.cpp:443
static Viewport & Get(AudacityProject &project)
Definition: Viewport.cpp:33
void SelectNone(AudacityProject &project)
bool ImportMIDI(const FilePath &fName, NoteTrack *dest)
Definition: ImportMIDI.cpp:85

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 OnImportMIDI().

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

◆ ImportMIDI()

bool anonymous_namespace{ImportMIDI.cpp}::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->MoveTo(offset);
122 wxString trackNameBase = fName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
123 dest->SetName(trackNameBase);
124 mf.Close();
125
126 NoteTrackRange::Get(*dest).ZoomAllNotes(&dest->GetSeq());
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:262
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:98
Alg_seq & GetSeq() const
Definition: NoteTrack.cpp:158
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:69

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

Referenced by DoImportMIDI(), and anonymous_namespace{ImportMIDI.cpp}::MIDIImportFileHandle::Import().

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

◆ OnImportMIDI()

void anonymous_namespace{ImportMIDI.cpp}::OnImportMIDI ( const CommandContext context)

Definition at line 140 of file ImportMIDI.cpp.

141{
142 auto &project = context.project;
143 auto &window = GetProjectFrame( project );
144
145 wxString fileName = SelectFile(FileNames::Operation::Open,
146 XO("Select a MIDI file"),
147 wxEmptyString, // Path
148 wxT(""), // Name
149 wxT(""), // Extension
150 {
151 { XO("MIDI and Allegro files"),
152 { wxT("mid"), wxT("midi"), wxT("gro"), }, true },
153 { XO("MIDI files"),
154 { wxT("mid"), wxT("midi"), }, true },
155 { XO("Allegro files"),
156 { wxT("gro"), }, true },
158 },
159 wxRESIZE_BORDER, // Flags
160 &window); // Parent
161
162 if (!fileName.empty())
163 DoImportMIDI(project, fileName);
164}
AUDACITY_DLL_API wxFrame & GetProjectFrame(AudacityProject &project)
Get the top-level window associated with the project (as a wxFrame only, when you do not need to use ...
FilePath SelectFile(FileNames::Operation op, const TranslatableString &message, const FilePath &default_path, const FilePath &default_filename, const FileExtension &default_extension, const FileTypes &fileTypes, int flags, wxWindow *parent)
Definition: SelectFile.cpp:17
AudacityProject & project
FILES_API const FileType AllFiles
Definition: FileNames.h:70
bool DoImportMIDI(AudacityProject &project, const FilePath &fileName)
Definition: ImportMIDI.cpp:41

References FileNames::AllFiles, DoImportMIDI(), GetProjectFrame(), CommandContext::project, project, SelectFile(), wxT(), and XO().

Here is the call graph for this function:

Variable Documentation

◆ DESC

const auto anonymous_namespace{ImportMIDI.cpp}::DESC = XO("MIDI files")

◆ exts

constexpr auto anonymous_namespace{ImportMIDI.cpp}::exts
constexpr
Initial value:
= {
wxT("gro"),
wxT("midi"),
wxT("mid"),
}

Definition at line 173 of file ImportMIDI.cpp.

◆ registered

Importer::RegisteredImportPlugin anonymous_namespace{ImportMIDI.cpp}::registered
Initial value:
{ "portsmf",
std::make_unique<MIDIImportPlugin>()
}

Definition at line 255 of file ImportMIDI.cpp.

◆ sAttachment

AttachedItem anonymous_namespace{ImportMIDI.cpp}::sAttachment
Initial value:
{
Command( wxT("ImportMIDI"), XXO("&MIDI..."), OnImportMIDI,
{ wxT("File/Import-Export/Import"),
{ OrderingHint::After, {"ImportAudio"} } }
}
const ReservedCommandFlag & AudioIONotBusyFlag()
XXO("&Cut/Copy/Paste Toolbar")
constexpr auto Command
Definition: MenuRegistry.h:456
void OnImportMIDI(const CommandContext &context)
Definition: ImportMIDI.cpp:140

Definition at line 166 of file ImportMIDI.cpp.