2121void sgu1_init (sgu1_t * sgu , const sgu1_desc_t * desc ) {
2222 CHIPS_ASSERT (sgu && desc );
2323 CHIPS_ASSERT (desc -> tick_hz > 0 );
24- CHIPS_ASSERT (desc -> sound_hz > 0 );
2524 memset (sgu , 0 , sizeof (* sgu ));
26- sgu -> sound_hz = desc -> sound_hz ;
27- sgu -> sample_period = (desc -> tick_hz * SGU1_FIXEDPOINT_SCALE ) / desc -> sound_hz ;
28- sgu -> sample_counter = sgu -> sample_period ;
2925 sgu -> sample_mag = desc -> magnitude ;
3026 sgu -> tick_period = (desc -> tick_hz * SGU1_FIXEDPOINT_SCALE ) / SGU1_CHIP_CLOCK ;
3127 sgu -> tick_counter = sgu -> tick_period ;
3228 SoundUnit_Init (& sgu -> su , 65536 );
33- sgu -> resampler = speex_resampler_init (SGU1_AUDIO_CHANNELS , SGU1_CHIP_CLOCK , sgu -> sound_hz , 10 , nullptr );
3429}
3530
3631void sgu1_reset (sgu1_t * sgu ) {
3732 CHIPS_ASSERT (sgu );
3833 SoundUnit_Reset (& sgu -> su );
3934 memset (sgu -> reg , 0 , sizeof (sgu -> reg ));
4035 sgu -> tick_counter = sgu -> tick_period ;
41- sgu -> sample_counter = sgu -> sample_period ;
4236 sgu -> sample [0 ] = sgu -> sample [1 ] = 0.0f ;
4337 sgu -> pins = 0 ;
44- speex_resampler_reset_mem (sgu -> resampler );
4538}
4639
4740/* tick the sound generation, return true when new sample ready */
4841static uint64_t _sgu1_tick (sgu1_t * sgu , uint64_t pins ) {
42+ pins &= ~SGU1_SAMPLE ;
4943 /* next sample? */
5044 sgu -> tick_counter -= SGU1_FIXEDPOINT_SCALE ;
5145 while (sgu -> tick_counter <= 0 ) {
5246 sgu -> tick_counter += sgu -> tick_period ;
5347 int32_t l , r ;
5448 SoundUnit_NextSample (& sgu -> su , & l , & r );
55- float in [2 ] = { ((float )l / 32767.0f ), ((float )r / 32767.0f ) };
56- spx_uint32_t in_len = 1 ; // 1 stereo frame
57- spx_uint32_t out_len = 1 ; // room for 1 stereo frame
58- speex_resampler_process_interleaved_float (sgu -> resampler , in , & in_len , sgu -> sample , & out_len );
49+ sgu -> sample [0 ] = sgu -> sample_mag * (float )l / 32767.0f ;
50+ sgu -> sample [1 ] = sgu -> sample_mag * (float )r / 32767.0f ;
51+ pins |= SGU1_SAMPLE ;
5952
6053 for (uint8_t i = 0 ; i < SGU1_NUM_CHANNELS ; i ++ ) {
6154 sgu -> voice [i ].sample_buffer [sgu -> voice [i ].sample_pos ++ ] = (float )(SoundUnit_GetSample (& sgu -> su , i ));
@@ -64,16 +57,6 @@ static uint64_t _sgu1_tick(sgu1_t* sgu, uint64_t pins) {
6457 }
6558 }
6659 }
67-
68- /* new sample? */
69- pins &= ~SGU1_SAMPLE ;
70- sgu -> sample_counter -= SGU1_FIXEDPOINT_SCALE ;
71- while (sgu -> sample_counter <= 0 ) {
72- sgu -> sample_counter += sgu -> sample_period ;
73- sgu -> sample [0 ] = sgu -> sample_mag * sgu -> sample [0 ];
74- sgu -> sample [1 ] = sgu -> sample_mag * sgu -> sample [1 ];
75- pins |= SGU1_SAMPLE ;
76- }
7760 return pins ;
7861}
7962
0 commit comments