Coverage Report

Created: 2026-07-12 08:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/game-music-emu/gme/Classic_Emu.cpp
Line
Count
Source
1
// Game_Music_Emu https://bitbucket.org/mpyne/game-music-emu/
2
3
#include "Classic_Emu.h"
4
5
#include "Multi_Buffer.h"
6
#include <string.h>
7
8
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
9
can redistribute it and/or modify it under the terms of the GNU Lesser
10
General Public License as published by the Free Software Foundation; either
11
version 2.1 of the License, or (at your option) any later version. This
12
module is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15
details. You should have received a copy of the GNU Lesser General Public
16
License along with this module; if not, write to the Free Software Foundation,
17
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18
19
#include "blargg_source.h"
20
21
Classic_Emu::Classic_Emu()
22
2.19k
{
23
2.19k
  buf           = 0;
24
2.19k
  stereo_buffer = 0;
25
2.19k
  voice_types   = 0;
26
27
  // avoid inconsistency in our duplicated constants
28
2.19k
  blaarg_static_assert( (int) wave_type  == (int) Multi_Buffer::wave_type, "wave_type inconsistent across two classes using it" );
29
2.19k
  blaarg_static_assert( (int) noise_type == (int) Multi_Buffer::noise_type, "noise_type inconsistent across two classes using it"  );
30
2.19k
  blaarg_static_assert( (int) mixed_type == (int) Multi_Buffer::mixed_type, "mixed_type inconsistent across two classes using it"  );
31
2.19k
}
32
33
Classic_Emu::~Classic_Emu()
34
2.19k
{
35
2.19k
  delete stereo_buffer;
36
2.19k
}
37
38
void Classic_Emu::set_equalizer_( equalizer_t const& eq )
39
4.37k
{
40
4.37k
  Music_Emu::set_equalizer_( eq );
41
4.37k
  update_eq( eq.treble );
42
4.37k
  if ( buf )
43
2.21k
    buf->bass_freq( (int) equalizer().bass );
44
4.37k
}
45
46
blargg_err_t Classic_Emu::set_sample_rate_( long rate )
47
2.19k
{
48
2.19k
  if ( !buf )
49
0
  {
50
0
    if ( !stereo_buffer )
51
0
      CHECK_ALLOC( stereo_buffer = BLARGG_NEW Stereo_Buffer );
52
0
    buf = stereo_buffer;
53
0
  }
54
2.19k
  return buf->set_sample_rate( rate, 1000 / 20 );
55
2.19k
}
56
57
blargg_err_t Classic_Emu::set_multi_channel ( bool is_enabled )
58
2.10k
{
59
2.10k
        RETURN_ERR( Music_Emu::set_multi_channel_( is_enabled ) );
60
2.10k
        return 0;
61
2.10k
}
62
63
void Classic_Emu::mute_voices_( int mask )
64
2.13k
{
65
2.13k
  Music_Emu::mute_voices_( mask );
66
16.3k
  for ( int i = voice_count(); i--; )
67
14.2k
  {
68
14.2k
    if ( mask & (1 << i) )
69
0
    {
70
0
      set_voice( i, 0, 0, 0 );
71
0
    }
72
14.2k
    else
73
14.2k
    {
74
14.2k
      Multi_Buffer::channel_t ch = buf->channel( i, (voice_types ? voice_types [i] : 0) );
75
14.2k
      assert( (ch.center && ch.left && ch.right) ||
76
14.2k
          (!ch.center && !ch.left && !ch.right) ); // all or nothing
77
14.2k
      set_voice( i, ch.center, ch.left, ch.right );
78
14.2k
    }
79
14.2k
  }
80
2.13k
}
81
82
void Classic_Emu::change_clock_rate( long rate )
83
2.21k
{
84
2.21k
  clock_rate_ = rate;
85
2.21k
  buf->clock_rate( rate );
86
2.21k
}
87
88
blargg_err_t Classic_Emu::setup_buffer( long rate )
89
2.21k
{
90
2.21k
  change_clock_rate( rate );
91
2.21k
  RETURN_ERR( buf->set_channel_count( voice_count() ) );
92
2.21k
  set_equalizer( equalizer() );
93
2.21k
  buf_changed_count = buf->channels_changed_count();
94
2.21k
  return 0;
95
2.21k
}
96
97
blargg_err_t Classic_Emu::start_track_( int track )
98
9.43k
{
99
9.43k
  RETURN_ERR( Music_Emu::start_track_( track ) );
100
9.43k
  buf->clear();
101
9.43k
  return 0;
102
9.43k
}
103
104
blargg_err_t Classic_Emu::play_( long count, sample_t* out )
105
3.86M
{
106
3.86M
  long remain = count;
107
9.39M
  while ( remain )
108
5.53M
  {
109
5.53M
    remain -= buf->read_samples( &out [count - remain], remain );
110
5.53M
    if ( remain )
111
1.67M
    {
112
1.67M
      if ( buf_changed_count != buf->channels_changed_count() )
113
0
      {
114
0
        buf_changed_count = buf->channels_changed_count();
115
0
        remute_voices();
116
0
      }
117
1.67M
      int msec = buf->length();
118
1.67M
      blip_time_t clocks_emulated = (blargg_long) msec * clock_rate_ / 1000;
119
1.67M
      RETURN_ERR( run_clocks( clocks_emulated, msec ) );
120
1.67M
      assert( clocks_emulated );
121
1.67M
      buf->end_frame( clocks_emulated );
122
1.67M
    }
123
5.53M
  }
124
3.86M
  return 0;
125
3.86M
}
126
127
// Rom_Data
128
129
blargg_err_t Rom_Data_::load_rom_data_( Data_Reader& in,
130
    int header_size, void* header_out, int fill, long pad_size )
131
2.08k
{
132
2.08k
  long file_offset = pad_size - header_size;
133
134
2.08k
  rom_addr = 0;
135
2.08k
  mask     = 0;
136
2.08k
  size_    = 0;
137
2.08k
  rom.clear();
138
139
2.08k
  file_size_ = in.remain();
140
2.08k
  if ( file_size_ <= header_size ) // <= because there must be data after header
141
8
    return gme_wrong_file_type;
142
2.07k
  blargg_err_t err = rom.resize( file_offset + file_size_ + pad_size );
143
2.07k
  if ( !err )
144
2.07k
    err = in.read( rom.begin() + file_offset, file_size_ );
145
2.07k
  if ( err )
146
0
  {
147
0
    rom.clear();
148
0
    return err;
149
0
  }
150
151
2.07k
  file_size_ -= header_size;
152
2.07k
  memcpy( header_out, &rom [file_offset], header_size );
153
154
2.07k
  memset( rom.begin()         , fill, pad_size );
155
2.07k
  memset( rom.end() - pad_size, fill, pad_size );
156
157
2.07k
  return 0;
158
2.07k
}
159
160
void Rom_Data_::set_addr_( long addr, int unit )
161
4.62k
{
162
4.62k
  rom_addr = addr - unit - pad_extra;
163
164
4.62k
  long rounded = (addr + file_size_ + unit - 1) / unit * unit;
165
4.62k
  if ( rounded <= 0 )
166
1.53k
  {
167
1.53k
    rounded = 0;
168
1.53k
  }
169
3.08k
  else
170
3.08k
  {
171
3.08k
    int shift = 0;
172
3.08k
    unsigned long max_addr = (unsigned long) (rounded - 1);
173
43.9k
    while ( max_addr >> shift )
174
40.9k
      shift++;
175
3.08k
    mask = (1L << shift) - 1;
176
3.08k
  }
177
178
4.62k
  if ( addr < 0 )
179
1.53k
    addr = 0;
180
4.62k
  size_ = rounded;
181
4.62k
  if ( rom.resize( rounded - rom_addr + pad_extra ) ) { } // OK if shrink fails
182
183
4.62k
  if ( 0 )
184
0
  {
185
0
    debug_printf( "addr: %X\n", addr );
186
0
    debug_printf( "file_size: %d\n", file_size_ );
187
0
    debug_printf( "rounded: %d\n", rounded );
188
0
    debug_printf( "mask: $%X\n", mask );
189
0
  }
190
4.62k
}