yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::editor::music::MusicPlayer Class Reference

Handles audio playback for the music editor using the SNES APU emulator. More...

#include <music_player.h>

Collaboration diagram for yaze::editor::music::MusicPlayer:

Public Member Functions

 MusicPlayer (zelda3::music::MusicBank *music_bank)
 
 ~MusicPlayer ()
 
void SetRom (Rom *rom)
 
void SetEmulator (emu::Emulator *emulator)
 Set the main emulator instance to use for playback.
 
void SetAudioExclusivityCallback (std::function< void(bool)> callback)
 Set a callback to be called when audio playback starts/stops.
 
emu::Emulatoremulator ()
 
void Update ()
 Call once per frame to update playback state.
 
void PlaySong (int song_index)
 Start playing a song by index.
 
void Pause ()
 Pause the current playback.
 
void Resume ()
 Resume paused playback.
 
void Stop ()
 Stop playback completely.
 
void TogglePlayPause ()
 Toggle between play/pause states.
 
void PreviewNote (const zelda3::music::MusicSong &song, const zelda3::music::TrackEvent &event, int segment_index, int channel_index)
 Preview a single note with the current instrument.
 
void PreviewSegment (const zelda3::music::MusicSong &song, int segment_index)
 Preview a specific segment of a song.
 
void PreviewInstrument (int instrument_index)
 Preview an instrument at middle C.
 
void PreviewSample (int sample_index)
 Preview a raw BRR sample.
 
void PreviewCustomSong (int song_index)
 Preview a custom (modified) song from memory.
 
void SetVolume (float volume)
 Set the master volume (0.0 to 1.0).
 
void SetPlaybackSpeed (float speed)
 Set the playback speed (0.25x to 2.0x).
 
void SetInterpolationType (int type)
 Set the DSP interpolation type for audio quality.
 
void SetDirectSpcMode (bool enabled)
 Enable/disable direct SPC mode (bypasses game CPU).
 
void SeekToSegment (int segment_index)
 Seek to a specific segment in the current song.
 
PlaybackState GetState () const
 
PlaybackMode GetMode () const
 
ChannelState GetChannelState (int channel_index) const
 
std::array< ChannelState, 8 > GetChannelStates () const
 
bool IsAudioReady () const
 Check if the audio system is ready for playback.
 
bool IsPlaying () const
 Check if currently playing.
 
bool IsPaused () const
 Check if currently paused.
 
int GetPlayingSongIndex () const
 Get the index of the currently playing song, or -1 if none.
 
const zelda3::music::MusicInstrumentResolveInstrumentForEvent (const zelda3::music::MusicSegment &segment, int channel_index, uint16_t tick) const
 Resolve the instrument used at a specific tick in a track.
 
DspDebugStatus GetDspStatus () const
 Get DSP buffer diagnostic status.
 
ApuDebugStatus GetApuStatus () const
 Get APU timing diagnostic status.
 
AudioQueueStatus GetAudioQueueStatus () const
 Get audio queue diagnostic status.
 
void ClearAudioQueue ()
 Clear the audio queue (stops sound immediately).
 
void ResetDspBuffer ()
 Reset the DSP sample buffer.
 
void ForceNewFrame ()
 Force a DSP NewFrame() call.
 
void ReinitAudio ()
 Reinitialize the audio system.
 

Private Member Functions

bool EnsureAudioReady ()
 
bool EnsurePreviewReady ()
 
void InitializeDirectSpc ()
 
void InitializePreviewMode ()
 
void PlaySongDirect (int song_id)
 
void UploadSoundBankFromRom (uint32_t rom_offset)
 
void UploadSongToAram (const std::vector< uint8_t > &data, uint16_t aram_address)
 
uint32_t GetBankRomOffset (uint8_t bank) const
 
int GetSongIndexInBank (int song_id, uint8_t bank) const
 
float CalculateTicksPerSecond (uint8_t tempo) const
 
uint32_t GetCurrentPlaybackTick () const
 
uint8_t GetSongTempo (const zelda3::music::MusicSong &song) const
 
void TransitionTo (PlaybackMode new_mode)
 
void PrepareAudioPlayback ()
 Prepare audio pipeline for playback.
 

Private Attributes

zelda3::music::MusicBankmusic_bank_ = nullptr
 
emu::Emulatoremulator_ = nullptr
 
Romrom_ = nullptr
 
PlaybackMode mode_ = PlaybackMode::Stopped
 
int playing_song_index_ = -1
 
bool use_direct_spc_ = true
 
float volume_ = 1.0f
 
int interpolation_type_ = 2
 
bool spc_initialized_ = false
 
bool preview_initialized_ = false
 
uint8_t current_spc_bank_ = 0xFF
 
std::chrono::steady_clock::time_point playback_start_time_
 
std::chrono::steady_clock::time_point last_frame_time_
 
uint32_t playback_start_tick_ = 0
 
float ticks_per_second_ = 0.0f
 
int playback_segment_index_ = 0
 
std::function< void(bool)> audio_exclusivity_callback_
 

Detailed Description

Handles audio playback for the music editor using the SNES APU emulator.

The MusicPlayer manages playback of songs from ROM or memory, providing:

  • Song playback with varispeed control (0.25x to 2.0x)
  • Note and segment preview for editing
  • Instrument and sample preview
  • Real-time DSP channel state monitoring

Playback uses direct SPC700/DSP emulation for authentic SNES audio.

Definition at line 122 of file music_player.h.

Constructor & Destructor Documentation

◆ MusicPlayer()

yaze::editor::music::MusicPlayer::MusicPlayer ( zelda3::music::MusicBank * music_bank)
explicit

Definition at line 20 of file music_player.cc.

◆ ~MusicPlayer()

yaze::editor::music::MusicPlayer::~MusicPlayer ( )

Definition at line 23 of file music_player.cc.

References Stop().

Here is the call graph for this function:

Member Function Documentation

◆ SetRom()

void yaze::editor::music::MusicPlayer::SetRom ( Rom * rom)

Definition at line 31 of file music_player.cc.

References rom_.

◆ SetEmulator()

void yaze::editor::music::MusicPlayer::SetEmulator ( emu::Emulator * emulator)

Set the main emulator instance to use for playback.

MusicPlayer controls this emulator directly for audio playback.

Parameters
emulatorThe emulator instance (must outlive MusicPlayer)

Definition at line 27 of file music_player.cc.

References emulator(), and emulator_.

Here is the call graph for this function:

◆ SetAudioExclusivityCallback()

void yaze::editor::music::MusicPlayer::SetAudioExclusivityCallback ( std::function< void(bool)> callback)
inline

Set a callback to be called when audio playback starts/stops.

This allows external systems (like EditorManager) to pause/mute other audio sources (like the main emulator) when MusicPlayer takes control.

Parameters
callbackFunction called with (true) when playback starts, and (false) when playback stops.

Definition at line 148 of file music_player.h.

References audio_exclusivity_callback_.

◆ emulator()

emu::Emulator * yaze::editor::music::MusicPlayer::emulator ( )
inline

Definition at line 153 of file music_player.h.

References emulator_.

Referenced by yaze::editor::MusicAudioDebugPanel::Draw(), and SetEmulator().

◆ Update()

◆ PlaySong()

◆ Pause()

◆ Resume()

◆ Stop()

◆ TogglePlayPause()

void yaze::editor::music::MusicPlayer::TogglePlayPause ( )

Toggle between play/pause states.

Definition at line 132 of file music_player.cc.

References mode_, Pause(), yaze::editor::music::Paused, yaze::editor::music::Playing, playing_song_index_, PlaySong(), yaze::editor::music::Previewing, Resume(), and yaze::editor::music::Stopped.

Here is the call graph for this function:

◆ PreviewNote()

◆ PreviewSegment()

◆ PreviewInstrument()

◆ PreviewSample()

◆ PreviewCustomSong()

◆ SetVolume()

void yaze::editor::music::MusicPlayer::SetVolume ( float volume)

Set the master volume (0.0 to 1.0).

Definition at line 695 of file music_player.cc.

References yaze::emu::Emulator::audio_backend(), emulator_, and yaze::emu::audio::IAudioBackend::SetVolume().

Referenced by yaze::editor::MusicPlaybackControlPanel::DrawToolset().

Here is the call graph for this function:

◆ SetPlaybackSpeed()

void yaze::editor::music::MusicPlayer::SetPlaybackSpeed ( float speed)

Set the playback speed (0.25x to 2.0x).

This affects both tempo and pitch (tape-style varispeed).

Definition at line 701 of file music_player.cc.

Referenced by yaze::editor::MusicPlaybackControlPanel::DrawToolset().

◆ SetInterpolationType()

void yaze::editor::music::MusicPlayer::SetInterpolationType ( int type)

Set the DSP interpolation type for audio quality.

Parameters
type0=Linear, 1=Hermite, 2=Gaussian, 3=Cosine, 4=Cubic

Definition at line 706 of file music_player.cc.

References emulator_, interpolation_type_, yaze::emu::Emulator::is_snes_initialized(), and yaze::emu::Emulator::set_interpolation_type().

Here is the call graph for this function:

◆ SetDirectSpcMode()

void yaze::editor::music::MusicPlayer::SetDirectSpcMode ( bool enabled)

Enable/disable direct SPC mode (bypasses game CPU).

Definition at line 713 of file music_player.cc.

References use_direct_spc_.

◆ SeekToSegment()

void yaze::editor::music::MusicPlayer::SeekToSegment ( int segment_index)

◆ GetState()

◆ GetMode()

PlaybackMode yaze::editor::music::MusicPlayer::GetMode ( ) const
inline

Definition at line 250 of file music_player.h.

References mode_.

◆ GetChannelState()

◆ GetChannelStates()

std::array< ChannelState, 8 > yaze::editor::music::MusicPlayer::GetChannelStates ( ) const

Definition at line 886 of file music_player.cc.

References emulator_, yaze::emu::Emulator::is_snes_initialized(), and yaze::emu::Emulator::snes().

Referenced by yaze::editor::MusicPlaybackControlPanel::DrawDebugControls().

Here is the call graph for this function:

◆ IsAudioReady()

bool yaze::editor::music::MusicPlayer::IsAudioReady ( ) const

Check if the audio system is ready for playback.

Definition at line 275 of file music_player.cc.

References rom_.

Referenced by yaze::editor::MusicPlaybackControlPanel::DrawToolset().

◆ IsPlaying()

bool yaze::editor::music::MusicPlayer::IsPlaying ( ) const
inline

Check if currently playing.

Definition at line 262 of file music_player.h.

References mode_, and yaze::editor::music::Playing.

◆ IsPaused()

bool yaze::editor::music::MusicPlayer::IsPaused ( ) const
inline

Check if currently paused.

Definition at line 267 of file music_player.h.

References mode_, and yaze::editor::music::Paused.

◆ GetPlayingSongIndex()

int yaze::editor::music::MusicPlayer::GetPlayingSongIndex ( ) const
inline

Get the index of the currently playing song, or -1 if none.

Definition at line 272 of file music_player.h.

References playing_song_index_.

◆ ResolveInstrumentForEvent()

const zelda3::music::MusicInstrument * yaze::editor::music::MusicPlayer::ResolveInstrumentForEvent ( const zelda3::music::MusicSegment & segment,
int channel_index,
uint16_t tick ) const

Resolve the instrument used at a specific tick in a track.

Definition at line 1114 of file music_player.cc.

References yaze::zelda3::music::TrackEvent::Command, yaze::zelda3::music::MusicBank::GetInstrument(), music_bank_, and yaze::zelda3::music::MusicSegment::tracks.

Here is the call graph for this function:

◆ GetDspStatus()

◆ GetApuStatus()

◆ GetAudioQueueStatus()

◆ ClearAudioQueue()

void yaze::editor::music::MusicPlayer::ClearAudioQueue ( )

Clear the audio queue (stops sound immediately).

Definition at line 1213 of file music_player.cc.

References yaze::emu::Emulator::audio_backend(), emulator_, and LOG_INFO.

Referenced by yaze::editor::MusicAudioDebugPanel::DrawDebugActions(), and yaze::editor::MusicPlaybackControlPanel::DrawDebugControls().

Here is the call graph for this function:

◆ ResetDspBuffer()

void yaze::editor::music::MusicPlayer::ResetDspBuffer ( )

Reset the DSP sample buffer.

Definition at line 1222 of file music_player.cc.

References emulator_, yaze::emu::Emulator::is_snes_initialized(), LOG_INFO, and yaze::emu::Emulator::snes().

Referenced by yaze::editor::MusicAudioDebugPanel::DrawDebugActions(), and yaze::editor::MusicPlaybackControlPanel::DrawDebugControls().

Here is the call graph for this function:

◆ ForceNewFrame()

void yaze::editor::music::MusicPlayer::ForceNewFrame ( )

Force a DSP NewFrame() call.

Definition at line 1230 of file music_player.cc.

References emulator_, yaze::emu::Emulator::is_snes_initialized(), LOG_INFO, and yaze::emu::Emulator::snes().

Referenced by yaze::editor::MusicAudioDebugPanel::DrawDebugActions(), and yaze::editor::MusicPlaybackControlPanel::DrawDebugControls().

Here is the call graph for this function:

◆ ReinitAudio()

void yaze::editor::music::MusicPlayer::ReinitAudio ( )

Reinitialize the audio system.

Definition at line 1238 of file music_player.cc.

References current_spc_bank_, emulator_, LOG_INFO, preview_initialized_, spc_initialized_, and Stop().

Referenced by yaze::editor::MusicAudioDebugPanel::DrawDebugActions(), and yaze::editor::MusicPlaybackControlPanel::DrawDebugControls().

Here is the call graph for this function:

◆ EnsureAudioReady()

◆ EnsurePreviewReady()

bool yaze::editor::music::MusicPlayer::EnsurePreviewReady ( )
private

Definition at line 336 of file music_player.cc.

References EnsureAudioReady(), InitializePreviewMode(), and preview_initialized_.

Referenced by PreviewInstrument(), and PreviewSample().

Here is the call graph for this function:

◆ InitializeDirectSpc()

void yaze::editor::music::MusicPlayer::InitializeDirectSpc ( )
private

◆ InitializePreviewMode()

◆ PlaySongDirect()

◆ UploadSoundBankFromRom()

void yaze::editor::music::MusicPlayer::UploadSoundBankFromRom ( uint32_t rom_offset)
private

Definition at line 717 of file music_player.cc.

References yaze::Rom::data(), emulator_, LOG_INFO, LOG_WARN, rom_, yaze::Rom::size(), and yaze::emu::Emulator::snes().

Referenced by InitializeDirectSpc(), InitializePreviewMode(), and PlaySongDirect().

Here is the call graph for this function:

◆ UploadSongToAram()

void yaze::editor::music::MusicPlayer::UploadSongToAram ( const std::vector< uint8_t > & data,
uint16_t aram_address )
private

Definition at line 747 of file music_player.cc.

References emulator_, and yaze::emu::Emulator::snes().

Referenced by PreviewCustomSong(), PreviewSample(), and PreviewSegment().

Here is the call graph for this function:

◆ GetBankRomOffset()

uint32_t yaze::editor::music::MusicPlayer::GetBankRomOffset ( uint8_t bank) const
private

◆ GetSongIndexInBank()

int yaze::editor::music::MusicPlayer::GetSongIndexInBank ( int song_id,
uint8_t bank ) const
private

Definition at line 762 of file music_player.cc.

◆ CalculateTicksPerSecond()

float yaze::editor::music::MusicPlayer::CalculateTicksPerSecond ( uint8_t tempo) const
private

◆ GetCurrentPlaybackTick()

uint32_t yaze::editor::music::MusicPlayer::GetCurrentPlaybackTick ( ) const
private

◆ GetSongTempo()

uint8_t yaze::editor::music::MusicPlayer::GetSongTempo ( const zelda3::music::MusicSong & song) const
private

◆ TransitionTo()

◆ PrepareAudioPlayback()

void yaze::editor::music::MusicPlayer::PrepareAudioPlayback ( )
private

Prepare audio pipeline for playback.

Consolidates the common audio priming pattern used by all playback methods:

  • Resets DSP sample buffer
  • Runs one audio frame to generate initial samples
  • Queues initial samples to audio backend
  • Starts audio playback and emulator

Definition at line 73 of file music_player.cc.

References yaze::emu::Emulator::audio_backend(), emulator_, yaze::editor::music::kNativeSampleRate, last_frame_time_, LOG_ERROR, LOG_INFO, yaze::emu::Emulator::ResetFrameTiming(), yaze::emu::Emulator::set_audio_focus_mode(), yaze::emu::Emulator::set_running(), and yaze::emu::Emulator::snes().

Referenced by PreviewCustomSong(), PreviewInstrument(), PreviewNote(), PreviewSample(), and PreviewSegment().

Member Data Documentation

◆ music_bank_

zelda3::music::MusicBank* yaze::editor::music::MusicPlayer::music_bank_ = nullptr
private

◆ emulator_

◆ rom_

Rom* yaze::editor::music::MusicPlayer::rom_ = nullptr
private

◆ mode_

PlaybackMode yaze::editor::music::MusicPlayer::mode_ = PlaybackMode::Stopped
private

◆ playing_song_index_

int yaze::editor::music::MusicPlayer::playing_song_index_ = -1
private

◆ use_direct_spc_

bool yaze::editor::music::MusicPlayer::use_direct_spc_ = true
private

Definition at line 355 of file music_player.h.

Referenced by PlaySong(), SetDirectSpcMode(), and Update().

◆ volume_

float yaze::editor::music::MusicPlayer::volume_ = 1.0f
private

Definition at line 356 of file music_player.h.

◆ interpolation_type_

int yaze::editor::music::MusicPlayer::interpolation_type_ = 2
private

Definition at line 357 of file music_player.h.

Referenced by EnsureAudioReady(), PlaySong(), PlaySongDirect(), and SetInterpolationType().

◆ spc_initialized_

bool yaze::editor::music::MusicPlayer::spc_initialized_ = false
private

Definition at line 360 of file music_player.h.

Referenced by EnsureAudioReady(), InitializeDirectSpc(), and ReinitAudio().

◆ preview_initialized_

bool yaze::editor::music::MusicPlayer::preview_initialized_ = false
private

◆ current_spc_bank_

uint8_t yaze::editor::music::MusicPlayer::current_spc_bank_ = 0xFF
private

Definition at line 362 of file music_player.h.

Referenced by InitializeDirectSpc(), PlaySongDirect(), and ReinitAudio().

◆ playback_start_time_

std::chrono::steady_clock::time_point yaze::editor::music::MusicPlayer::playback_start_time_
private

◆ last_frame_time_

std::chrono::steady_clock::time_point yaze::editor::music::MusicPlayer::last_frame_time_
private

Definition at line 366 of file music_player.h.

Referenced by PlaySong(), PlaySongDirect(), PrepareAudioPlayback(), Resume(), and Update().

◆ playback_start_tick_

uint32_t yaze::editor::music::MusicPlayer::playback_start_tick_ = 0
private

◆ ticks_per_second_

float yaze::editor::music::MusicPlayer::ticks_per_second_ = 0.0f
private

◆ playback_segment_index_

int yaze::editor::music::MusicPlayer::playback_segment_index_ = 0
private

◆ audio_exclusivity_callback_

std::function<void(bool)> yaze::editor::music::MusicPlayer::audio_exclusivity_callback_
private

The documentation for this class was generated from the following files: