cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

Audio.cpp (3565B)


      1/*
      2 * Gearboy - Nintendo Game Boy Emulator
      3 * Copyright (C) 2012  Ignacio Sanchez
      4
      5 * This program is free software: you can redistribute it and/or modify
      6 * it under the terms of the GNU General Public License as published by
      7 * the Free Software Foundation, either version 3 of the License, or
      8 * any later version.
      9
     10 * This program is distributed in the hope that it will be useful,
     11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13 * GNU General Public License for more details.
     14
     15 * You should have received a copy of the GNU General Public License
     16 * along with this program.  If not, see http://www.gnu.org/licenses/
     17 *
     18 */
     19
     20#include "Audio.h"
     21#include "Memory.h"
     22
     23Audio::Audio()
     24{
     25    m_bCGB = false;
     26    m_ElapsedCycles = 0;
     27    m_SampleRate = 44100;
     28    InitPointer(m_pApu);
     29    InitPointer(m_pBuffer);
     30    InitPointer(m_pSampleBuffer);
     31}
     32
     33Audio::~Audio()
     34{
     35    SafeDelete(m_pApu);
     36    SafeDelete(m_pBuffer);
     37    SafeDeleteArray(m_pSampleBuffer);
     38}
     39
     40void Audio::Init()
     41{
     42    m_pSampleBuffer = new blip_sample_t[AUDIO_BUFFER_SIZE];
     43
     44    m_pApu = new Gb_Apu();
     45    m_pBuffer = new Stereo_Buffer();
     46
     47    m_pBuffer->clock_rate(4194304);
     48    m_pBuffer->set_sample_rate(m_SampleRate);
     49
     50    //m_pApu->treble_eq(-15.0);
     51    //m_pBuffer->bass_freq(100);
     52
     53    m_pApu->set_output(m_pBuffer->center(), m_pBuffer->left(), m_pBuffer->right());
     54}
     55
     56void Audio::Reset(bool bCGB)
     57{
     58    m_bCGB = bCGB;
     59
     60    Gb_Apu::mode_t mode = m_bCGB ? Gb_Apu::mode_cgb : Gb_Apu::mode_dmg;
     61    m_pApu->reset(mode);
     62    m_pBuffer->clear();
     63
     64    for (int reg = 0xFF10; reg <= 0xFF3F; reg++)
     65    {
     66        u8 value = m_bCGB ? kInitialValuesForColorFFXX[reg - 0xFF00] : kInitialValuesForFFXX[reg - 0xFF00];
     67        m_pApu->write_register(0, reg, value);
     68    }
     69
     70    m_ElapsedCycles = 0;
     71}
     72
     73void Audio::SetSampleRate(int rate)
     74{
     75    if (rate != m_SampleRate)
     76    {
     77        m_SampleRate = rate;
     78        m_pBuffer->set_sample_rate(m_SampleRate);
     79    }
     80}
     81
     82void Audio::SetVolume(float volume)
     83{
     84    m_pApu->volume(volume);
     85}
     86
     87void Audio::EndFrame(s16* pSampleBuffer, int* pSampleCount)
     88{
     89    m_pApu->end_frame(m_ElapsedCycles);
     90    m_pBuffer->end_frame(m_ElapsedCycles);
     91
     92    int count = static_cast<int>(m_pBuffer->read_samples(m_pSampleBuffer, AUDIO_BUFFER_SIZE));
     93
     94    if (IsValidPointer(pSampleBuffer) && IsValidPointer(pSampleCount))
     95    {
     96        *pSampleCount = count;
     97
     98        for (int i=0; i<count; i++)
     99        {
    100            pSampleBuffer[i] = m_pSampleBuffer[i];
    101        }
    102    }
    103
    104    m_ElapsedCycles = 0;
    105}
    106
    107void Audio::SaveState(std::ostream& stream)
    108{
    109    using namespace std;
    110
    111    gb_apu_state_t apu_state;
    112
    113    m_pApu->save_state(&apu_state);
    114
    115    stream.write(reinterpret_cast<const char*> (&m_ElapsedCycles), sizeof(m_ElapsedCycles));
    116    stream.write(reinterpret_cast<const char*> (m_pSampleBuffer), sizeof(blip_sample_t) * AUDIO_BUFFER_SIZE);
    117    stream.write(reinterpret_cast<const char*> (&apu_state), sizeof(apu_state));
    118}
    119
    120void Audio::LoadState(std::istream& stream)
    121{
    122    using namespace std;
    123
    124    gb_apu_state_t apu_state;
    125
    126    stream.read(reinterpret_cast<char*> (&m_ElapsedCycles), sizeof(m_ElapsedCycles));
    127    stream.read(reinterpret_cast<char*> (m_pSampleBuffer), sizeof(blip_sample_t) * AUDIO_BUFFER_SIZE);
    128    stream.read(reinterpret_cast<char*> (&apu_state), sizeof(apu_state));
    129
    130    Gb_Apu::mode_t mode = m_bCGB ? Gb_Apu::mode_cgb : Gb_Apu::mode_dmg;
    131    m_pApu->reset(mode);
    132    m_pApu->load_state(apu_state);
    133    m_pBuffer->clear();
    134}
    135
    136Gb_Apu* Audio::GetApu()
    137{
    138    return m_pApu;
    139}