add source code
This commit is contained in:
		
							
								
								
									
										8
									
								
								CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
cmake_minimum_required(3.10)
 | 
			
		||||
project(sdl-pcm LANGUAGE C)
 | 
			
		||||
 | 
			
		||||
find_package(SDL2 REQUIRED)
 | 
			
		||||
 | 
			
		||||
add_executable(${PROJECT_NAME} main.c)
 | 
			
		||||
 | 
			
		||||
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
 | 
			
		||||
							
								
								
									
										45
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
#include <SDL2/SDL.h>
 | 
			
		||||
#include <SDL2/SDL_audio.h>
 | 
			
		||||
#include <SDL2/SDL_error.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#define PCM_BUF_SIZE (1024*2*2*2)
 | 
			
		||||
 | 
			
		||||
static Uint8 *s_audio_buf = NULL;
 | 
			
		||||
static Uint8 *s_audio_pos = NULL;
 | 
			
		||||
static Uint8 *s_audio_end = NULL;
 | 
			
		||||
 | 
			
		||||
void audo_pcm_callback(void *data, Uint8 *stream, int len) {
 | 
			
		||||
    SDL_memset(stream, 0, len);
 | 
			
		||||
 | 
			
		||||
    if (s_audio_pos >= s_audio_end) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int remain_buffer_len = s_audio_end - s_audio_pos;
 | 
			
		||||
    len = (len < remain_buffer_len) ? len : remain_buffer_len;
 | 
			
		||||
 | 
			
		||||
    SDL_MixAudio(stream, s_audio_pos, len, SDL_MIX_MAXVOLUME/8);
 | 
			
		||||
    s_audio_pos += len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#undef main
 | 
			
		||||
int main(int argc, char* argv[]) {
 | 
			
		||||
    int ret = -1;
 | 
			
		||||
    FILE *audio_fd = NULL;
 | 
			
		||||
    SDL_AudioSpec spec;
 | 
			
		||||
    const char *path = "44100_16bit_2ch.pcm";
 | 
			
		||||
    size_t read_buffer_len = 0;
 | 
			
		||||
 | 
			
		||||
    if (SDL_Init(SDL_INIT_AUDIO)) {
 | 
			
		||||
        fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed: %s\n", SDL_GetError());
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_fd = fopen(path, "rb");
 | 
			
		||||
    if (!audio_fd) {
 | 
			
		||||
        fprintf(stderr, "fopen(path, \"rb\")\n");
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user