OpenTTD Source  20241108-master-g80f628063a
bemidi.cpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../openttd.h"
12 #include "bemidi.h"
13 #include "../base_media_base.h"
14 #include "midifile.hpp"
15 
16 #include "../safeguards.h"
17 
20 
21 std::optional<std::string_view> MusicDriver_BeMidi::Start(const StringList &parm)
22 {
23  return std::nullopt;
24 }
25 
27 {
28  this->StopSong();
29 }
30 
32 {
33  std::string filename = MidiFile::GetSMFFile(song);
34 
35  this->Stop();
36  this->midi_synth_file = new BMidiSynthFile();
37  if (!filename.empty()) {
38  entry_ref midiRef;
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;
44  } else {
45  this->Stop();
46  }
47  }
48 }
49 
51 {
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;
56 }
57 
59 {
60  if (this->midi_synth_file == nullptr) return false;
61 
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;
67  return true;
68  }
69  return !this->midi_synth_file->IsFinished();
70 }
71 
73 {
74  this->current_volume = vol / 128.0;
75  if (this->midi_synth_file != nullptr) this->midi_synth_file->SetVolume(this->current_volume);
76 }
static FMusicDriver_BeMidi iFMusicDriver_BeMidi
Factory for BeOS' midi player.
Definition: bemidi.cpp:19
Base of BeOS Midi support.
Factory for the BeOS midi player.
Definition: bemidi.h:41
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition: bemidi.cpp:31
void StopSong() override
Stop playing the current song.
Definition: bemidi.cpp:50
void Stop() override
Stop this driver.
Definition: bemidi.cpp:26
bool IsSongPlaying() override
Are we currently playing a song?
Definition: bemidi.cpp:58
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition: bemidi.cpp:21
void SetVolume(uint8_t vol) override
Set the volume, if possible.
Definition: bemidi.cpp:72
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1028
Metadata about a music track.