Audacity 3.2.0
ExportMIDI.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file ExportMIDI.cpp
6
7 Paul Licameli split from FileMenus.cpp
8
9**********************************************************************/
10
11#include <wx/frame.h>
12
13#if defined(USE_MIDI)
14
15
16//#include "strparse.h"
17//#include "mfmidi.h"
18
19#include "FileNames.h"
20#include "NoteTrack.h"
21#include "Project.h"
22#include "ProjectWindows.h"
23#include "SelectFile.h"
24#include "AudacityMessageBox.h"
26
27#include "ShuttleGui.h"
29
30namespace {
32{
33 S.StartStatic(XO("Exported Allegro (.gro) files save time as:"));
34 {
35 // Bug 2692: Place button group in panel so tabbing will work and,
36 // on the Mac, VoiceOver will announce as radio buttons.
37 S.StartPanel();
38 {
39 S.StartRadioButtonGroup(NoteTrack::AllegroStyleSetting);
40 {
41 S.TieRadioButton();
42 S.TieRadioButton();
43 }
44 S.EndRadioButtonGroup();
45 }
46 S.EndPanel();
47 }
48 S.EndStatic();
49}
50
52 wxT("AllegroTimeOption"), AddControls };
53}
54
55// Insert a menu item
56#include "CommandContext.h"
57#include "MenuRegistry.h"
58#include "CommonCommandFlags.h"
59
60namespace {
63 [](const AudacityProject &project){
64 return !TrackList::Get(project).Any<const NoteTrack>().empty();
65 }
66 }; return flag; } //gsw
67
68using namespace MenuRegistry;
69
70void OnExportMIDI(const CommandContext &context)
71{
72 auto &project = context.project;
73 auto &tracks = TrackList::Get( project );
74 auto &window = GetProjectFrame( project );
75
76 // Make sure that there is
77 // exactly one NoteTrack selected.
78 const auto range = tracks.Selected<const NoteTrack>();
79 const auto numNoteTracksSelected = range.size();
80
81 if(numNoteTracksSelected > 1) {
83 XO("Please select only one Note Track at a time.") );
84 return;
85 }
86 else if(numNoteTracksSelected < 1) {
88 XO("Please select a Note Track.") );
89 return;
90 }
91
92 wxASSERT(numNoteTracksSelected);
93 if (!numNoteTracksSelected)
94 return;
95
96 const auto nt = *range.begin();
97
98 while(true) {
99
100 wxString fName;
101
102 fName = SelectFile(FileNames::Operation::Export,
103 XO("Export MIDI As:"),
104 wxEmptyString,
105 fName,
106 wxT("mid"),
107 {
108 { XO("MIDI file"), { wxT("mid") }, true },
109 { XO("Allegro file"), { wxT("gro") }, true },
110 },
111 wxFD_SAVE | wxFD_OVERWRITE_PROMPT | wxRESIZE_BORDER,
112 &window);
113
114 if (fName.empty())
115 return;
116
117 if(!fName.Contains(wxT("."))) {
118 fName = fName + wxT(".mid");
119 }
120
121 // Move existing files out of the way. Otherwise wxTextFile will
122 // append to (rather than replace) the current file.
123
124 if (wxFileExists(fName)) {
125#ifdef __WXGTK__
126 wxString safetyFileName = fName + wxT("~");
127#else
128 wxString safetyFileName = fName + wxT(".bak");
129#endif
130
131 if (wxFileExists(safetyFileName))
132 wxRemoveFile(safetyFileName);
133
134 wxRename(fName, safetyFileName);
135 }
136
137 if(fName.EndsWith(wxT(".mid")) || fName.EndsWith(wxT(".midi"))) {
138 nt->ExportMIDI(fName);
139 } else if(fName.EndsWith(wxT(".gro"))) {
140 nt->ExportAllegro(fName);
141 } else {
142 auto msg = XO(
143"You have selected a filename with an unrecognized file extension.\nDo you want to continue?");
144 auto title = XO("Export MIDI");
145 int id = AudacityMessageBox( msg, title, wxYES_NO );
146 if (id == wxNO) {
147 continue;
148 } else if (id == wxYES) {
149 nt->ExportMIDI(fName);
150 }
151 }
152 break;
153 }
154}
155
157 Command( wxT("ExportMIDI"), XXO("Export MI&DI..."), OnExportMIDI,
159 { wxT("File/Import-Export/ExportOther"),
160 { OrderingHint::After, {"ExportLabels"} } }
161};
162
163}
164
165#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")
static const auto title
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
#define S(N)
Definition: ToChars.cpp:64
static std::once_flag flag
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
size_t size() const
How many attachment pointers are in the Site.
Definition: ClientData.h:259
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
AudacityProject & project
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:86
static EnumSetting< bool > AllegroStyleSetting
Definition: NoteTrack.h:89
Generates classes whose instances register items at construction.
Definition: Registry.h:388
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:630
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1079
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:347
constexpr auto Command
Definition: MenuRegistry.h:456
ImportExportPrefs::RegisteredControls reg
Definition: ExportMIDI.cpp:51
void OnExportMIDI(const CommandContext &context)
Definition: ExportMIDI.cpp:70
const ReservedCommandFlag & NoteTracksExistFlag()
Definition: ExportMIDI.cpp:62