Audacity 3.2.0
Namespaces | Functions | Variables
ImportMIDI.cpp File Reference
#include "ImportMIDI.h"
#include <wx/defs.h>
#include <wx/ffile.h>
#include <wx/frame.h>
#include "../lib-src/header-substitutes/allegro.h"
#include "FileNames.h"
#include "../NoteTrack.h"
#include "Project.h"
#include "ProjectFileIO.h"
#include "ProjectHistory.h"
#include "../ProjectWindow.h"
#include "ProjectWindows.h"
#include "SelectFile.h"
#include "../SelectUtilities.h"
#include "AudacityMessageBox.h"
#include "../widgets/FileHistory.h"
#include "../commands/CommandContext.h"
#include "../commands/CommandManager.h"
#include "../CommonCommandFlags.h"
Include dependency graph for ImportMIDI.cpp:

Go to the source code of this file.

Namespaces

namespace  anonymous_namespace{ImportMIDI.cpp}
 

Functions

bool DoImportMIDI (AudacityProject &project, const FilePath &fileName)
 
bool ImportMIDI (const FilePath &fName, NoteTrack *dest)
 
void anonymous_namespace{ImportMIDI.cpp}::OnImportMIDI (const CommandContext &context)
 

Variables

AttachedItem anonymous_namespace{ImportMIDI.cpp}::sAttachment
 

Function Documentation

◆ DoImportMIDI()

bool DoImportMIDI ( AudacityProject project,
const FilePath fileName 
)

Definition at line 39 of file ImportMIDI.cpp.

40{
41 auto &projectFileIO = ProjectFileIO::Get( project );
42 auto &tracks = TrackList::Get( project );
43 auto newTrack = std::make_shared<NoteTrack>();
44 bool initiallyEmpty = tracks.empty();
45
46 if (::ImportMIDI(fileName, newTrack.get())) {
47
49 auto pTrack = tracks.Add( newTrack );
50 pTrack->SetSelected(true);
51
52 // Fix the bug 2109.
53 // In case the project had soloed tracks before importing,
54 // the newly imported track is muted.
55 const bool projectHasSolo =
56 !(tracks.Any<PlayableTrack>() + &PlayableTrack::GetSolo).empty();
57#ifdef EXPERIMENTAL_MIDI_OUT
58 if (projectHasSolo)
59 pTrack->SetMute(true);
60#endif
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")
bool ImportMIDI(const FilePath &fName, NoteTrack *dest)
Definition: ImportMIDI.cpp:85
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)
void ZoomAfterImport(Track *pTrack)
static ProjectWindow & Get(AudacityProject &project)
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:354
void SelectNone(AudacityProject &project)

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

Referenced by DoImportMIDIProject(), anonymous_namespace{ImportMIDI.cpp}::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 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 dest->ZoomAllNotes();
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:388
void MoveTo(double origin) override
Change start time to given time point.
Definition: NoteTrack.h:81
void ZoomAllNotes()
Zooms so that all notes are visible.
Definition: NoteTrack.cpp:1106
void SetName(const wxString &n)
Definition: Track.cpp:61

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

Referenced by DoImportMIDI().

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