Compare commits
No commits in common. "19cb65625460a041106a32c80631eb19f1fafbb5" and "759a99edcfc467e3f2dc7e314b9623008d43698e" have entirely different histories.
19cb656254
...
759a99edcf
1
example-test-code/README.md
Normal file
1
example-test-code/README.md
Normal file
@ -0,0 +1 @@
|
||||
Some code here is from ChatGPT
|
171
example-test-code/audiotest.c
Normal file
171
example-test-code/audiotest.c
Normal file
@ -0,0 +1,171 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_audio.h>
|
||||
#include <stdio.h>
|
||||
/*
|
||||
* Audio INFO: audio related declarations and callback here.
|
||||
*/
|
||||
|
||||
const int MAX_RECORDING_SECONDS = 2;
|
||||
const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;
|
||||
|
||||
int num_devices = 0;
|
||||
int num_recdevices = 0;
|
||||
int num_playdevices = 0;
|
||||
|
||||
//Recording data buffer
|
||||
Uint8* gRecordingBuffer = NULL;
|
||||
|
||||
//Size of data buffer
|
||||
Uint32 gBufferByteSize = 0;
|
||||
|
||||
//Position in data buffer
|
||||
Uint32 gBufferBytePosition = 0;
|
||||
|
||||
void audioRecordingCallback(void* userdata, Uint8* stream, int len )
|
||||
{
|
||||
memcpy(&gRecordingBuffer[ gBufferBytePosition ], stream, len);
|
||||
gBufferBytePosition += len;
|
||||
printf("Recording Callback: %u\n", *stream);
|
||||
}
|
||||
|
||||
void audioPlaybackCallback(void* userdata, Uint8* stream, int len )
|
||||
{
|
||||
memcpy(stream, &gRecordingBuffer[ gBufferBytePosition ], len);
|
||||
gBufferBytePosition += len;
|
||||
printf("Playback Callback: %u\n", *stream);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
printf("SDL init failed: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
printf("SDL init success: %s\n", SDL_GetError());
|
||||
|
||||
while (1) {
|
||||
int go = 0;
|
||||
while (!go) {
|
||||
printf("q to quit, Enter to continue\n");
|
||||
char ch = getchar();
|
||||
switch (ch) {
|
||||
case 'q':
|
||||
goto end;
|
||||
break;
|
||||
case '\n':
|
||||
go = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_AudioDeviceID recordingDeviceId = 0;
|
||||
SDL_AudioDeviceID playbackDeviceId = 0;
|
||||
|
||||
SDL_AudioSpec gReceivedRecordingSpec;
|
||||
SDL_AudioSpec gReceivedPlaybackSpec;
|
||||
|
||||
SDL_AudioSpec desiredRecordingSpec;
|
||||
|
||||
SDL_zero(desiredRecordingSpec);
|
||||
desiredRecordingSpec.freq = 44100;
|
||||
desiredRecordingSpec.format = AUDIO_S16;
|
||||
desiredRecordingSpec.channels = 2;
|
||||
desiredRecordingSpec.samples = 4096;
|
||||
desiredRecordingSpec.callback = audioRecordingCallback;
|
||||
|
||||
SDL_AudioSpec desiredPlaybackSpec;
|
||||
SDL_zero(desiredPlaybackSpec);
|
||||
desiredPlaybackSpec.freq = 44100;
|
||||
desiredPlaybackSpec.format = AUDIO_S16; // AUDIO_F32;
|
||||
desiredPlaybackSpec.channels = 2;
|
||||
desiredPlaybackSpec.samples = 4096;
|
||||
desiredPlaybackSpec.callback = audioPlaybackCallback;
|
||||
|
||||
//Maximum position in data buffer for recording
|
||||
Uint32 gBufferByteMaxPosition = 0;
|
||||
|
||||
printf("Audio Driver: %s\n", SDL_GetCurrentAudioDriver());
|
||||
|
||||
num_recdevices = SDL_GetNumAudioDevices(1);
|
||||
if(num_recdevices < 1) {
|
||||
printf( "Unable to get audio capture device! SDL Error: %s\n", SDL_GetError() );
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Num audio recording devices: %i\n", num_recdevices);
|
||||
for(int i = 0; i < num_recdevices; ++i) {
|
||||
const char* deviceName = SDL_GetAudioDeviceName(i, 1);
|
||||
printf("REC: %d - %s\n", i, deviceName);
|
||||
}
|
||||
|
||||
num_playdevices = SDL_GetNumAudioDevices(0);
|
||||
if(num_playdevices < 1) {
|
||||
printf( "Unable to get audio playback device! SDL Error: %s\n", SDL_GetError() );
|
||||
return 0;
|
||||
}
|
||||
printf("Num audio playback devices: %i\n", num_playdevices);
|
||||
for(int i = 0; i < num_recdevices; ++i) {
|
||||
const char* deviceName = SDL_GetAudioDeviceName(i, 0);
|
||||
printf("PBK: %d - %s\n", i, deviceName);
|
||||
}
|
||||
|
||||
const char* device_name = SDL_GetAudioDeviceName(2, 1);
|
||||
recordingDeviceId = SDL_OpenAudioDevice(device_name, SDL_TRUE, &desiredRecordingSpec, &gReceivedRecordingSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||
if(recordingDeviceId == 0) {
|
||||
printf("Failed to open recording device! SDL Error: %s", SDL_GetError() );
|
||||
return 1;
|
||||
}
|
||||
int bytesPerSample = gReceivedRecordingSpec.channels * (SDL_AUDIO_BITSIZE(gReceivedRecordingSpec.format) / 8);
|
||||
int bytesPerSecond = gReceivedRecordingSpec.freq * bytesPerSample;
|
||||
gBufferByteSize = RECORDING_BUFFER_SECONDS * bytesPerSecond;
|
||||
gBufferByteMaxPosition = MAX_RECORDING_SECONDS * bytesPerSecond;
|
||||
printf("bytesPerSample %d, bytesPerSecond %d\n", bytesPerSample,bytesPerSecond);
|
||||
printf("gBufferByteSize %d, gBufferByteMaxPosition %d\n", gBufferByteSize,gBufferByteMaxPosition);
|
||||
|
||||
playbackDeviceId = SDL_OpenAudioDevice( NULL, 0, &desiredPlaybackSpec, &gReceivedPlaybackSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE );
|
||||
if(playbackDeviceId == 0) {
|
||||
printf("Failed to open playback device! SDL Error: %s", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
gRecordingBuffer = malloc(gBufferByteSize);
|
||||
memset(gRecordingBuffer, 0, gBufferByteSize);
|
||||
gBufferBytePosition = 0;
|
||||
|
||||
SDL_Delay(100);
|
||||
SDL_PauseAudioDevice( recordingDeviceId, SDL_FALSE );
|
||||
|
||||
while(1) {
|
||||
SDL_LockAudioDevice( recordingDeviceId );
|
||||
if(gBufferBytePosition > gBufferByteMaxPosition) {
|
||||
printf("end\n");
|
||||
SDL_UnlockAudioDevice( recordingDeviceId );
|
||||
SDL_PauseAudioDevice( recordingDeviceId, SDL_TRUE );
|
||||
break;
|
||||
//} else {
|
||||
// printf("bufferPos %d\n",gBufferBytePosition);
|
||||
}
|
||||
SDL_UnlockAudioDevice( recordingDeviceId );
|
||||
}
|
||||
|
||||
printf("playback\n");
|
||||
gBufferBytePosition = 0;
|
||||
SDL_PauseAudioDevice(playbackDeviceId, 0);
|
||||
while(1) {
|
||||
SDL_LockAudioDevice(playbackDeviceId);
|
||||
if(gBufferBytePosition > gBufferByteMaxPosition) {
|
||||
printf("end\n");
|
||||
SDL_UnlockAudioDevice(playbackDeviceId);
|
||||
SDL_PauseAudioDevice(playbackDeviceId, SDL_TRUE);
|
||||
break;
|
||||
}
|
||||
SDL_UnlockAudioDevice(playbackDeviceId);
|
||||
}
|
||||
|
||||
printf("Cleanup\n");
|
||||
free(gRecordingBuffer);
|
||||
SDL_CloseAudioDevice(recordingDeviceId);
|
||||
SDL_CloseAudioDevice(playbackDeviceId);
|
||||
}
|
||||
end:
|
||||
return 0;
|
||||
}
|
517
example-test-code/main.c.bak
Normal file
517
example-test-code/main.c.bak
Normal file
@ -0,0 +1,517 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_audio.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
const int MAX_RECORDING_DEVICES = 10;
|
||||
const int MAX_RECORDING_SECONDS = 1;
|
||||
const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;
|
||||
/*
|
||||
* Application
|
||||
*/
|
||||
int init();
|
||||
void destroy();
|
||||
int select_device(SDL_KeyboardEvent* key_event, int max_index);
|
||||
|
||||
typedef enum {
|
||||
SELECTING_DEVICE,
|
||||
RECORDING,
|
||||
RECORDED,
|
||||
PLAYBACK,
|
||||
} application_state;
|
||||
|
||||
typedef struct {
|
||||
application_state application_state;
|
||||
int device_index;
|
||||
int recording_device_id;
|
||||
int playback_device_id;
|
||||
} state;
|
||||
|
||||
void application_state_to_string(application_state state, char* string);
|
||||
/*
|
||||
* Visual
|
||||
*/
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Renderer* renderer = NULL;
|
||||
TTF_Font* global_font = NULL;
|
||||
|
||||
typedef struct {
|
||||
SDL_Texture* texture;
|
||||
int width;
|
||||
int height;
|
||||
} text_texture;
|
||||
|
||||
int text_texture_init(text_texture* text_texture);
|
||||
int text_texture_load(text_texture* text_texture, char* string);
|
||||
int text_texture_render(text_texture* text_texture, int x, int y);
|
||||
int text_texture_free(text_texture* text_texture);
|
||||
/*
|
||||
* Audio
|
||||
*/
|
||||
int num_devices = 0;
|
||||
|
||||
SDL_AudioSpec* recording_spec;
|
||||
SDL_AudioSpec* playback_spec;
|
||||
SDL_AudioSpec* received_recording_spec;
|
||||
SDL_AudioSpec* received_playback_spec;
|
||||
Uint8* recording_buffer = NULL;
|
||||
unsigned int recording_buffer_size = 0;
|
||||
unsigned int recording_buffer_position = 0;
|
||||
unsigned int recording_buffer_position_max = 0;
|
||||
|
||||
SDL_AudioDeviceID audio_recording_init(int index, void* userdata);
|
||||
SDL_AudioDeviceID audio_playback_init(void* userdata);
|
||||
void audio_buffer_destroy();
|
||||
void audio_recording_callback( void* userdata, Uint8* stream, int len );
|
||||
void audio_playback_callback( void* userdata, Uint8* stream, int len );
|
||||
|
||||
void handle_input(SDL_Event* event, state* state);
|
||||
void handle_task(state* state);
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (!init()) return 1;
|
||||
|
||||
num_devices = SDL_GetNumAudioDevices(1);
|
||||
printf("Num audio devices: %i\n", num_devices);
|
||||
char char_buffer[64];
|
||||
char default_fmt[] = "Device selected: %i";
|
||||
|
||||
state state = {
|
||||
SELECTING_DEVICE,
|
||||
-1,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
text_texture display_text_texture;
|
||||
text_texture_init(&display_text_texture);
|
||||
sprintf(char_buffer, default_fmt, state.device_index);
|
||||
text_texture_load(&display_text_texture, char_buffer);
|
||||
|
||||
text_texture device_textures[num_devices];
|
||||
for (int i = 0; i < num_devices; i++) {
|
||||
text_texture_init(&device_textures[i]);
|
||||
sprintf(char_buffer, "%i: %s", i, SDL_GetAudioDeviceName(i, 1));
|
||||
text_texture_load(&device_textures[i], char_buffer);
|
||||
}
|
||||
|
||||
text_texture state_text_texture;
|
||||
text_texture_init(&state_text_texture);
|
||||
application_state_to_string(state.application_state, char_buffer);
|
||||
text_texture_load(&state_text_texture, char_buffer);
|
||||
|
||||
SDL_Event event;
|
||||
int running = 1;
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
SDL_Keycode keysym = event.key.keysym.sym;
|
||||
switch (event.type) {
|
||||
|
||||
case SDL_QUIT:
|
||||
running = 0;
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
handle_input(&event, &state);
|
||||
|
||||
sprintf(char_buffer, default_fmt, state.device_index);
|
||||
text_texture_load(&display_text_texture, char_buffer);
|
||||
application_state_to_string(state.application_state, char_buffer);
|
||||
text_texture_load(&state_text_texture, char_buffer);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
handle_task(&state);
|
||||
|
||||
// Clear the screen
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Render
|
||||
text_texture_render(&display_text_texture, 0, 0);
|
||||
|
||||
if (state.application_state == SELECTING_DEVICE) {
|
||||
int text_y_offset_step = display_text_texture.height * 2;
|
||||
int text_y_offset = text_y_offset_step;
|
||||
for (int i = 0; i < num_devices; i++) {
|
||||
text_texture_render(&device_textures[i], 0, text_y_offset);
|
||||
text_y_offset += text_y_offset_step;
|
||||
}
|
||||
}
|
||||
|
||||
text_texture_render(&state_text_texture, 0, SCREEN_HEIGHT - state_text_texture.height);
|
||||
|
||||
// Update the screen
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
text_texture_free(&display_text_texture);
|
||||
destroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_input(SDL_Event* event, state* state)
|
||||
{
|
||||
SDL_Keycode keysym = event->key.keysym.sym;
|
||||
switch (state->application_state) {
|
||||
|
||||
case SELECTING_DEVICE:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
case SDLK_RETURN:
|
||||
if (state->device_index == -1 || !state->recording_device_id) break;
|
||||
recording_buffer_position = 0;
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
||||
/*SDL_UnlockAudioDevice(state->recording_device_id);*/
|
||||
state->application_state = RECORDING;
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
state->device_index = -1;
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
break;
|
||||
}
|
||||
int device_index_new = select_device(&event->key, num_devices);
|
||||
if (device_index_new == -1) break;
|
||||
if (state->device_index != -1 && device_index_new == -1) break;
|
||||
state->device_index = device_index_new;
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
state->recording_device_id = audio_recording_init(state->device_index, NULL);
|
||||
break;
|
||||
|
||||
case RECORDING:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
||||
break;
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||
audio_buffer_destroy();
|
||||
state->application_state = SELECTING_DEVICE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RECORDED:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
case SDLK_RETURN:
|
||||
if (!state->playback_device_id) {
|
||||
state->playback_device_id = audio_playback_init(NULL);
|
||||
break;
|
||||
}
|
||||
recording_buffer_position = 0;
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 0);
|
||||
/*SDL_UnlockAudioDevice(state->playback_device_id);*/
|
||||
state->application_state = PLAYBACK;
|
||||
break;
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
audio_buffer_destroy();
|
||||
state->application_state = SELECTING_DEVICE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case PLAYBACK:
|
||||
switch (keysym) {
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void handle_task(state* state)
|
||||
{
|
||||
switch (state->application_state) {
|
||||
|
||||
case SELECTING_DEVICE:
|
||||
break;
|
||||
|
||||
case RECORDING:
|
||||
SDL_LockAudioDevice(state->recording_device_id);
|
||||
/*printf("buf pos: %u\n", recording_buffer_position);*/
|
||||
if (recording_buffer_position >= recording_buffer_position_max) {
|
||||
SDL_UnlockAudioDevice(state->recording_device_id);
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
SDL_UnlockAudioDevice(state->recording_device_id);
|
||||
break;
|
||||
|
||||
case RECORDED:
|
||||
break;
|
||||
|
||||
case PLAYBACK:
|
||||
SDL_LockAudioDevice(state->playback_device_id);
|
||||
if (recording_buffer_position >= recording_buffer_position_max) {
|
||||
SDL_UnlockAudioDevice(state->playback_device_id);
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 1);
|
||||
SDL_CloseAudioDevice(state->playback_device_id);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
SDL_UnlockAudioDevice(state->playback_device_id);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int init()
|
||||
{
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (TTF_Init() == -1) {
|
||||
printf("TTF could not initialize! TTF Error: %s\n", TTF_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow("SDL record", SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
|
||||
if (!window) {
|
||||
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
global_font = TTF_OpenFont("FiraCode-Regular.ttf", 24);
|
||||
if (!global_font) {
|
||||
printf("Failed to load font! TTF_Error: %s\n", TTF_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
recording_spec = malloc(sizeof(SDL_AudioSpec));
|
||||
received_recording_spec = malloc(sizeof(SDL_AudioSpec));
|
||||
playback_spec = malloc(sizeof(SDL_AudioSpec));
|
||||
received_playback_spec = malloc(sizeof(SDL_AudioSpec));
|
||||
return 1;
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
free(recording_spec);
|
||||
free(received_recording_spec);
|
||||
free(playback_spec);
|
||||
free(received_playback_spec);
|
||||
free(recording_buffer);
|
||||
|
||||
TTF_CloseFont(global_font);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int select_device(SDL_KeyboardEvent* key_event, int max_index)
|
||||
{
|
||||
SDL_Keycode keycode = key_event->keysym.sym;
|
||||
SDL_Keycode mod = key_event->keysym.mod;
|
||||
|
||||
int keycode_is_num = (keycode >= SDLK_0 && keycode <= SDLK_9);
|
||||
int no_mod = (mod == 0);
|
||||
if (!keycode_is_num || !no_mod) {
|
||||
printf("Not number: %c <%u>\n", keycode, keycode);
|
||||
return -1;
|
||||
} else {
|
||||
printf("Is number: %c <%u>\n", keycode, keycode);
|
||||
}
|
||||
|
||||
int index = keycode - SDLK_0;
|
||||
if (index >= max_index) return -1;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void application_state_to_string(application_state state, char* string)
|
||||
{
|
||||
switch (state) {
|
||||
case SELECTING_DEVICE:
|
||||
sprintf(string, "Selecting Device [0-9] -> [y/RET], ");
|
||||
break;
|
||||
case RECORDING:
|
||||
sprintf(string, "Recording");
|
||||
break;
|
||||
case RECORDED:
|
||||
sprintf(string, "Recorded");
|
||||
break;
|
||||
case PLAYBACK:
|
||||
sprintf(string, "Playing");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int text_texture_init(text_texture* text_texture)
|
||||
{
|
||||
text_texture->texture = NULL;
|
||||
text_texture->width = 0;
|
||||
text_texture->height = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_load(text_texture* text_texture, char* string)
|
||||
{
|
||||
if (text_texture->texture) {
|
||||
SDL_DestroyTexture(text_texture->texture);
|
||||
text_texture->texture = NULL;
|
||||
}
|
||||
|
||||
SDL_Color text_color = { 255, 255, 255 };
|
||||
SDL_Surface* text_surface = TTF_RenderText_Solid(global_font, string, text_color);
|
||||
if (!text_surface) {
|
||||
printf("Unable to render text surface! TTF_Error: %s\n", TTF_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, text_surface);
|
||||
if (!texture) {
|
||||
printf("Unable to create texture from rendered text! SDL_Error: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
text_texture->texture = SDL_CreateTextureFromSurface(renderer, text_surface);
|
||||
text_texture->width = text_surface->w;
|
||||
text_texture->height = text_surface->h;
|
||||
|
||||
SDL_FreeSurface(text_surface);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_render(text_texture* text_texture, int x, int y)
|
||||
{
|
||||
if (!text_texture) {
|
||||
printf("text_texture uninitialized");
|
||||
return 0;
|
||||
}
|
||||
if (!text_texture->texture) {
|
||||
printf("text_texture->texture uninitialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int quad_width = text_texture->width;
|
||||
int quad_height = text_texture->height;
|
||||
SDL_Rect render_quad = { x, y, quad_width, quad_height };
|
||||
SDL_RenderCopy(renderer, text_texture->texture, NULL, &render_quad);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_free(text_texture* text_texture)
|
||||
{
|
||||
if (!text_texture->texture) {
|
||||
return 0;
|
||||
}
|
||||
SDL_DestroyTexture(text_texture->texture);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_AudioDeviceID audio_recording_init(int index, void* userdata)
|
||||
{
|
||||
memset(recording_spec, 0, sizeof(SDL_AudioSpec));
|
||||
memset(received_recording_spec, 0, sizeof(SDL_AudioSpec));
|
||||
recording_spec->freq = 44100;
|
||||
recording_spec->format = AUDIO_F32;
|
||||
recording_spec->channels = 2;
|
||||
recording_spec->samples = 4096;
|
||||
recording_spec->callback = audio_recording_callback;
|
||||
recording_spec->userdata = userdata;
|
||||
|
||||
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(index, 1), 1, recording_spec, received_recording_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||
printf("Recording Device: %s\n", SDL_GetAudioDeviceName(index, 1));
|
||||
if (!device_id) {
|
||||
printf("Failed to open recording device! SDL Error: %s", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int bytes_per_sample = received_recording_spec->channels * (SDL_AUDIO_BITSIZE(received_recording_spec->format)/8);
|
||||
unsigned int bytes_per_second = received_recording_spec->freq * bytes_per_sample;
|
||||
recording_buffer_size = RECORDING_BUFFER_SECONDS * bytes_per_second;
|
||||
recording_buffer_position_max = MAX_RECORDING_SECONDS * bytes_per_second;
|
||||
|
||||
recording_buffer = malloc(recording_buffer_size);
|
||||
memset(recording_buffer, 0, recording_buffer_size);
|
||||
|
||||
return device_id;
|
||||
}
|
||||
|
||||
SDL_AudioDeviceID audio_playback_init(void* userdata)
|
||||
{
|
||||
if (!recording_buffer) {
|
||||
printf("Audio buffer not initialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(playback_spec, 0, sizeof(SDL_AudioSpec));
|
||||
memset(received_playback_spec, 0, sizeof(SDL_AudioSpec));
|
||||
playback_spec->freq = 44100;
|
||||
playback_spec->format = AUDIO_F32;
|
||||
playback_spec->channels = 2;
|
||||
playback_spec->samples = 4096;
|
||||
playback_spec->callback = audio_playback_callback;
|
||||
playback_spec->callback = userdata;
|
||||
|
||||
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, playback_spec, received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||
if (!device_id) {
|
||||
printf("Failed to open playback device! SDL Error: %s", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return device_id;
|
||||
}
|
||||
|
||||
int audio_device_stop(int device_id)
|
||||
{
|
||||
SDL_PauseAudioDevice(device_id, 1);
|
||||
SDL_CloseAudioDevice(device_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void audio_buffer_destroy()
|
||||
{
|
||||
free(recording_buffer);
|
||||
recording_buffer = NULL;
|
||||
recording_buffer_size = 0;
|
||||
recording_buffer_position = 0;
|
||||
}
|
||||
|
||||
void audio_recording_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
memcpy(&recording_buffer[recording_buffer_position], stream, len);
|
||||
recording_buffer_position += len;
|
||||
char string[64];
|
||||
sprintf(string, "Record pos: %u %u", recording_buffer_position, len);
|
||||
log_message(LOG_INFO, string);
|
||||
}
|
||||
|
||||
void audio_playback_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
memcpy(stream, &recording_buffer[recording_buffer_position], len);
|
||||
recording_buffer_position += len;
|
||||
char string[64];
|
||||
sprintf(string, "Playback pos: %u %u", recording_buffer_position, len);
|
||||
log_message(LOG_INFO, string);
|
||||
}
|
64
example-test-code/main.c.bak1
Normal file
64
example-test-code/main.c.bak1
Normal file
@ -0,0 +1,64 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
SDL_AudioSpec* desiredSpec;
|
||||
void audio_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
// Fill the audio buffer with data
|
||||
char string[64];
|
||||
sprintf(string, "stream: %u len: %u\n", stream, len);
|
||||
printf(string, "stream: %u len: %u\n", stream, len);
|
||||
log_message(LOG_INFO, string);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
desiredSpec = malloc(sizeof(SDL_AudioSpec));
|
||||
memset(desiredSpec, 0, sizeof(SDL_AudioSpec));
|
||||
desiredSpec->freq = 44100; // Sample rate
|
||||
desiredSpec->format = AUDIO_F32; // Audio format
|
||||
desiredSpec->channels = 2;
|
||||
desiredSpec->samples = 4096; // Buffer size
|
||||
desiredSpec->callback = audio_callback; // Set the callback
|
||||
|
||||
SDL_AudioDeviceID audioDevice = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(2, 1), 1, desiredSpec, NULL, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||
if (SDL_GetError()[0] != '\0') {
|
||||
printf("SDL Error : %s\n", SDL_GetError());
|
||||
}
|
||||
printf("Name: %s\n", SDL_GetAudioDeviceName(2, 1));
|
||||
if (audioDevice == 0) {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
// Start audio playback
|
||||
SDL_PauseAudioDevice(audioDevice, 0);
|
||||
if (SDL_GetError()[0] != '\0') {
|
||||
printf("SDL Error : %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_Event event;
|
||||
int running = 1;
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
SDL_Keycode keysym = event.key.keysym.sym;
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
running = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_CloseAudioDevice(audioDevice);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
98
example-test-code/main.c.bak2
Normal file
98
example-test-code/main.c.bak2
Normal file
@ -0,0 +1,98 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
Uint8* data;
|
||||
int current_pos;
|
||||
int max_length;
|
||||
} BufferType;
|
||||
|
||||
SDL_AudioDeviceID dev;
|
||||
|
||||
void audio_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
BufferType* buffer = (BufferType*)userdata;
|
||||
|
||||
printf("callback: %u %u\n", *stream, len);
|
||||
int space_left = buffer->max_length - buffer->current_pos;
|
||||
if (space_left <= 0) {
|
||||
// Buffer is full; stop the device
|
||||
SDL_PauseAudioDevice(dev, 1);
|
||||
printf("Buffer full, stopping recording.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (len > space_left) {
|
||||
len = space_left;
|
||||
}
|
||||
|
||||
memcpy(buffer->data + buffer->current_pos, stream, len);
|
||||
buffer->current_pos += len;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
|
||||
fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
BufferType audio_buffer;
|
||||
audio_buffer.max_length = 44100 * 5 * sizeof(float) * 2; // 5 seconds of stereo float audio
|
||||
audio_buffer.data = (Uint8*)malloc(audio_buffer.max_length);
|
||||
if (audio_buffer.data == NULL) {
|
||||
fprintf(stderr, "Failed to allocate audio buffer\n");
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
audio_buffer.current_pos = 0;
|
||||
|
||||
SDL_AudioSpec desired_spec, obtained_spec;
|
||||
SDL_zero(desired_spec);
|
||||
desired_spec.freq = 44100;
|
||||
desired_spec.format = AUDIO_F32SYS;
|
||||
desired_spec.channels = 2;
|
||||
desired_spec.samples = 512;
|
||||
desired_spec.callback = audio_callback;
|
||||
desired_spec.userdata = &audio_buffer;
|
||||
|
||||
dev = SDL_OpenAudioDevice(NULL, 1, &desired_spec, &obtained_spec, 0);
|
||||
if (dev == 0) {
|
||||
fprintf(stderr, "Failed to open audio device: %s\n", SDL_GetError());
|
||||
free(audio_buffer.data);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Audio device opened successfully.\n");
|
||||
printf("Obtained Spec:\n");
|
||||
printf(" Frequency: %d\n", obtained_spec.freq);
|
||||
printf(" Format: 0x%X\n", obtained_spec.format);
|
||||
printf(" Channels: %d\n", obtained_spec.channels);
|
||||
printf(" Samples: %d\n", obtained_spec.samples);
|
||||
|
||||
SDL_PauseAudioDevice(dev, 0); // Start recording
|
||||
printf("Recording started.\n");
|
||||
|
||||
SDL_Event event;
|
||||
while (audio_buffer.current_pos < audio_buffer.max_length) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
printf("Quit event received.\n");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
SDL_Delay(100);
|
||||
}
|
||||
|
||||
printf("Recording complete. Total bytes recorded: %d\n", audio_buffer.current_pos);
|
||||
|
||||
cleanup:
|
||||
SDL_PauseAudioDevice(dev, 1); // Stop recording
|
||||
SDL_CloseAudioDevice(dev);
|
||||
free(audio_buffer.data);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
85
example-test-code/temp.cpp
Normal file
85
example-test-code/temp.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_audio.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
SDL_AudioSpec want, have;
|
||||
SDL_AudioDeviceID player;
|
||||
|
||||
void SDLCALL callback_player( void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
|
||||
for (int i = 0; i < len; i ++) {
|
||||
auto value = Uint8(sin(i) * 1000);
|
||||
stream[i] = value ;
|
||||
}
|
||||
}
|
||||
|
||||
auto setupAudioPlayer()
|
||||
{
|
||||
|
||||
printf("### Device Player ###\n");
|
||||
int num = SDL_GetNumAudioDevices(false);
|
||||
printf("num: %d\n", num);
|
||||
for (int i = 0; i < num; i++) {
|
||||
auto name = SDL_GetAudioDeviceName(i, false);
|
||||
printf("%d: %s\n", i, name);
|
||||
}
|
||||
|
||||
int deviceIndex;
|
||||
printf("select device to player audio: ");
|
||||
scanf("%d", &deviceIndex);
|
||||
printf("\n");
|
||||
|
||||
SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
|
||||
want.freq = 44100;
|
||||
want.format = AUDIO_F32;
|
||||
want.channels = 2;
|
||||
want.samples = 1024;
|
||||
want.callback = callback_player; // you wrote this function elsewhere.
|
||||
//want.size = 512;
|
||||
|
||||
SDL_AudioDeviceID player = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(deviceIndex, false), SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||
if (player == 0) {
|
||||
printf("Failed to open player device! SDL Error: %s", SDL_GetError());
|
||||
|
||||
} else {
|
||||
|
||||
printf("freq %d \nformat %d \nchannels %d \nsamples %d\n", have.freq, have.format, have.channels, have.samples);
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
void destroyAudio()
|
||||
{
|
||||
|
||||
SDL_CloseAudioDevice(player);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int main(int n, char** args)
|
||||
{
|
||||
|
||||
int err = SDL_Init(SDL_INIT_AUDIO);
|
||||
printf("error: %d", err);
|
||||
if (err < 0) {
|
||||
printf("erorr: %d\n", err);
|
||||
printf("Failed to init audio! SDL Error: %s", SDL_GetError());
|
||||
}
|
||||
//recorder = setupAudioRecorder();
|
||||
auto player = setupAudioPlayer();
|
||||
printf("player: %d\n", player);
|
||||
//loadWave();
|
||||
|
||||
printf("playing\n");
|
||||
//SDL_PauseAudioDevice(recorder, 0);
|
||||
SDL_PauseAudioDevice(player, 0);
|
||||
//SDL_SemWait(sem);
|
||||
SDL_Delay(10000);
|
||||
destroyAudio();
|
||||
//system("pause");
|
||||
return 0;
|
||||
}
|
116
main.c
116
main.c
@ -2,9 +2,6 @@
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <semaphore.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "texture.h"
|
||||
#include "audio.h"
|
||||
#include "net.h"
|
||||
@ -12,18 +9,15 @@
|
||||
|
||||
#define SCREEN_WIDTH 640
|
||||
#define SCREEN_HEIGHT 480
|
||||
#define TEXT_LINES 20
|
||||
#define BODY_TEXT_LINES TEXT_LINES-2
|
||||
#define FONT_SIZE SCREEN_HEIGHT/TEXT_LINES
|
||||
|
||||
#define PORT 55555
|
||||
|
||||
typedef enum {
|
||||
SOCKET_LISTEN,
|
||||
SELECTING_DEVICE,
|
||||
RECORDING,
|
||||
RECORDED,
|
||||
PLAYBACK,
|
||||
SOCKET,
|
||||
} application_state;
|
||||
|
||||
typedef struct {
|
||||
@ -31,7 +25,6 @@ typedef struct {
|
||||
audio_state audio;
|
||||
int device_index;
|
||||
int update_ui;
|
||||
sem_t sem;
|
||||
} state;
|
||||
|
||||
int init();
|
||||
@ -39,8 +32,6 @@ void destroy();
|
||||
int select_device(SDL_KeyboardEvent* key_event, int max_index);
|
||||
void application_state_to_string(application_state state, char* string);
|
||||
|
||||
void* peer_discovery_t(void* args);
|
||||
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Renderer* renderer = NULL;
|
||||
|
||||
@ -48,7 +39,6 @@ int num_devices = 0;
|
||||
|
||||
int handle_input(SDL_Event* event, state* state);
|
||||
void handle_task(state* state);
|
||||
void render_body_text(text_texture* textures, int len);
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (!init()) return 1;
|
||||
@ -56,43 +46,33 @@ int main(int argc, char* argv[])
|
||||
num_devices = SDL_GetNumAudioDevices(1);
|
||||
log_message(LOG_INFO, "Num audio devices: %i", num_devices);
|
||||
char char_buffer[64];
|
||||
char device_selected_fmt[] = "Device selected: %i";
|
||||
char default_fmt[] = "Device selected: %i";
|
||||
|
||||
state state;
|
||||
//state.application_state = SOCKET_LISTEN;
|
||||
state.application_state = SELECTING_DEVICE;
|
||||
state.application_state = SOCKET;
|
||||
if (!audio_init(&state.audio)) return 1;
|
||||
state.device_index = -1;
|
||||
state.update_ui = 0;
|
||||
sem_init(&state.sem, 0, 1);
|
||||
|
||||
text_texture display_text_texture;
|
||||
text_texture_init(&display_text_texture, renderer, FONT_SIZE);
|
||||
sprintf(char_buffer, device_selected_fmt, state.device_index);
|
||||
text_texture_init(&display_text_texture, renderer);
|
||||
sprintf(char_buffer, default_fmt, state.device_index);
|
||||
text_texture_load(&display_text_texture, char_buffer);
|
||||
|
||||
text_texture device_textures[num_devices];
|
||||
for (int i = 0; i < num_devices; i++) {
|
||||
text_texture_init(&device_textures[i], renderer);
|
||||
sprintf(char_buffer, "%i: %s", i, SDL_GetAudioDeviceName(i, 1));
|
||||
text_texture_load(&device_textures[i], char_buffer);
|
||||
}
|
||||
|
||||
text_texture state_text_texture;
|
||||
text_texture_init(&state_text_texture, renderer, FONT_SIZE);
|
||||
text_texture_init(&state_text_texture, renderer);
|
||||
application_state_to_string(state.application_state, char_buffer);
|
||||
text_texture_load(&state_text_texture, char_buffer);
|
||||
|
||||
text_texture_frame socket_frame;
|
||||
text_texture_frame_init(&socket_frame, BODY_TEXT_LINES, renderer, FONT_SIZE);
|
||||
|
||||
text_texture_frame select_device_frame;
|
||||
text_texture_frame_init(&select_device_frame, num_devices, renderer, FONT_SIZE);
|
||||
|
||||
for (int i = 0; i < select_device_frame.len; i++) {
|
||||
sprintf(char_buffer, "%i: %s", i, SDL_GetAudioDeviceName(i, 1));
|
||||
text_texture_load(&select_device_frame.textures[i], char_buffer);
|
||||
}
|
||||
|
||||
pthread_t peer_discovery_thread;
|
||||
pthread_create(&peer_discovery_thread, NULL, peer_discovery_t, &state);
|
||||
|
||||
SDL_Event event;
|
||||
while (1) {
|
||||
sem_wait(&state.sem);
|
||||
while (SDL_PollEvent(&event)) {
|
||||
SDL_Keycode keysym = event.key.keysym.sym;
|
||||
switch (event.type) {
|
||||
@ -111,7 +91,7 @@ int main(int argc, char* argv[])
|
||||
handle_task(&state);
|
||||
|
||||
if (state.update_ui) {
|
||||
sprintf(char_buffer, device_selected_fmt, state.device_index);
|
||||
sprintf(char_buffer, default_fmt, state.device_index);
|
||||
text_texture_load(&display_text_texture, char_buffer);
|
||||
application_state_to_string(state.application_state, char_buffer);
|
||||
text_texture_load(&state_text_texture, char_buffer);
|
||||
@ -126,29 +106,29 @@ int main(int argc, char* argv[])
|
||||
// Render
|
||||
text_texture_render(&display_text_texture, 0, 0);
|
||||
|
||||
if (state.application_state == SOCKET_LISTEN) {
|
||||
text_texture_frame_render(&socket_frame, 0, FONT_SIZE);
|
||||
}
|
||||
|
||||
if (state.application_state == SELECTING_DEVICE) {
|
||||
text_texture_frame_render(&select_device_frame, 0, FONT_SIZE);
|
||||
int text_y_offset_step = display_text_texture.height * 2;
|
||||
int text_y_offset = text_y_offset_step;
|
||||
for (int i = 0; i < num_devices; i++) {
|
||||
text_texture_render(&device_textures[i], 0, text_y_offset);
|
||||
text_y_offset += text_y_offset_step;
|
||||
}
|
||||
}
|
||||
|
||||
text_texture_render(&state_text_texture, 0, SCREEN_HEIGHT - state_text_texture.height);
|
||||
|
||||
// Update the screen
|
||||
SDL_RenderPresent(renderer);
|
||||
sem_post(&state.sem);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
log_message(LOG_INFO, "Cleanup start");
|
||||
audio_destroy(&state.audio);
|
||||
log_message(LOG_INFO, "Audio done");
|
||||
text_texture_destroy(&display_text_texture);
|
||||
text_texture_destroy(&state_text_texture);
|
||||
text_texture_frame_destroy(&socket_frame);
|
||||
text_texture_frame_destroy(&select_device_frame);
|
||||
text_texture_free(&display_text_texture);
|
||||
for (int i = 0; i < num_devices; i++) {
|
||||
text_texture_free(&device_textures[i]);
|
||||
}
|
||||
log_message(LOG_INFO, "Texture done");
|
||||
destroy();
|
||||
log_message(LOG_INFO, "Done");
|
||||
@ -160,18 +140,6 @@ int handle_input(SDL_Event* event, state* state)
|
||||
SDL_Keycode keysym = event->key.keysym.sym;
|
||||
switch (state->application_state) {
|
||||
|
||||
case SOCKET_LISTEN:
|
||||
switch (keysym) {
|
||||
case SDLK_1:
|
||||
log_message(LOG_INFO, "Socket button 1 pressed");
|
||||
net_broadcast();
|
||||
break;
|
||||
case SDLK_2:
|
||||
log_message(LOG_INFO, "Socket button 2 pressed");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case SELECTING_DEVICE:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
@ -195,6 +163,7 @@ int handle_input(SDL_Event* event, state* state)
|
||||
audio_device_close(&state->audio, 1);
|
||||
audio_recording_init(&state->audio, state->device_index);
|
||||
state->update_ui = 1;
|
||||
|
||||
break;
|
||||
|
||||
case RECORDING:
|
||||
@ -238,6 +207,17 @@ int handle_input(SDL_Event* event, state* state)
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCKET:
|
||||
switch (keysym) {
|
||||
case SDLK_1:
|
||||
log_message(LOG_INFO, "Socket button 1 pressed");
|
||||
net_broadcast();
|
||||
break;
|
||||
case SDLK_2:
|
||||
log_message(LOG_INFO, "Socket button 2 pressed");
|
||||
net_listen_clients();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@ -246,9 +226,6 @@ void handle_task(state* state)
|
||||
{
|
||||
switch (state->application_state) {
|
||||
|
||||
case SOCKET_LISTEN:
|
||||
break;
|
||||
|
||||
case SELECTING_DEVICE:
|
||||
break;
|
||||
|
||||
@ -269,6 +246,8 @@ void handle_task(state* state)
|
||||
}
|
||||
break;
|
||||
|
||||
case SOCKET:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -339,11 +318,8 @@ int select_device(SDL_KeyboardEvent* key_event, int max_index)
|
||||
void application_state_to_string(application_state state, char* string)
|
||||
{
|
||||
switch (state) {
|
||||
case SOCKET_LISTEN:
|
||||
sprintf(string, "Socket");
|
||||
break;
|
||||
case SELECTING_DEVICE:
|
||||
sprintf(string, "Selecting Device [0-9] -> [y/RET]");
|
||||
sprintf(string, "Selecting Device [0-9] -> [y/RET], ");
|
||||
break;
|
||||
case RECORDING:
|
||||
sprintf(string, "Recording [ESC]: CANCEL");
|
||||
@ -354,16 +330,8 @@ void application_state_to_string(application_state state, char* string)
|
||||
case PLAYBACK:
|
||||
sprintf(string, "Playing [ESC]: CANCEL");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void* peer_discovery_t(void* args)
|
||||
{
|
||||
state* state = args;
|
||||
while (1) {
|
||||
sem_wait(&state->sem);
|
||||
net_listen_clients();
|
||||
sem_post(&state->sem);
|
||||
sleep(1);
|
||||
case SOCKET:
|
||||
sprintf(string, "Socket");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
40
texture.c
40
texture.c
@ -1,11 +1,11 @@
|
||||
#include "texture.h"
|
||||
#include "log.h"
|
||||
|
||||
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer, int font_size)
|
||||
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer)
|
||||
{
|
||||
text_texture->texture = NULL;
|
||||
text_texture->renderer = renderer;
|
||||
text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", font_size);
|
||||
text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", 24);
|
||||
if (!text_texture->font) {
|
||||
log_message(LOG_ERROR, "Failed to load font! TTF_Error: %s", TTF_GetError());
|
||||
return 0;
|
||||
@ -23,7 +23,6 @@ int text_texture_load(text_texture* text_texture, char* string)
|
||||
}
|
||||
|
||||
SDL_Color text_color = { 255, 255, 255 };
|
||||
if (strlen(string) == 0) string = " ";
|
||||
SDL_Surface* text_surface = TTF_RenderText_Solid(text_texture->font, string, text_color);
|
||||
if (!text_surface) {
|
||||
log_message(LOG_ERROR, "Unable to render text surface! TTF_Error: %s", TTF_GetError());
|
||||
@ -36,7 +35,7 @@ int text_texture_load(text_texture* text_texture, char* string)
|
||||
return 0;
|
||||
}
|
||||
|
||||
text_texture->texture = texture;
|
||||
text_texture->texture = SDL_CreateTextureFromSurface(text_texture->renderer, text_surface);
|
||||
text_texture->width = text_surface->w;
|
||||
text_texture->height = text_surface->h;
|
||||
|
||||
@ -63,42 +62,11 @@ int text_texture_render(text_texture* text_texture, int x, int y)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_destroy(text_texture* text_texture)
|
||||
int text_texture_free(text_texture* text_texture)
|
||||
{
|
||||
if (!text_texture->texture) {
|
||||
return 0;
|
||||
}
|
||||
SDL_DestroyTexture(text_texture->texture);
|
||||
TTF_CloseFont(text_texture->font);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_frame_init(text_texture_frame* text_texture_frame, int len, SDL_Renderer* renderer, int font_size)
|
||||
{
|
||||
text_texture_frame->textures = malloc(len*sizeof(text_texture));
|
||||
for (int i = 0; i < len; i++) {
|
||||
text_texture_init(&text_texture_frame->textures[i], renderer, font_size);
|
||||
}
|
||||
text_texture_frame->len = len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_frame_render(text_texture_frame* text_texture_frame, int x, int y)
|
||||
{
|
||||
int text_y_offset_step = TTF_FontHeight(text_texture_frame->textures[0].font);
|
||||
int text_y_offset = y;
|
||||
for (int i = 0; i < text_texture_frame->len; i++) {
|
||||
text_texture_render(&text_texture_frame->textures[i], x, text_y_offset);
|
||||
text_y_offset += text_y_offset_step;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_frame_destroy(text_texture_frame* text_texture_frame)
|
||||
{
|
||||
for (int i = 0; i < text_texture_frame->len; i++) {
|
||||
text_texture_destroy(&text_texture_frame->textures[i]);
|
||||
}
|
||||
free(text_texture_frame->textures);
|
||||
return 1;
|
||||
}
|
||||
|
13
texture.h
13
texture.h
@ -12,18 +12,9 @@ typedef struct {
|
||||
int height;
|
||||
} text_texture;
|
||||
|
||||
typedef struct {
|
||||
text_texture* textures;
|
||||
int len;
|
||||
} text_texture_frame;
|
||||
|
||||
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer, int font_size);
|
||||
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer);
|
||||
int text_texture_load(text_texture* text_texture, char* string);
|
||||
int text_texture_render(text_texture* text_texture, int x, int y);
|
||||
int text_texture_destroy(text_texture* text_texture);
|
||||
|
||||
int text_texture_frame_init(text_texture_frame* text_texture_frame, int len, SDL_Renderer* renderer, int font_size);
|
||||
int text_texture_frame_render(text_texture_frame* text_texture_frame, int x, int y);
|
||||
int text_texture_frame_destroy(text_texture_frame* text_texture_frame);
|
||||
int text_texture_free(text_texture* text_texture);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user