using SDL_ConvertAudio instead manual sound samples conversion
This commit is contained in:
		
							
								
								
									
										59
									
								
								src/studio.c
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								src/studio.c
									
									
									
									
									
								
							@@ -104,8 +104,12 @@ static struct
 | 
				
			|||||||
	SDL_Renderer* renderer;
 | 
						SDL_Renderer* renderer;
 | 
				
			||||||
	SDL_Texture* texture;
 | 
						SDL_Texture* texture;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	SDL_AudioSpec audioSpec;
 | 
						struct
 | 
				
			||||||
	SDL_AudioDeviceID audioDevice;
 | 
						{
 | 
				
			||||||
 | 
							SDL_AudioSpec 		spec;
 | 
				
			||||||
 | 
							SDL_AudioDeviceID 	device;
 | 
				
			||||||
 | 
							SDL_AudioCVT 		cvt;
 | 
				
			||||||
 | 
						} audio;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	SDL_Joystick* joysticks[MAX_CONTROLLERS];
 | 
						SDL_Joystick* joysticks[MAX_CONTROLLERS];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -232,8 +236,6 @@ static struct
 | 
				
			|||||||
	s32 argc;
 | 
						s32 argc;
 | 
				
			||||||
	char **argv;
 | 
						char **argv;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	float* floatSamples;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
} studio =
 | 
					} studio =
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	.tic80local = NULL,
 | 
						.tic80local = NULL,
 | 
				
			||||||
@@ -242,7 +244,10 @@ static struct
 | 
				
			|||||||
	.window = NULL,
 | 
						.window = NULL,
 | 
				
			||||||
	.renderer = NULL,
 | 
						.renderer = NULL,
 | 
				
			||||||
	.texture = NULL,
 | 
						.texture = NULL,
 | 
				
			||||||
	.audioDevice = 0,
 | 
						.audio = 
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							.device = 0,
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	.cart = 
 | 
						.cart = 
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -319,7 +324,6 @@ static struct
 | 
				
			|||||||
	.quitFlag = false,
 | 
						.quitFlag = false,
 | 
				
			||||||
	.argc = 0,
 | 
						.argc = 0,
 | 
				
			||||||
	.argv = NULL,
 | 
						.argv = NULL,
 | 
				
			||||||
	.floatSamples = NULL,
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
tic_tiles* getBankTiles()
 | 
					tic_tiles* getBankTiles()
 | 
				
			||||||
@@ -2044,24 +2048,13 @@ static void transparentBlit(u32* out, s32 pitch)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static void blitSound()
 | 
					static void blitSound()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	s32 samples = studio.audioSpec.freq / TIC_FRAMERATE;
 | 
						if(studio.audio.cvt.needed)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// TODO: use SDL_ConvertAudio to covert format
 | 
					 | 
				
			||||||
	if(studio.audioSpec.format == AUDIO_F32)
 | 
					 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if(!studio.floatSamples)
 | 
							SDL_memcpy(studio.audio.cvt.buf, studio.tic->samples.buffer, studio.tic->samples.size);
 | 
				
			||||||
			studio.floatSamples = SDL_malloc(samples * sizeof studio.floatSamples[0]);
 | 
							SDL_ConvertAudio(&studio.audio.cvt);
 | 
				
			||||||
 | 
							SDL_QueueAudio(studio.audio.device, studio.audio.cvt.buf, studio.audio.cvt.len_cvt);
 | 
				
			||||||
		s16* ptr = studio.tic->samples.buffer;
 | 
					 | 
				
			||||||
		s16* end = ptr + samples;
 | 
					 | 
				
			||||||
		float* out = studio.floatSamples;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		while(ptr != end) *out++ = *ptr++ / 32767.0f;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		SDL_QueueAudio(studio.audioDevice, studio.floatSamples, samples * sizeof studio.floatSamples[0]);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else if (studio.audioSpec.format == AUDIO_S16)
 | 
						else SDL_QueueAudio(studio.audio.device, studio.tic->samples.buffer, studio.tic->samples.size);
 | 
				
			||||||
		SDL_QueueAudio(studio.audioDevice, studio.tic->samples.buffer, studio.tic->samples.size);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void drawRecordLabel(u32* frame, s32 pitch, s32 sx, s32 sy, const u32* color)
 | 
					static void drawRecordLabel(u32* frame, s32 pitch, s32 sx, s32 sy, const u32* color)
 | 
				
			||||||
@@ -2512,10 +2505,18 @@ static void initSound()
 | 
				
			|||||||
		.userdata = NULL,
 | 
							.userdata = NULL,
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	studio.audioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &studio.audioSpec, SDL_AUDIO_ALLOW_ANY_CHANGE);
 | 
						studio.audio.device = SDL_OpenAudioDevice(NULL, 0, &want, &studio.audio.spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if(studio.audioDevice)
 | 
						SDL_BuildAudioCVT(&studio.audio.cvt, want.format, want.channels, studio.audio.spec.freq, studio.audio.spec.format, studio.audio.spec.channels, studio.audio.spec.freq);
 | 
				
			||||||
		SDL_PauseAudioDevice(studio.audioDevice, 0);
 | 
					
 | 
				
			||||||
 | 
						if(studio.audio.cvt.needed)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							studio.audio.cvt.len = studio.audio.spec.freq * sizeof studio.tic->samples.buffer[0] / TIC_FRAMERATE;
 | 
				
			||||||
 | 
							studio.audio.cvt.buf = SDL_malloc(studio.audio.cvt.len * studio.audio.cvt.len_mult);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if(studio.audio.device)
 | 
				
			||||||
 | 
							SDL_PauseAudioDevice(studio.audio.device, 0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void initTouchGamepad()
 | 
					static void initTouchGamepad()
 | 
				
			||||||
@@ -2637,7 +2638,7 @@ static void onFSInitialized(FileSystem* fs)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	initSound();
 | 
						initSound();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	studio.tic80local = (tic80_local*)tic80_create(studio.audioSpec.freq);
 | 
						studio.tic80local = (tic80_local*)tic80_create(studio.audio.spec.freq);
 | 
				
			||||||
	studio.tic = studio.tic80local->memory;
 | 
						studio.tic = studio.tic80local->memory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -2775,8 +2776,8 @@ s32 main(s32 argc, char **argv)
 | 
				
			|||||||
	if(studio.tic80local)
 | 
						if(studio.tic80local)
 | 
				
			||||||
		tic80_delete((tic80*)studio.tic80local);
 | 
							tic80_delete((tic80*)studio.tic80local);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if(studio.floatSamples)
 | 
						if(studio.audio.cvt.buf)
 | 
				
			||||||
		SDL_free(studio.floatSamples);
 | 
							SDL_free(studio.audio.cvt.buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	SDL_DestroyTexture(studio.gamepad.texture);
 | 
						SDL_DestroyTexture(studio.gamepad.texture);
 | 
				
			||||||
	SDL_DestroyTexture(studio.texture);
 | 
						SDL_DestroyTexture(studio.texture);
 | 
				
			||||||
@@ -2789,7 +2790,7 @@ s32 main(s32 argc, char **argv)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#if !defined (__MACOSX__)
 | 
					#if !defined (__MACOSX__)
 | 
				
			||||||
	// stucks here on macos
 | 
						// stucks here on macos
 | 
				
			||||||
	SDL_CloseAudioDevice(studio.audioDevice);
 | 
						SDL_CloseAudioDevice(studio.audio.device);
 | 
				
			||||||
	SDL_Quit();
 | 
						SDL_Quit();
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user