Audacity 3.2.0
ImportMIDI.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 ImportMIDI.cpp
6
7 Dominic Mazzoni
8
9**********************************************************************/
10
11#include <wx/defs.h>
12#include <wx/ffile.h>
13#include <wx/frame.h>
14
15#if defined(USE_MIDI)
16
17
18#include "WrapAllegro.h"
19
20#include "Import.h"
21#include "ImportPlugin.h"
23#include "NoteTrack.h"
25#include "Project.h"
26#include "ProjectFileIO.h"
27#include "ProjectHistory.h"
28#include "Viewport.h"
29#include "ProjectWindows.h"
30#include "SelectFile.h"
31#include "SelectUtilities.h"
32#include "AudacityMessageBox.h"
33#include "widgets/FileHistory.h"
34#include "ProgressDialog.h"
35
36namespace {
37
38bool ImportMIDI(const FilePath &fName, NoteTrack * dest);
39
40// Given an existing project, try to import into it, return true on success
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}
84
85bool ImportMIDI(const FilePath &fName, NoteTrack * dest)
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}
129
130}
131
132// Insert a menu item
133#include "CommandContext.h"
134#include "MenuRegistry.h"
135#include "CommonCommandFlags.h"
136
137namespace {
138using namespace MenuRegistry;
139
140void OnImportMIDI(const CommandContext &context)
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}
165
167 Command( wxT("ImportMIDI"), XXO("&MIDI..."), OnImportMIDI,
169 { wxT("File/Import-Export/Import"),
170 { OrderingHint::After, {"ImportAudio"} } }
171};
172
173constexpr auto exts = {
174 wxT("gro"),
175 wxT("midi"),
176 wxT("mid"),
177};
178
179const auto DESC = XO("MIDI files");
180
182{
183public:
184 explicit MIDIImportFileHandle(const FilePath &fileName)
185 : mFileName{ fileName }
186 {}
187
189
190 FilePath GetFilename() const override { return mFileName; }
191
193
195 {
196 // TODO: Get Uncompressed byte count.
197 return 0;
198 }
199
200 wxInt32 GetStreamCount() override { return 1; }
201
203 {
204 static TranslatableStrings empty;
205 return empty;
206 }
207
208 void Import(
209 ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
210 TrackHolders& outTracks, Tags* tags,
211 std::optional<LibFileFormats::AcidizerTags>& outAcidTags) override;
212
213 void Cancel() override {}
214 void Stop() override {}
215
216 void SetStreamUsage(wxInt32, bool) override {}
217
219};
220
221void MIDIImportFileHandle::Import(
222 ImportProgressListener& progressListener, WaveTrackFactory*,
223 TrackHolders& outTracks, Tags*, std::optional<LibFileFormats::AcidizerTags>&)
224{
225 auto newTrack = std::make_shared<NoteTrack>();
226 if (::ImportMIDI(mFileName, newTrack.get())) {
227 outTracks.push_back(newTrack);
228 progressListener.OnImportResult(
230 }
231 else
232 progressListener.OnImportResult(
234}
235
236class MIDIImportPlugin final : public ImportPlugin
237{
238public:
241 {}
242 ~MIDIImportPlugin() override {}
243
244 wxString GetPluginStringID() override { return wxT("portsmf"); }
245
247
248 std::unique_ptr<ImportFileHandle> Open(const FilePath &fileName,
249 AudacityProject *project) override
250 {
251 return std::make_unique<MIDIImportFileHandle>(fileName);
252 }
253};
254
256 std::make_unique<MIDIImportPlugin>()
257};
258
259}
260
261#include "ModuleConstants.h"
263
264#endif
wxT("CloseDown"))
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
const ReservedCommandFlag & AudioIONotBusyFlag()
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
The interface that all file import "plugins" (if you want to call them that) must implement....
std::vector< std::shared_ptr< Track > > TrackHolders
Definition: ImportRaw.h:24
#define DEFINE_MODULE_ENTRIES
wxString FilePath
Definition: Project.h:21
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 ...
accessors for certain important windows associated with each project
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
const auto tracks
const auto project
std::vector< TranslatableString > TranslatableStrings
static const auto fn
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
AudacityProject & project
void Append(const FilePath &file)
Definition: FileHistory.h:46
static FileHistory & Global()
Definition: FileHistory.cpp:39
FILES_API const FileType AllFiles
Definition: FileNames.h:70
Abstract base class used in importing a file.
Base class for FlacImportFileHandle, LOFImportFileHandle, MP3ImportFileHandle, OggImportFileHandle an...
Definition: ImportPlugin.h:111
unsigned long long ByteCount
Definition: ImportPlugin.h:114
Base class for FlacImportPlugin, LOFImportPlugin, MP3ImportPlugin, OggImportPlugin and PCMImportPlugi...
Definition: ImportPlugin.h:67
Interface used to report on import state and progress.
virtual void OnImportResult(ImportResult result)=0
Used to report on import result for file handle passed as argument to OnImportFileOpened.
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:78
void SetSequence(std::unique_ptr< Alg_seq > &&seq)
Definition: NoteTrack.cpp:256
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.
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)
Generates classes whose instances register items at construction.
Definition: Registry.h:388
ID3 Tags (for MP3)
Definition: Tags.h:73
void SetName(const wxString &n)
Definition: Track.cpp:69
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
Holds a msgid for the translation catalog; may also bind format arguments.
void ZoomFitHorizontallyAndShowTrack(Track *pTrack)
Definition: Viewport.cpp:439
static Viewport & Get(AudacityProject &project)
Definition: Viewport.cpp:33
Used to create or clone a WaveTrack, with appropriate context from the project that will own the trac...
Definition: WaveTrack.h:870
TranslatableString GetPluginFormatDescription() override
Definition: ImportMIDI.cpp:246
std::unique_ptr< ImportFileHandle > Open(const FilePath &fileName, AudacityProject *project) override
Definition: ImportMIDI.cpp:248
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
constexpr auto Command
Definition: MenuRegistry.h:456
void SelectNone(AudacityProject &project)
bool DoImportMIDI(AudacityProject &project, const FilePath &fileName)
Definition: ImportMIDI.cpp:41
Importer::RegisteredImportPlugin registered
Definition: ImportMIDI.cpp:255
void OnImportMIDI(const CommandContext &context)
Definition: ImportMIDI.cpp:140
bool ImportMIDI(const FilePath &fName, NoteTrack *dest)
Definition: ImportMIDI.cpp:85
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
const TranslatableStrings & GetStreamInfo() override
Definition: ImportMIDI.cpp:202