OpenTTD Source: src/music/bemidi.cpp Source File

OpenTTD Source 15.0-beta1

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
19 static FMusicDriver_BeMidi iFMusicDriver_BeMidi;
20
21std::optional<std::string_view> MusicDriver_BeMidi::Start(const StringList &parm)
22{
23 return std::nullopt;
24}
25
26 void MusicDriver_BeMidi::Stop()
27{
28 this->StopSong();
29}
30
31 void MusicDriver_BeMidi::PlaySong(const MusicSongInfo &song)
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
50 void MusicDriver_BeMidi::StopSong()
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
58 bool MusicDriver_BeMidi::IsSongPlaying()
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
72 void MusicDriver_BeMidi::SetVolume(uint8_t vol)
73{
74 this->current_volume = vol / 128.0;
75 if (this->midi_synth_file != nullptr) this->midi_synth_file->SetVolume(this->current_volume);
76}
iFMusicDriver_BeMidi
static FMusicDriver_BeMidi iFMusicDriver_BeMidi
Factory for BeOS' midi player.
Definition bemidi.cpp:19
bemidi.h
Base of BeOS Midi support.
FMusicDriver_BeMidi
Factory for the BeOS midi player.
Definition bemidi.h:41
MusicDriver_BeMidi::PlaySong
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition bemidi.cpp:31
MusicDriver_BeMidi::StopSong
void StopSong() override
Stop playing the current song.
Definition bemidi.cpp:50
MusicDriver_BeMidi::Stop
void Stop() override
Stop this driver.
Definition bemidi.cpp:26
MusicDriver_BeMidi::IsSongPlaying
bool IsSongPlaying() override
Are we currently playing a song?
Definition bemidi.cpp:58
MusicDriver_BeMidi::Start
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition bemidi.cpp:21
MusicDriver_BeMidi::SetVolume
void SetVolume(uint8_t vol) override
Set the volume, if possible.
Definition bemidi.cpp:72
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
MidiFile::GetSMFFile
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition midifile.cpp:1025
MusicSongInfo
Metadata about a music track.
Definition base_media_base.h:325