-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.cpp
356 lines (292 loc) · 7.75 KB
/
base.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "base.h"
#include "config.h"
#include "monitor.h"
#include <cmath>
static t_size sjis_decode_char( const char * p_sjis, t_size max )
{
const t_uint8 * sjis = (const t_uint8 *) p_sjis;
if ( max == 0 )
return 0;
else if ( max > 2 )
max = 2;
if ( sjis [0] < 0x80 )
{
return sjis [0] > 0 ? 1 : 0;
}
if ( max >= 1 )
{
// TODO: definitely weak
if ( unsigned( sjis [0] - 0xA1 ) < 0x3F )
return 1;
}
if ( max >= 2 )
{
// TODO: probably very weak
if ( ( unsigned( sjis [0] - 0x81 ) < 0x1F || unsigned( sjis [0] - 0xE0 ) < 0x10 ) &&
unsigned( sjis [1] - 0x40 ) < 0xBD )
return 2;
}
return 0;
}
static bool is_valid_sjis( const char * param, t_size max = ~0 )
{
t_size walk = 0;
while ( walk < max && param [walk] != 0 )
{
t_size d;
d = sjis_decode_char( param + walk, max - walk );
if ( d == 0 ) return false;
walk += d;
if ( walk > max )
{
return false;
}
}
return true;
}
void input_gep::meta_add( file_info & p_info, const char * name, const char * value, t_size max )
{
if ( value[ 0 ] )
{
pfc::string8 t;
if ( value[ max - 1 ] )
{
// TODO: moo
t.set_string( value, max );
value = t;
}
else max = strlen( value );
if ( pfc::is_lower_ascii( value ) || pfc::is_valid_utf8( value, max ) )
p_info.meta_add( name, value );
else if ( is_valid_sjis( value, max ) )
p_info.meta_add( name, pfc::stringcvt::string_utf8_from_codepage( 932, value ) ); // Shift-JIS
else
p_info.meta_add( name, pfc::stringcvt::string_utf8_from_ansi( value ) );
}
}
input_gep::input_gep()
{
emu = 0;
//voice_mask = 0;
buffer = 0;
sample_rate = cfg_sample_rate;
monitoring = false;
effects_enable = !!cfg_effects_enable;
effects_bass = cfg_effects_bass;
effects_treble = cfg_effects_treble;
effects_echo_depth = cfg_effects_echo_depth;
}
input_gep::~input_gep()
{
if (emu)
{
handle_warning();
delete emu;
}
delete buffer;
if ( monitoring ) monitor_stop();
}
void input_gep::handle_warning(gme_t * emu)
{
const char * s = emu ? emu->warning() : this->emu->warning();
if ( s )
{
pfc::string8 path;
filesystem::g_get_display_path( m_path, path );
console::formatter() << "Emulation warning: " << s << " (" << path << ")";
}
}
void input_gep::monitor_start()
{
monitoring = true;
if (is_normal_playback) ::monitor_start( emu, m_path );
}
void input_gep::monitor_update()
{
if (is_normal_playback) ::monitor_update( emu );
else ::monitor_apply( emu );
}
void input_gep::monitor_stop()
{
monitoring = false;
if (is_normal_playback) ::monitor_stop( emu );
}
void input_gep::setup_effects( bool echo )
{
if ( effects_enable )
{
gme_t::equalizer_t eq;
// bass - logarithmic, 2 to 8194 Hz
double bass = double( 255 - effects_bass ) / 255;
eq.bass = std::pow( 2.0, bass * 13 ) + 2.0;
// treble - level from -108 to 0 to 5 dB
double treble = double( effects_treble - 128 ) / 128;
eq.treble = treble * ( ( treble > 0 ) ? 16.0 : 80.0 ) - 8.0;
emu->set_equalizer( eq );
if ( echo && effects_echo_depth > 0 )
{
if ( ! buffer ) buffer = new Simple_Effects_Buffer;
emu->set_buffer( buffer );
}
}
}
void input_gep::setup_effects_2()
{
if ( effects_enable && effects_echo_depth > 0 )
{
double depth = double( effects_echo_depth ) / 255;
Simple_Effects_Buffer::config_t & cfg = buffer->config();
cfg.stereo = 0.6 * depth;
cfg.echo = 0.30 * depth;
cfg.enabled = true;
cfg.surround = true;
buffer->apply_config();
}
}
void input_gep::open( service_ptr_t<file> p_filehint, const char * p_path, t_input_open_reason p_reason, abort_callback & p_abort )
{
if ( p_filehint.is_empty() )
{
filesystem::g_open( m_file, p_path, ( p_reason == input_open_info_write ) ? filesystem::open_mode_write_existing : filesystem::open_mode_read, p_abort );
}
else m_file = p_filehint;
m_stats = m_file->get_stats( p_abort );
m_path = p_path;
tag_song_ms = cfg_default_length;
tag_fade_ms = cfg_default_fade;
}
unsigned input_gep::get_subsong_count()
{
return 1;
}
t_uint32 input_gep::get_subsong( unsigned p_index )
{
return p_index;
}
t_filestats input_gep::get_file_stats( abort_callback & p_abort )
{
return m_stats;
}
void input_gep::decode_initialize( t_uint32 p_subsong, unsigned p_flags, abort_callback & p_abort )
{
played = 0;
no_infinite = !cfg_indefinite || ( p_flags & input_flag_no_looping );
is_normal_playback = !!( p_flags & input_flag_playback );
monitor_start();
subsong = p_subsong;
emu->start_track( subsong );
handle_warning();
if ( no_infinite )
{
/*song_len=MulDiv(tag_song_ms, sample_rate, 1000);
fade_len=MulDiv(tag_fade_ms, sample_rate, 1000);*/
//int fade_min = ( 512 * 8 * 1000 / 2 + sample_rate - 1 ) / sample_rate;
emu->set_fade( tag_song_ms, tag_fade_ms /*max( tag_fade_ms, fade_min )*/ );
}
stop_on_errors = !! ( p_flags & input_flag_testing_integrity );
first_block = true;
}
bool input_gep::decode_run( audio_chunk & p_chunk,abort_callback & p_abort )
{
p_abort.check();
if ( ! emu->track_ended() )
{
//if ( no_infinite && played >= song_len + fade_len ) return false;
if ( monitoring ) monitor_update();
sample_buffer.grow_size( 1024 );
blip_sample_t * buf = sample_buffer.get_ptr();
ERRCHK( emu->play( 1024, buf ) );
if ( stop_on_errors )
{
const char * s = emu->warning();
if ( s ) throw exception_io_data( s );
}
/*int d_start,d_end;
d_start = played;*/
played += 512;
/*d_end = played;
if ( no_infinite && song_len && d_end > song_len )
{
blip_sample_t * foo = buf;
for(int n = d_start; n < d_end; n++)
{
if ( n > song_len )
{
if ( n <= song_len + fade_len )
{
int bleh = song_len + fade_len - n;
*foo++ = MulDiv( *foo, bleh, fade_len );
*foo++ = MulDiv( *foo, bleh, fade_len );
}
else
{
*foo++ = 0;
*foo++ = 0;
}
}
}
}*/
p_chunk.set_data_fixedpoint( buf, 1024 * sizeof( blip_sample_t ), sample_rate, 2, sizeof( blip_sample_t ) * 8, audio_chunk::channel_config_stereo );
return true;
}
return false;
}
void input_gep::decode_seek( double p_seconds, abort_callback & p_abort )
{
first_block = true;
int now = int( audio_math::time_to_samples( p_seconds, sample_rate ) );
if ( now == played ) return;
else if ( now < played )
{
emu->start_track( subsong );
handle_warning();
if ( no_infinite ) emu->set_fade( tag_song_ms, tag_fade_ms );
played = 0;
}
/*sample_buffer.grow_size( 1024 );
blip_sample_t * buf = sample_buffer.get_ptr();*/
int played_ = played;
int ten_seconds = int( sample_rate ) * 10;
while ( now - played_ > ten_seconds )
{
p_abort.check();
ERRCHK( emu->skip( ten_seconds * 2 ) );
played_ += ten_seconds;
}
ERRCHK( emu->skip( ( now - played_ ) * 2 ) );
played = played_;
}
bool input_gep::decode_can_seek()
{
return true;
}
bool input_gep::decode_get_dynamic_info( file_info & p_out, double & p_timestamp_delta )
{
if ( first_block )
{
p_out.info_set_int( "samplerate", sample_rate );
p_timestamp_delta = 0.;
first_block = false;
return true;
}
return false;
}
bool input_gep::decode_get_dynamic_info_track( file_info & p_out, double & p_timestamp_delta )
{
return false;
}
void input_gep::decode_on_idle( abort_callback & p_abort )
{
}
void input_gep::retag_set_info( t_uint32 p_subsong, const file_info & p_info, abort_callback & p_abort )
{
throw exception_io_data();
}
void input_gep::retag_commit( abort_callback & p_abort )
{
throw exception_io_data();
}
bool input_gep::g_is_our_content_type( const char * p_content_type )
{
return false;
}