Coverage Report

Created: 2026-09-28 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/game-music-emu/gme/Dual_Resampler.cpp
Line
Count
Source
1
// Game_Music_Emu https://bitbucket.org/mpyne/game-music-emu/
2
3
#include "Dual_Resampler.h"
4
5
#include <stdlib.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
Dual_Resampler::Dual_Resampler() :
22
228
  sample_buf_size(0),
23
228
  oversamples_per_frame(-1),
24
228
  buf_pos(-1),
25
228
  resampler_size(0)
26
228
{
27
228
}
28
29
228
Dual_Resampler::~Dual_Resampler() { }
30
31
blargg_err_t Dual_Resampler::reset( int pairs )
32
237
{
33
  // expand allocations a bit
34
237
  RETURN_ERR( sample_buf.resize( (pairs + (pairs >> 2)) * 2 ) );
35
237
  resize( pairs );
36
237
  resampler_size = oversamples_per_frame + (oversamples_per_frame >> 2);
37
237
  return resampler.buffer_size( resampler_size );
38
237
}
39
40
void Dual_Resampler::resize( int pairs )
41
333
{
42
333
  int new_sample_buf_size = pairs * 2;
43
333
  if ( sample_buf_size != new_sample_buf_size )
44
267
  {
45
267
    if ( (unsigned) new_sample_buf_size > sample_buf.size() )
46
0
    {
47
0
      check( false );
48
0
      return;
49
0
    }
50
267
    sample_buf_size = new_sample_buf_size;
51
267
    oversamples_per_frame = int (pairs * resampler.ratio()) * 2 + 2;
52
267
    clear();
53
267
  }
54
333
}
55
56
void Dual_Resampler::play_frame_( Blip_Buffer& blip_buf, dsample_t* out )
57
11.4k
{
58
11.4k
  long pair_count = sample_buf_size >> 1;
59
11.4k
  blip_time_t blip_time = blip_buf.count_clocks( pair_count );
60
11.4k
  int sample_count = oversamples_per_frame - resampler.written();
61
62
11.4k
  int new_count = play_frame( blip_time, sample_count, resampler.buffer() );
63
11.4k
  assert( new_count < resampler_size );
64
65
11.4k
  blip_buf.end_frame( blip_time );
66
11.4k
  assert( blip_buf.samples_avail() == pair_count );
67
68
11.4k
  resampler.write( new_count );
69
70
11.4k
#ifdef  NDEBUG // Avoid warning when asserts are disabled
71
11.4k
  resampler.read( sample_buf.begin(), sample_buf_size );
72
#else
73
  long count = resampler.read( sample_buf.begin(), sample_buf_size );
74
  assert( count == (long) sample_buf_size );
75
#endif
76
77
11.4k
  mix_samples( blip_buf, out );
78
11.4k
  blip_buf.remove_samples( pair_count );
79
11.4k
}
80
81
void Dual_Resampler::dual_play( long count, dsample_t* out, Blip_Buffer& blip_buf )
82
8.78k
{
83
  // empty extra buffer
84
8.78k
  long remain = sample_buf_size - buf_pos;
85
8.78k
  if ( remain )
86
8.32k
  {
87
8.32k
    if ( remain > count )
88
39
      remain = count;
89
8.32k
    count -= remain;
90
8.32k
    memcpy( out, &sample_buf [buf_pos], remain * sizeof *out );
91
8.32k
    out += remain;
92
8.32k
    buf_pos += remain;
93
8.32k
  }
94
95
  // entire frames
96
11.7k
  while ( count >= (long) sample_buf_size )
97
3.00k
  {
98
3.00k
    play_frame_( blip_buf, out );
99
3.00k
    out += sample_buf_size;
100
3.00k
    count -= sample_buf_size;
101
3.00k
  }
102
103
  // extra
104
8.78k
  if ( count )
105
8.44k
  {
106
8.44k
    play_frame_( blip_buf, sample_buf.begin() );
107
8.44k
    buf_pos = count;
108
8.44k
    memcpy( out, sample_buf.begin(), count * sizeof *out );
109
8.44k
    out += count;
110
8.44k
  }
111
8.78k
}
112
113
void Dual_Resampler::mix_samples( Blip_Buffer& blip_buf, dsample_t* out )
114
11.4k
{
115
11.4k
  Blip_Reader sn;
116
11.4k
  int bass = sn.begin( blip_buf );
117
11.4k
  const dsample_t* in = sample_buf.begin();
118
119
9.31M
  for ( int n = sample_buf_size >> 1; n--; )
120
9.30M
  {
121
9.30M
    int s = sn.read();
122
9.30M
    int32_t l = (int32_t) in [0] * 2 + s;
123
9.30M
    if ( (int16_t) l != l )
124
0
      l = 0x7FFF - (l >> 24);
125
126
9.30M
    sn.next( bass );
127
9.30M
    int32_t r = (int32_t) in [1] * 2 + s;
128
9.30M
    if ( (int16_t) r != r )
129
0
      r = 0x7FFF - (r >> 24);
130
131
9.30M
    in += 2;
132
9.30M
    out [0] = l;
133
9.30M
    out [1] = r;
134
9.30M
    out += 2;
135
9.30M
  }
136
137
11.4k
  sn.end( blip_buf );
138
11.4k
}
139