mirror of
				https://github.com/libsdl-org/SDL.git
				synced 2025-11-04 01:34:38 +00:00 
			
		
		
		
	Fixed sine wave distortion over time.
Audio distortion after a while caused by loss of precision in dividing a large floating point number resolved by keeping `current_sine_sample` (formelly named `total_samples_generated`) between 0 and freq - 1.
This commit is contained in:
		
				
					committed by
					
						
						Sam Lantinga
					
				
			
			
				
	
			
			
			
						parent
						
							010f27dc70
						
					
				
				
					commit
					b95989d14a
				
			@@ -14,7 +14,7 @@
 | 
			
		||||
static SDL_Window *window = NULL;
 | 
			
		||||
static SDL_Renderer *renderer = NULL;
 | 
			
		||||
static SDL_AudioStream *stream = NULL;
 | 
			
		||||
static int total_samples_generated = 0;
 | 
			
		||||
static int current_sine_sample = 0;
 | 
			
		||||
 | 
			
		||||
/* This function runs once at startup. */
 | 
			
		||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
 | 
			
		||||
@@ -76,11 +76,14 @@ SDL_AppResult SDL_AppIterate(void *appstate)
 | 
			
		||||
        /* generate a 440Hz pure tone */
 | 
			
		||||
        for (i = 0; i < SDL_arraysize(samples); i++) {
 | 
			
		||||
            const int freq = 440;
 | 
			
		||||
            const int phase = (total_samples_generated * freq) % 8000;
 | 
			
		||||
            samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0);
 | 
			
		||||
            total_samples_generated++;
 | 
			
		||||
            const int phase = current_sine_sample * freq / 8000.0f;
 | 
			
		||||
            samples[i] = SDL_sinf(phase * 2 * SDL_PI_F);
 | 
			
		||||
            current_sine_sample++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* wrapping around to avoid floating-point errors */
 | 
			
		||||
        current_sine_sample %= 8000;
 | 
			
		||||
 | 
			
		||||
        /* feed the new data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */
 | 
			
		||||
        SDL_PutAudioStreamData(stream, samples, sizeof (samples));
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@
 | 
			
		||||
static SDL_Window *window = NULL;
 | 
			
		||||
static SDL_Renderer *renderer = NULL;
 | 
			
		||||
static SDL_AudioStream *stream = NULL;
 | 
			
		||||
static int total_samples_generated = 0;
 | 
			
		||||
static int current_sine_sample = 0;
 | 
			
		||||
 | 
			
		||||
/* this function will be called (usually in a background thread) when the audio stream is consuming data. */
 | 
			
		||||
static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astream, int additional_amount, int total_amount)
 | 
			
		||||
@@ -36,11 +36,14 @@ static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astr
 | 
			
		||||
        /* generate a 440Hz pure tone */
 | 
			
		||||
        for (i = 0; i < total; i++) {
 | 
			
		||||
            const int freq = 440;
 | 
			
		||||
            const int phase = (total_samples_generated * freq) % 8000;
 | 
			
		||||
            samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0);
 | 
			
		||||
            total_samples_generated++;
 | 
			
		||||
            const int phase = current_sine_sample * freq / 8000.0f;
 | 
			
		||||
            samples[i] = SDL_sinf(phase * 2 * SDL_PI_F);
 | 
			
		||||
            current_sine_sample++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* wrapping around to avoid floating-point errors */
 | 
			
		||||
        current_sine_sample %= 8000;
 | 
			
		||||
 | 
			
		||||
        /* feed the new data to the stream. It will queue at the end, and trickle out as the hardware needs more data. */
 | 
			
		||||
        SDL_PutAudioStreamData(astream, samples, total * sizeof (float));
 | 
			
		||||
        additional_amount -= total;  /* subtract what we've just fed the stream. */
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user