2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file bemidi.cpp Support for BeOS midi. */
10 #include "../stdafx.h"
11 #include "../openttd.h"
13 #include "../base_media_base.h"
14 #include "midifile.hpp"
16 #include "../safeguards.h"
18 /** Factory for BeOS' midi player. */
19 static FMusicDriver_BeMidi iFMusicDriver_BeMidi
;
21 std::optional
<std::string_view
> MusicDriver_BeMidi::Start(const StringList
&parm
)
26 void MusicDriver_BeMidi::Stop()
31 void MusicDriver_BeMidi::PlaySong(const MusicSongInfo
&song
)
33 std::string filename
= MidiFile::GetSMFFile(song
);
36 this->midi_synth_file
= new BMidiSynthFile();
37 if (!filename
.empty()) {
39 get_ref_for_path(filename
.c_str(), &midiRef
);
40 if (this->midi_synth_file
->LoadFile(&midiRef
) == B_OK
) {
41 this->midi_synth_file
->SetVolume(this->current_volume
);
42 this->midi_synth_file
->Start();
43 this->just_started
= true;
50 void MusicDriver_BeMidi::StopSong()
52 /* Reusing BMidiSynthFile can cause stuck notes when switching
53 * tracks, just delete whole object entirely. */
54 delete this->midi_synth_file
;
55 this->midi_synth_file
= nullptr;
58 bool MusicDriver_BeMidi::IsSongPlaying()
60 if (this->midi_synth_file
== nullptr) return false;
62 /* IsFinished() returns true for a moment after Start()
63 * but before it really starts playing, use just_started flag
64 * to prevent accidental track skipping. */
65 if (this->just_started
) {
66 if (!this->midi_synth_file
->IsFinished()) this->just_started
= false;
69 return !this->midi_synth_file
->IsFinished();
72 void MusicDriver_BeMidi::SetVolume(uint8_t vol
)
74 this->current_volume
= vol
/ 128.0;
75 if (this->midi_synth_file
!= nullptr) this->midi_synth_file
->SetVolume(this->current_volume
);