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#ifdef EXPERIMENTAL_MIDI_OUT
60 if (projectHasSolo)
61 pTrack->SetMute(true);
62#endif
63
65 .PushState(
66 XO("Imported MIDI from '%s'").Format( fileName ),
67 XO("Import MIDI")
68 );
69
71 FileHistory::Global().Append(fileName);
72
73 // If the project was clean and temporary (not permanently saved), then set
74 // the filename to the just imported path.
75 if (initiallyEmpty && projectFileIO.IsTemporary()) {
76 wxFileName fn(fileName);
77 project.SetProjectName(fn.GetName());
78 project.SetInitialImportPath(fn.GetPath());
79 projectFileIO.SetProjectTitle();
80 }
81 return true;
82 }
83 else
84 return false;
85}
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:347
void ZoomFitHorizontallyAndShowTrack(Track *pTrack)
Definition: Viewport.cpp:461
static Viewport & Get(AudacityProject &project)
Definition: Viewport.cpp:32
void SelectNone(AudacityProject &project)
bool ImportMIDI(const FilePath &fName, NoteTrack *dest)
Definition: ImportMIDI.cpp:87

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

88{
89 if (fName.length() <= 4){
91 XO("Could not open file %s: Filename too short.").Format( fName ) );
92 return false;
93 }
94
95 bool is_midi = false;
96 if (fName.Right(4).CmpNoCase(wxT(".mid")) == 0 || fName.Right(5).CmpNoCase(wxT(".midi")) == 0)
97 is_midi = true;
98 else if(fName.Right(4).CmpNoCase(wxT(".gro")) != 0) {
100 XO("Could not open file %s: Incorrect filetype.").Format( fName ) );
101 return false;
102 }
103
104 wxFFile mf(fName, wxT("rb"));
105 if (!mf.IsOpened()) {
107 XO("Could not open file %s.").Format( fName ) );
108 return false;
109 }
110
111 double offset = 0.0;
112 auto new_seq = std::make_unique<Alg_seq>(fName.mb_str(), is_midi, &offset);
113
114 //Should we also check if(seq->tracks() == 0) ?
115 if(new_seq->get_read_error() == alg_error_open){
117 XO("Could not open file %s.").Format( fName ) );
118 mf.Close();
119 return false;
120 }
121
122 dest->SetSequence(std::move(new_seq));
123 dest->MoveTo(offset);
124 wxString trackNameBase = fName.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
125 dest->SetName(trackNameBase);
126 mf.Close();
127
128 NoteTrackRange::Get(*dest).ZoomAllNotes(&dest->GetSeq());
129 return true;
130}
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:263
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:106
Alg_seq & GetSeq() const
Definition: NoteTrack.cpp:146
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:61

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

143{
144 auto &project = context.project;
145 auto &window = GetProjectFrame( project );
146
147 wxString fileName = SelectFile(FileNames::Operation::Open,
148 XO("Select a MIDI file"),
149 wxEmptyString, // Path
150 wxT(""), // Name
151 wxT(""), // Extension
152 {
153 { XO("MIDI and Allegro files"),
154 { wxT("mid"), wxT("midi"), wxT("gro"), }, true },
155 { XO("MIDI files"),
156 { wxT("mid"), wxT("midi"), }, true },
157 { XO("Allegro files"),
158 { wxT("gro"), }, true },
160 },
161 wxRESIZE_BORDER, // Flags
162 &window); // Parent
163
164 if (!fileName.empty())
165 DoImportMIDI(project, fileName);
166}
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 175 of file ImportMIDI.cpp.

◆ registered

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

Definition at line 256 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:142

Definition at line 168 of file ImportMIDI.cpp.