Compare commits
20 Commits
0b8f5a5620
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 31f9ddfe9f | |||
| 78fc1bc43c | |||
| 797689ae27 | |||
| 351d600eba | |||
| 19cb656254 | |||
| 1d30dba681 | |||
| 759a99edcf | |||
| 9114ea6691 | |||
| 1b5e58d95a | |||
| ebc1a4e1ac | |||
| c7b35b705d | |||
| 9a5486c03c | |||
| f9cf93eaee | |||
| 988bc52e57 | |||
| 64195dd5a9 | |||
| 92d3e150cb | |||
| 9a0003059d | |||
| e4895439fa | |||
| 2ef08b6b1b | |||
| ba14ddaf04 |
@@ -1,5 +1,6 @@
|
|||||||
obj/
|
obj/
|
||||||
main
|
main
|
||||||
|
test
|
||||||
*.out
|
*.out
|
||||||
|
|
||||||
logfile.txt
|
logfile.txt
|
||||||
|
|||||||
@@ -0,0 +1,144 @@
|
|||||||
|
#include "audio.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
static SDL_AudioSpec recording_spec;
|
||||||
|
static SDL_AudioSpec playback_spec;
|
||||||
|
static SDL_AudioSpec received_recording_spec;
|
||||||
|
static SDL_AudioSpec received_playback_spec;
|
||||||
|
|
||||||
|
void audio_recording_callback(void* userdata, uint8_t* stream, int len);
|
||||||
|
void audio_playback_callback(void* userdata, uint8_t* stream, int len);
|
||||||
|
|
||||||
|
int audio_init(audio_state* state)
|
||||||
|
{
|
||||||
|
state->recording_device_id = 0;
|
||||||
|
state->playback_device_id = 0;
|
||||||
|
|
||||||
|
SDL_zero(recording_spec);
|
||||||
|
recording_spec.freq = 44100;
|
||||||
|
recording_spec.format = AUDIO_S32;
|
||||||
|
recording_spec.channels = 2;
|
||||||
|
recording_spec.samples = 4096;
|
||||||
|
recording_spec.callback = audio_recording_callback;
|
||||||
|
recording_spec.userdata = state;
|
||||||
|
|
||||||
|
SDL_zero(playback_spec);
|
||||||
|
playback_spec.freq = 44100;
|
||||||
|
playback_spec.format = AUDIO_S32;
|
||||||
|
playback_spec.channels = 2;
|
||||||
|
playback_spec.samples = 4096;
|
||||||
|
playback_spec.callback = audio_playback_callback;
|
||||||
|
playback_spec.userdata = state;
|
||||||
|
|
||||||
|
state->recording_buffer = NULL;
|
||||||
|
state->recording_buffer_size = 0;
|
||||||
|
state->recording_buffer_position = 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int audio_recording_init(audio_state* state, int index)
|
||||||
|
{
|
||||||
|
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(index, 1), 1, &recording_spec, &received_recording_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||||
|
log_message(LOG_INFO, "Recording Device: %s", SDL_GetAudioDeviceName(index, 1));
|
||||||
|
if (!device_id) {
|
||||||
|
log_message(LOG_INFO, "Failed to open recording device! SDL Error: %s", SDL_GetError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
state->recording_device_id = device_id;
|
||||||
|
|
||||||
|
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;
|
||||||
|
state->recording_buffer_size = MAX_RECORDING_SECONDS * bytes_per_second;
|
||||||
|
|
||||||
|
if (state->recording_buffer) free(state->recording_buffer);
|
||||||
|
state->recording_buffer = malloc(state->recording_buffer_size);
|
||||||
|
memset(state->recording_buffer, 0, state->recording_buffer_size);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_destroy(audio_state* state)
|
||||||
|
{
|
||||||
|
free(state->recording_buffer);
|
||||||
|
SDL_CloseAudioDevice(state->recording_device_id);
|
||||||
|
SDL_CloseAudioDevice(state->playback_device_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
int audio_playback_init(audio_state* state)
|
||||||
|
{
|
||||||
|
if (!state->recording_buffer) {
|
||||||
|
log_message(LOG_INFO, "Audio buffer not initialized");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, &playback_spec, &received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||||
|
if (!device_id) {
|
||||||
|
log_message(LOG_INFO, "Failed to open playback device! SDL Error: %s", SDL_GetError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
state->playback_device_id = device_id;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_device_pause(audio_state* state, int is_recording, int is_pause)
|
||||||
|
{
|
||||||
|
int device_id = is_recording ? state->recording_device_id : state->playback_device_id;
|
||||||
|
SDL_PauseAudioDevice(device_id, is_pause);
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_device_close(audio_state* state, int is_recording)
|
||||||
|
{
|
||||||
|
int device_id = is_recording ? state->recording_device_id : state->playback_device_id;
|
||||||
|
SDL_CloseAudioDevice(device_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_buffer_reset(audio_state* state)
|
||||||
|
{
|
||||||
|
memset(state->recording_buffer, 0, state->recording_buffer_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_recording_callback(void* userdata, uint8_t* stream, int len)
|
||||||
|
{
|
||||||
|
audio_state* state = userdata;
|
||||||
|
|
||||||
|
int space_left = state->recording_buffer_size - state->recording_buffer_position;
|
||||||
|
if (space_left <= 0) {
|
||||||
|
log_message(LOG_INFO, "Stopping recording ID: %i", state->recording_device_id);
|
||||||
|
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len > space_left) {
|
||||||
|
len = space_left;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(&state->recording_buffer[state->recording_buffer_position], stream, len);
|
||||||
|
state->recording_buffer_position += len;
|
||||||
|
|
||||||
|
log_message(LOG_INFO, "Record pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void audio_playback_callback(void* userdata, uint8_t* stream, int len)
|
||||||
|
{
|
||||||
|
audio_state* state = userdata;
|
||||||
|
|
||||||
|
int space_left = state->recording_buffer_size - state->recording_buffer_position;
|
||||||
|
if (space_left <= 0) {
|
||||||
|
log_message(LOG_INFO, "Stopping playback ID: %i", state->recording_device_id);
|
||||||
|
SDL_PauseAudioDevice(state->playback_device_id, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len > space_left) {
|
||||||
|
len = space_left;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(stream, &state->recording_buffer[state->recording_buffer_position], len);
|
||||||
|
state->recording_buffer_position += len;
|
||||||
|
|
||||||
|
log_message(LOG_INFO, "Playback pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len);
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef AUDIO_H
|
||||||
|
#define AUDIO_H
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define MAX_RECORDING_SECONDS 2
|
||||||
|
|
||||||
|
typedef struct audio_state {
|
||||||
|
int recording_device_id;
|
||||||
|
int playback_device_id;
|
||||||
|
uint8_t* recording_buffer;
|
||||||
|
unsigned int recording_buffer_size;
|
||||||
|
unsigned int recording_buffer_position;
|
||||||
|
} audio_state;
|
||||||
|
|
||||||
|
int audio_init(audio_state* state);
|
||||||
|
void audio_destroy(audio_state* state);
|
||||||
|
int audio_recording_init(audio_state* state, int index);
|
||||||
|
int audio_playback_init(audio_state* state);
|
||||||
|
void audio_device_pause(audio_state* state, int is_recording, int pause);
|
||||||
|
void audio_device_close(audio_state* state, int is_recording);
|
||||||
|
void audio_buffer_reset(audio_state* state);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1 +0,0 @@
|
|||||||
Some code here is from ChatGPT
|
|
||||||
@@ -1,517 +0,0 @@
|
|||||||
#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);
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
// Chat GPT code
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
const char* get_current_time()
|
const char* get_current_time()
|
||||||
{
|
{
|
||||||
@@ -10,9 +14,13 @@ const char* get_current_time()
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_message(LogLevel level, const char* message)
|
void log_message(LogLevel level, const char* format, ...)
|
||||||
{
|
{
|
||||||
const char* level_strings[] = { "INFO", "WARNING", "ERROR" };
|
const char* level_strings[] = { "INFO", "WARNING", "ERROR" };
|
||||||
|
static char* last_message = NULL;
|
||||||
|
static unsigned int repeat_count = 0;
|
||||||
|
static unsigned int exp_count = 1;
|
||||||
|
char* message = NULL;
|
||||||
|
|
||||||
// Open the log file in append mode
|
// Open the log file in append mode
|
||||||
FILE* log_file = fopen("logfile.txt", "a");
|
FILE* log_file = fopen("logfile.txt", "a");
|
||||||
@@ -21,9 +29,50 @@ void log_message(LogLevel level, const char* message)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the log message to the file
|
va_list args;
|
||||||
fprintf(log_file, "[%s] [%s] %s\n", get_current_time(), level_strings[level], message);
|
|
||||||
|
|
||||||
// Close the log file
|
va_start(args, format);
|
||||||
|
int message_size = vsnprintf(NULL, 0, format, args) + 1;
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
va_start(args, format);
|
||||||
|
message = malloc(message_size);
|
||||||
|
vsnprintf(message, message_size, format, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
if (!last_message) goto output;
|
||||||
|
// If current and last messages different
|
||||||
|
if (strcmp(message, last_message)) {
|
||||||
|
repeat_count = 0;
|
||||||
|
exp_count = 1;
|
||||||
|
goto output;
|
||||||
|
}
|
||||||
|
repeat_count++;
|
||||||
|
if (repeat_count < LOG_MAX_REPEAT) goto output;
|
||||||
|
|
||||||
|
unsigned int bit_width = sizeof(exp_count) * 8; // Calculate the number of bits in an int
|
||||||
|
unsigned int leftmost_bit_mask = 1 << (bit_width - 1); // Create a mask with the
|
||||||
|
while (exp_count < repeat_count && exp_count < leftmost_bit_mask) exp_count = exp_count << 1;
|
||||||
|
if (repeat_count != exp_count && repeat_count != LOG_MAX_REPEAT) goto clean;
|
||||||
|
|
||||||
|
fprintf(log_file, "[%s] [%s] %s !! x%u !!\n", get_current_time(), level_strings[level], last_message, repeat_count);
|
||||||
|
printf("[%s] %s !! x%u !!\n", level_strings[level], last_message, repeat_count);
|
||||||
|
|
||||||
|
goto clean;
|
||||||
|
output:
|
||||||
|
// Write the log message to the file
|
||||||
|
fprintf(log_file, "[%s] [%s] ", get_current_time(), level_strings[level]);
|
||||||
|
fprintf(log_file, "%s", message);
|
||||||
|
fprintf(log_file, "\n");
|
||||||
|
// Write to stdout
|
||||||
|
printf("[%s] ", level_strings[level]);
|
||||||
|
printf("%s", message);
|
||||||
|
printf("\n");
|
||||||
|
// Record last message
|
||||||
|
if (last_message) free(last_message);
|
||||||
|
last_message = malloc(message_size);
|
||||||
|
strcpy(last_message, message);
|
||||||
|
clean:
|
||||||
|
free(message);
|
||||||
fclose(log_file);
|
fclose(log_file);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
// Chat GPT code
|
// Chat GPT code
|
||||||
#include <stdio.h>
|
#ifndef LOG_H
|
||||||
#include <time.h>
|
#define LOG_H
|
||||||
#include <stdlib.h>
|
|
||||||
|
#define LOG_MAX_REPEAT 5
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LOG_INFO,
|
LOG_INFO,
|
||||||
@@ -11,4 +12,6 @@ typedef enum {
|
|||||||
|
|
||||||
const char* get_current_time();
|
const char* get_current_time();
|
||||||
|
|
||||||
void log_message(LogLevel level, const char* message);
|
void log_message(LogLevel level, const char* format, ...);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,27 +1,25 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_audio.h>
|
|
||||||
#include <SDL2/SDL_image.h>
|
|
||||||
#include <SDL2/SDL_ttf.h>
|
#include <SDL2/SDL_ttf.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "log.h"
|
#include <semaphore.h>
|
||||||
/*
|
#include <pthread.h>
|
||||||
* INFO: I will mark places to look at with INFO:
|
|
||||||
*/
|
|
||||||
const int SCREEN_WIDTH = 640;
|
|
||||||
const int SCREEN_HEIGHT = 480;
|
|
||||||
|
|
||||||
const int MAX_RECORDING_DEVICES = 10;
|
#include "texture.h"
|
||||||
const int MAX_RECORDING_SECONDS = 1;
|
#include "audio.h"
|
||||||
const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;
|
#include "net.h"
|
||||||
/*
|
#include "log.h"
|
||||||
* Application
|
|
||||||
*/
|
#define SCREEN_WIDTH 640
|
||||||
int init();
|
#define SCREEN_HEIGHT 480
|
||||||
void destroy();
|
#define TEXT_LINES 20
|
||||||
int select_device(SDL_KeyboardEvent* key_event, int max_index);
|
#define BODY_TEXT_LINES TEXT_LINES-2
|
||||||
|
#define FONT_SIZE SCREEN_HEIGHT/TEXT_LINES
|
||||||
|
|
||||||
|
#define PORT 55555
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
SOCKET_LISTEN,
|
||||||
SELECTING_DEVICE,
|
SELECTING_DEVICE,
|
||||||
RECORDING,
|
RECORDING,
|
||||||
RECORDED,
|
RECORDED,
|
||||||
@@ -30,86 +28,75 @@ typedef enum {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
application_state application_state;
|
application_state application_state;
|
||||||
|
audio_state audio;
|
||||||
int device_index;
|
int device_index;
|
||||||
int recording_device_id;
|
int update_ui;
|
||||||
int playback_device_id;
|
sem_t sem;
|
||||||
} state;
|
} state;
|
||||||
|
|
||||||
|
int init();
|
||||||
|
void destroy();
|
||||||
|
int select_device(SDL_KeyboardEvent* key_event, int max_index);
|
||||||
void application_state_to_string(application_state state, char* string);
|
void application_state_to_string(application_state state, char* string);
|
||||||
/*
|
|
||||||
* Visual
|
void* broadcast_t(void* args);
|
||||||
*/
|
void* peer_discovery_t(void* args);
|
||||||
|
|
||||||
SDL_Window* window = NULL;
|
SDL_Window* window = NULL;
|
||||||
SDL_Renderer* renderer = 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 INFO: audio related declarations and callback here.
|
|
||||||
*/
|
|
||||||
int num_devices = 0;
|
int num_devices = 0;
|
||||||
|
|
||||||
SDL_AudioSpec recording_spec;
|
int handle_input(SDL_Event* event, state* state);
|
||||||
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);
|
void handle_task(state* state);
|
||||||
|
void render_body_text(text_texture* textures, int len);
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (!init()) return 1;
|
if (!init()) return 1;
|
||||||
|
|
||||||
num_devices = SDL_GetNumAudioDevices(1);
|
num_devices = SDL_GetNumAudioDevices(1);
|
||||||
printf("Num audio devices: %i\n", num_devices);
|
log_message(LOG_INFO, "Num audio devices: %i", num_devices);
|
||||||
char char_buffer[64];
|
char char_buffer[64];
|
||||||
char default_fmt[] = "Device selected: %i";
|
char device_selected_fmt[] = "Device selected: %i";
|
||||||
|
|
||||||
state state = {
|
state state;
|
||||||
SELECTING_DEVICE,
|
state.application_state = SOCKET_LISTEN;
|
||||||
-1,
|
//state.application_state = SELECTING_DEVICE;
|
||||||
0,
|
if (!audio_init(&state.audio)) return 1;
|
||||||
0,
|
state.device_index = -1;
|
||||||
};
|
state.update_ui = 0;
|
||||||
|
sem_init(&state.sem, 0, 1);
|
||||||
|
|
||||||
text_texture display_text_texture;
|
text_texture display_text_texture;
|
||||||
text_texture_init(&display_text_texture);
|
text_texture_init(&display_text_texture, renderer, FONT_SIZE);
|
||||||
sprintf(char_buffer, default_fmt, state.device_index);
|
sprintf(char_buffer, device_selected_fmt, state.device_index);
|
||||||
text_texture_load(&display_text_texture, char_buffer);
|
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 state_text_texture;
|
||||||
text_texture_init(&state_text_texture);
|
text_texture_init(&state_text_texture, renderer, FONT_SIZE);
|
||||||
application_state_to_string(state.application_state, char_buffer);
|
application_state_to_string(state.application_state, char_buffer);
|
||||||
text_texture_load(&state_text_texture, 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);
|
||||||
|
|
||||||
|
pthread_t broadcast_thread;
|
||||||
|
pthread_create(&broadcast_thread, NULL, broadcast_t, &state);
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (1) {
|
while (1) {
|
||||||
|
sem_wait(&state.sem);
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
SDL_Keycode keysym = event.key.keysym.sym;
|
SDL_Keycode keysym = event.key.keysym.sym;
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
@@ -119,20 +106,35 @@ int main(int argc, char* argv[])
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
// INFO: main state logic here.
|
if (!handle_input(&event, &state)) goto cleanup;
|
||||||
handle_input(&event, &state);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// INFO: more state logic here. I check the buffer write position here
|
|
||||||
handle_task(&state);
|
handle_task(&state);
|
||||||
|
|
||||||
sprintf(char_buffer, default_fmt, state.device_index);
|
if (state.update_ui) {
|
||||||
|
sprintf(char_buffer, device_selected_fmt, state.device_index);
|
||||||
text_texture_load(&display_text_texture, char_buffer);
|
text_texture_load(&display_text_texture, char_buffer);
|
||||||
application_state_to_string(state.application_state, char_buffer);
|
application_state_to_string(state.application_state, char_buffer);
|
||||||
text_texture_load(&state_text_texture, char_buffer);
|
text_texture_load(&state_text_texture, char_buffer);
|
||||||
|
log_message(LOG_INFO, "Update UI State: %s", char_buffer);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
struct sockaddr_in* peer_addr;
|
||||||
|
while (net_get_peers(&peer_addr)) {
|
||||||
|
if (i >= socket_frame.len) break;;
|
||||||
|
char* ip = inet_ntoa(peer_addr->sin_addr);
|
||||||
|
int port = ntohs(peer_addr->sin_port);
|
||||||
|
sprintf(char_buffer, "%s:%i", ip, port);
|
||||||
|
text_texture_load(&socket_frame.textures[i], char_buffer);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
for (; i < socket_frame.len; i++) text_texture_load(&socket_frame.textures[i], "");
|
||||||
|
|
||||||
|
state.update_ui = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Clear the screen
|
// Clear the screen
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
@@ -141,103 +143,104 @@ int main(int argc, char* argv[])
|
|||||||
// Render
|
// Render
|
||||||
text_texture_render(&display_text_texture, 0, 0);
|
text_texture_render(&display_text_texture, 0, 0);
|
||||||
|
|
||||||
if (state.application_state == SELECTING_DEVICE) {
|
if (state.application_state == SOCKET_LISTEN) {
|
||||||
int text_y_offset_step = display_text_texture.height * 2;
|
text_texture_frame_render(&socket_frame, 0, FONT_SIZE);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (state.application_state == SELECTING_DEVICE) {
|
||||||
|
text_texture_frame_render(&select_device_frame, 0, FONT_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
text_texture_render(&state_text_texture, 0, SCREEN_HEIGHT - state_text_texture.height);
|
text_texture_render(&state_text_texture, 0, SCREEN_HEIGHT - state_text_texture.height);
|
||||||
|
|
||||||
// Update the screen
|
// Update the screen
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
sem_post(&state.sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
SDL_CloseAudioDevice(state.recording_device_id);
|
log_message(LOG_INFO, "Cleanup start");
|
||||||
SDL_CloseAudioDevice(state.playback_device_id);
|
audio_destroy(&state.audio);
|
||||||
text_texture_free(&display_text_texture);
|
log_message(LOG_INFO, "Audio done");
|
||||||
for (int i = 0; i < num_devices; i++) {
|
text_texture_destroy(&display_text_texture);
|
||||||
text_texture_free(&device_textures[i]);
|
text_texture_destroy(&state_text_texture);
|
||||||
}
|
text_texture_frame_destroy(&socket_frame);
|
||||||
|
text_texture_frame_destroy(&select_device_frame);
|
||||||
|
log_message(LOG_INFO, "Texture done");
|
||||||
destroy();
|
destroy();
|
||||||
|
log_message(LOG_INFO, "Done");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_input(SDL_Event* event, state* state)
|
int handle_input(SDL_Event* event, state* state)
|
||||||
{
|
{
|
||||||
SDL_Keycode keysym = event->key.keysym.sym;
|
SDL_Keycode keysym = event->key.keysym.sym;
|
||||||
switch (state->application_state) {
|
switch (state->application_state) {
|
||||||
// INFO: init of recording audio device here
|
|
||||||
|
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");
|
||||||
|
net_print_peers();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case SELECTING_DEVICE:
|
case SELECTING_DEVICE:
|
||||||
switch (keysym) {
|
switch (keysym) {
|
||||||
case SDLK_y:
|
case SDLK_y:
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
// INFO: init audio device here and below
|
if (state->device_index == -1 || !state->audio.recording_device_id) break;
|
||||||
// Here it initialises everytime you press enter or y, and
|
audio_buffer_reset(&state->audio);
|
||||||
// unpauses device immediately. Seems to work mor consistently
|
state->audio.recording_buffer_position = 0;
|
||||||
SDL_CloseAudioDevice(state->recording_device_id);
|
audio_device_pause(&state->audio, 1, 0);
|
||||||
state->recording_device_id =
|
|
||||||
audio_recording_init(state->device_index, NULL);
|
|
||||||
|
|
||||||
if (state->device_index == -1 || !state->recording_device_id) break;
|
|
||||||
recording_buffer_position = 0;
|
|
||||||
// INFO: calling this should unpause the device and start calling the callback
|
|
||||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
|
||||||
/*SDL_UnlockAudioDevice(state->recording_device_id);*/
|
|
||||||
// INFO: state change looks like this here
|
|
||||||
state->application_state = RECORDING;
|
state->application_state = RECORDING;
|
||||||
|
state->update_ui = 1;
|
||||||
break;
|
break;
|
||||||
|
case SDLK_q:
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
state->device_index = -1;
|
return 0;
|
||||||
SDL_CloseAudioDevice(state->recording_device_id);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int device_index_new = select_device(&event->key, num_devices);
|
int device_index_new = select_device(&event->key, num_devices);
|
||||||
if (device_index_new == -1) break;
|
if (device_index_new == -1) break;
|
||||||
if (state->device_index != -1 && device_index_new == -1) break;
|
if (device_index_new == state->device_index) break;
|
||||||
state->device_index = device_index_new;
|
state->device_index = device_index_new;
|
||||||
// INFO: inititialising here seems to be less consistent. When I
|
audio_device_close(&state->audio, 1);
|
||||||
// spam enter or y after initialising here, Sometimes I see
|
audio_recording_init(&state->audio, state->device_index);
|
||||||
// callback being called sometimes not.
|
state->update_ui = 1;
|
||||||
/*SDL_CloseAudioDevice(state->recording_device_id);*/
|
|
||||||
/*state->recording_device_id = audio_recording_init(state->device_index, NULL);*/
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RECORDING:
|
case RECORDING:
|
||||||
switch (keysym) {
|
switch (keysym) {
|
||||||
case SDLK_y:
|
|
||||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
|
||||||
break;
|
|
||||||
case SDLK_q:
|
case SDLK_q:
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
audio_device_pause(&state->audio, 1, 1);
|
||||||
audio_buffer_destroy();
|
|
||||||
state->application_state = SELECTING_DEVICE;
|
state->application_state = SELECTING_DEVICE;
|
||||||
|
state->update_ui = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// INFO: init of playback audio device here. Can't seem to get it to play / call callback
|
|
||||||
case RECORDED:
|
case RECORDED:
|
||||||
switch (keysym) {
|
switch (keysym) {
|
||||||
case SDLK_y:
|
case SDLK_y:
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
SDL_CloseAudioDevice(state->playback_device_id);
|
if (!state->audio.playback_device_id)
|
||||||
state->playback_device_id = audio_playback_init(NULL);
|
audio_playback_init(&state->audio);
|
||||||
recording_buffer_position = 0;
|
state->audio.recording_buffer_position = 0;
|
||||||
SDL_PauseAudioDevice(state->playback_device_id, 0);
|
audio_device_pause(&state->audio, 0, 0);
|
||||||
/*SDL_UnlockAudioDevice(state->playback_device_id);*/
|
|
||||||
state->application_state = PLAYBACK;
|
state->application_state = PLAYBACK;
|
||||||
|
state->update_ui = 1;
|
||||||
break;
|
break;
|
||||||
case SDLK_q:
|
case SDLK_q:
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
audio_buffer_destroy();
|
|
||||||
state->application_state = SELECTING_DEVICE;
|
state->application_state = SELECTING_DEVICE;
|
||||||
|
state->update_ui = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -246,49 +249,42 @@ void handle_input(SDL_Event* event, state* state)
|
|||||||
switch (keysym) {
|
switch (keysym) {
|
||||||
case SDLK_q:
|
case SDLK_q:
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
|
audio_device_pause(&state->audio, 0, 1);
|
||||||
state->application_state = RECORDED;
|
state->application_state = RECORDED;
|
||||||
|
state->update_ui = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_task(state* state)
|
void handle_task(state* state)
|
||||||
{
|
{
|
||||||
switch (state->application_state) {
|
switch (state->application_state) {
|
||||||
|
|
||||||
|
case SOCKET_LISTEN:
|
||||||
|
break;
|
||||||
|
|
||||||
case SELECTING_DEVICE:
|
case SELECTING_DEVICE:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RECORDING:
|
case RECORDING:
|
||||||
// INFO: check buffer position here and stop if reaces the end
|
if (state->audio.recording_buffer_position >= state->audio.recording_buffer_size) {
|
||||||
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;
|
state->application_state = RECORDED;
|
||||||
break;
|
state->update_ui = 1;
|
||||||
}
|
}
|
||||||
SDL_UnlockAudioDevice(state->recording_device_id);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RECORDED:
|
case RECORDED:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PLAYBACK:
|
case PLAYBACK:
|
||||||
// INFO:
|
if (state->audio.recording_buffer_position >= state->audio.recording_buffer_size) {
|
||||||
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;
|
state->application_state = RECORDED;
|
||||||
break;
|
state->update_ui = 1;
|
||||||
}
|
}
|
||||||
SDL_UnlockAudioDevice(state->playback_device_id);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -297,12 +293,12 @@ void handle_task(state* state)
|
|||||||
int init()
|
int init()
|
||||||
{
|
{
|
||||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
||||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
log_message(LOG_ERROR, "SDL could not initialize! SDL Error: %s", SDL_GetError());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TTF_Init() == -1) {
|
if (TTF_Init() == -1) {
|
||||||
printf("TTF could not initialize! TTF Error: %s\n", TTF_GetError());
|
log_message(LOG_ERROR, "TTF could not initialize! TTF Error: %s", TTF_GetError());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,48 +307,45 @@ int init()
|
|||||||
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
|
|
||||||
if (!window) {
|
if (!window) {
|
||||||
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
log_message(LOG_ERROR, "Window could not be created! SDL Error: %s", SDL_GetError());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||||
if (!renderer) {
|
if (!renderer) {
|
||||||
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
|
log_message(LOG_ERROR, "Renderer could not be created! SDL_Error: %s", SDL_GetError());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
global_font = TTF_OpenFont("FiraCode-Regular.ttf", 24);
|
net_init(PORT);
|
||||||
if (!global_font) {
|
|
||||||
printf("Failed to load font! TTF_Error: %s\n", TTF_GetError());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy()
|
void destroy()
|
||||||
{
|
{
|
||||||
free(recording_buffer);
|
|
||||||
|
|
||||||
TTF_CloseFont(global_font);
|
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
|
log_message(LOG_INFO, "Renderer done");
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
|
log_message(LOG_INFO, "Window done");
|
||||||
TTF_Quit();
|
TTF_Quit();
|
||||||
|
log_message(LOG_INFO, "TTF done");
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
int select_device(SDL_KeyboardEvent* key_event, int max_index)
|
int select_device(SDL_KeyboardEvent* key_event, int max_index)
|
||||||
{
|
{
|
||||||
SDL_Keycode keycode = key_event->keysym.sym;
|
SDL_Keycode keycode = key_event->keysym.sym;
|
||||||
SDL_Keycode mod = key_event->keysym.mod;
|
/*SDL_Keycode mod = key_event->keysym.mod;*/
|
||||||
|
|
||||||
int keycode_is_num = (keycode >= SDLK_0 && keycode <= SDLK_9);
|
int keycode_is_num = (keycode >= SDLK_0 && keycode <= SDLK_9);
|
||||||
int no_mod = (mod == 0);
|
/*int no_mod = (mod == 0);*/
|
||||||
if (!keycode_is_num || !no_mod) {
|
/*if (!keycode_is_num || !no_mod) {*/
|
||||||
printf("Not number: %c <%u>\n", keycode, keycode);
|
if (!keycode_is_num) {
|
||||||
|
log_message(LOG_INFO, "Not number: %c <%u>", keycode, keycode);
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
printf("Is number: %c <%u>\n", keycode, keycode);
|
log_message(LOG_INFO, "Is number: %c <%u>", keycode, keycode);
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = keycode - SDLK_0;
|
int index = keycode - SDLK_0;
|
||||||
@@ -364,171 +357,40 @@ int select_device(SDL_KeyboardEvent* key_event, int max_index)
|
|||||||
void application_state_to_string(application_state state, char* string)
|
void application_state_to_string(application_state state, char* string)
|
||||||
{
|
{
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
case SOCKET_LISTEN:
|
||||||
|
sprintf(string, "Socket");
|
||||||
|
break;
|
||||||
case SELECTING_DEVICE:
|
case SELECTING_DEVICE:
|
||||||
sprintf(string, "Selecting Device [0-9] -> [y/RET], ");
|
sprintf(string, "Selecting Device [0-9] -> [y/RET]");
|
||||||
break;
|
break;
|
||||||
case RECORDING:
|
case RECORDING:
|
||||||
sprintf(string, "Recording");
|
sprintf(string, "Recording [ESC]: CANCEL");
|
||||||
break;
|
break;
|
||||||
case RECORDED:
|
case RECORDED:
|
||||||
sprintf(string, "Recorded");
|
sprintf(string, "Recorded [y/RET]: PLAY | [ESC]: SELECT");
|
||||||
break;
|
break;
|
||||||
case PLAYBACK:
|
case PLAYBACK:
|
||||||
sprintf(string, "Playing");
|
sprintf(string, "Playing [ESC]: CANCEL");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int text_texture_init(text_texture* text_texture)
|
void* broadcast_t(void* args)
|
||||||
{
|
{
|
||||||
text_texture->texture = NULL;
|
while (1) {
|
||||||
text_texture->width = 0;
|
net_broadcast();
|
||||||
text_texture->height = 0;
|
sleep(1);
|
||||||
return 1;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int text_texture_load(text_texture* text_texture, char* string)
|
void* peer_discovery_t(void* args)
|
||||||
{
|
{
|
||||||
if (text_texture->texture) {
|
state* state = args;
|
||||||
SDL_DestroyTexture(text_texture->texture);
|
while (1) {
|
||||||
text_texture->texture = NULL;
|
int added_client = net_listen_peers();
|
||||||
|
int pruned_client = net_prune_peers();
|
||||||
|
sem_wait(&state->sem);
|
||||||
|
state->update_ui = added_client || pruned_client;
|
||||||
|
sem_post(&state->sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
// INFO: device init here
|
|
||||||
// Most of the audio stuff I copied from an example
|
|
||||||
SDL_AudioDeviceID audio_recording_init(int index, void* userdata)
|
|
||||||
{
|
|
||||||
// INFO: audio spec and callback
|
|
||||||
SDL_zero(recording_spec);
|
|
||||||
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;
|
|
||||||
// INFO: I just copied example where they allocated 1 second worth of buffer so it doesn't die
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
// INFO:
|
|
||||||
SDL_AudioDeviceID audio_playback_init(void* userdata)
|
|
||||||
{
|
|
||||||
if (!recording_buffer) {
|
|
||||||
printf("Audio buffer not initialized");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// INFO: audio spec and callback
|
|
||||||
SDL_zero(playback_spec);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
// INFO: callback here
|
|
||||||
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);
|
|
||||||
printf(string, "Record pos: %u %u\n", recording_buffer_position, len);
|
|
||||||
log_message(LOG_INFO, string);
|
|
||||||
}
|
|
||||||
// INFO:
|
|
||||||
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);
|
|
||||||
printf(string, "Playback pos: %u %u", recording_buffer_position, len);
|
|
||||||
log_message(LOG_INFO, string);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ $(OBJS): $(OBJD)/%.o: %.c
|
|||||||
$(CC) $(CCFLAGS) -c $? -o $@
|
$(CC) $(CCFLAGS) -c $? -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -r $(TARGET) $(OBJD)
|
rm -r $(TARGET) $(TARGET1) $(OBJD)
|
||||||
|
|
||||||
run: $(TARGET)
|
run: $(TARGET)
|
||||||
./$(TARGET)
|
./$(TARGET)
|
||||||
|
|||||||
@@ -0,0 +1,277 @@
|
|||||||
|
#include "net.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
int socket;
|
||||||
|
socklen_t addr_len;
|
||||||
|
} net_info;
|
||||||
|
|
||||||
|
typedef struct peer_addr_node {
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
time_t time_last;
|
||||||
|
time_t time_elapsed;
|
||||||
|
struct peer_addr_node* next;
|
||||||
|
} peer_addr_node;
|
||||||
|
static peer_addr_node* peer_list = NULL;
|
||||||
|
|
||||||
|
static int peer_list_add(struct sockaddr_in* addr);
|
||||||
|
static int peer_list_exists(struct sockaddr_in* addr);
|
||||||
|
static void peer_list_destroy();
|
||||||
|
|
||||||
|
static net_info broadcast_info;
|
||||||
|
static net_info listen_info;
|
||||||
|
static net_info peer_info;
|
||||||
|
static int PORT;
|
||||||
|
|
||||||
|
static int init = 0;
|
||||||
|
|
||||||
|
int net_init(unsigned int port)
|
||||||
|
{
|
||||||
|
PORT = port;
|
||||||
|
// broadcast socket
|
||||||
|
broadcast_info.socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (broadcast_info.socket < 0) {
|
||||||
|
log_message(LOG_ERROR, "Error creating broadcast socket");
|
||||||
|
close(broadcast_info.socket);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int broadcast_enable = 1;
|
||||||
|
if (setsockopt(broadcast_info.socket, SOL_SOCKET, SO_BROADCAST, &broadcast_enable, sizeof(broadcast_enable)) < 0) return 0;
|
||||||
|
|
||||||
|
memset(&broadcast_info.addr, 0, sizeof(broadcast_info.addr));
|
||||||
|
broadcast_info.addr.sin_family = AF_INET;
|
||||||
|
broadcast_info.addr.sin_addr.s_addr = inet_addr("255.255.255.255");
|
||||||
|
broadcast_info.addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
broadcast_info.addr_len = sizeof(broadcast_info.addr);
|
||||||
|
|
||||||
|
// listen socket
|
||||||
|
listen_info.socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (listen_info.socket < 0) {
|
||||||
|
log_message(LOG_ERROR, "Error creating listen socket", strerror(errno));
|
||||||
|
close(listen_info.socket);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int reuse_enable = 1;
|
||||||
|
if (setsockopt(listen_info.socket, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(reuse_enable)) < 0) return 0;
|
||||||
|
|
||||||
|
memset(&listen_info.addr, 0, sizeof(listen_info.addr));
|
||||||
|
listen_info.addr.sin_family = AF_INET;
|
||||||
|
listen_info.addr.sin_addr.s_addr = INADDR_BROADCAST;
|
||||||
|
listen_info.addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
listen_info.addr_len =sizeof(listen_info.addr);
|
||||||
|
|
||||||
|
struct timeval tv;
|
||||||
|
tv.tv_sec = 0;
|
||||||
|
tv.tv_usec = 100000;
|
||||||
|
setsockopt(listen_info.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||||
|
|
||||||
|
if (bind(listen_info.socket, (const struct sockaddr*)&listen_info.addr, sizeof(listen_info.addr)) < 0) {
|
||||||
|
log_message(LOG_ERROR, "Socket bind failed: %s", strerror(errno));
|
||||||
|
close(listen_info.socket);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
peer_info.socket = -1;
|
||||||
|
|
||||||
|
init = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void net_destroy()
|
||||||
|
{
|
||||||
|
close(broadcast_info.socket);
|
||||||
|
close(listen_info.socket);
|
||||||
|
close(peer_info.socket);
|
||||||
|
peer_list_destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_broadcast()
|
||||||
|
{
|
||||||
|
if (!init) return 0;;
|
||||||
|
const char* message = "broadcast message";
|
||||||
|
int retv = sendto(broadcast_info.socket, message,
|
||||||
|
strlen(message), 0,
|
||||||
|
(struct sockaddr*)&broadcast_info.addr,
|
||||||
|
broadcast_info.addr_len);
|
||||||
|
if (retv < 0) {
|
||||||
|
log_message(LOG_ERROR, "Failed to send broadcast message: %s", strerror(errno));
|
||||||
|
log_message(LOG_ERROR, "retv: %i", retv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
log_message(LOG_INFO, "Broadcast message sent");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_listen_peers()
|
||||||
|
{
|
||||||
|
if (!init) return 0;
|
||||||
|
|
||||||
|
net_info broadcast_listen;
|
||||||
|
|
||||||
|
broadcast_listen.socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (broadcast_listen.socket < 0) {
|
||||||
|
log_message(LOG_ERROR, "Error creating listen socket", strerror(errno));
|
||||||
|
close(broadcast_listen.socket);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buffer[64];
|
||||||
|
|
||||||
|
memset(&broadcast_listen.addr, 0, sizeof(broadcast_listen.addr));
|
||||||
|
broadcast_listen.addr.sin_family = AF_INET;
|
||||||
|
broadcast_listen.addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
broadcast_listen.addr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
broadcast_listen.addr_len = sizeof(broadcast_listen.addr);
|
||||||
|
|
||||||
|
int retv = recvfrom(listen_info.socket, buffer, sizeof(buffer), 0,
|
||||||
|
(struct sockaddr*)&broadcast_listen.addr,
|
||||||
|
&broadcast_listen.addr_len);
|
||||||
|
|
||||||
|
close(broadcast_listen.socket);
|
||||||
|
|
||||||
|
if (retv < 0) {
|
||||||
|
if (errno != EAGAIN)
|
||||||
|
log_message(LOG_ERROR, "Failed to receive broadcast message: %s", strerror(errno));
|
||||||
|
close(broadcast_listen.socket);
|
||||||
|
broadcast_listen.socket = -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[retv] = '\0';
|
||||||
|
|
||||||
|
log_message(LOG_INFO,
|
||||||
|
"Broadcast message received from:\n%s:%d\n%s",
|
||||||
|
inet_ntoa(broadcast_listen.addr.sin_addr),
|
||||||
|
ntohs(broadcast_listen.addr.sin_port),
|
||||||
|
buffer);
|
||||||
|
|
||||||
|
retv = peer_list_exists(&broadcast_listen.addr);
|
||||||
|
if (retv) return 0;
|
||||||
|
|
||||||
|
peer_list_add(&broadcast_listen.addr);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_prune_peers()
|
||||||
|
{
|
||||||
|
if (!init) return 0;
|
||||||
|
if (!peer_list) return 0;
|
||||||
|
int pruned = 0;
|
||||||
|
peer_addr_node* node = peer_list;
|
||||||
|
peer_addr_node* last_node = NULL;
|
||||||
|
while (node) {
|
||||||
|
peer_addr_node* next_node = node->next;
|
||||||
|
|
||||||
|
node->time_elapsed += time(NULL) - node->time_last;
|
||||||
|
node->time_last = time(NULL);
|
||||||
|
|
||||||
|
if (node->time_elapsed < CLIENT_MAX_TIME) {
|
||||||
|
last_node = node;
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
|
||||||
|
pruned = 1;
|
||||||
|
free(node);
|
||||||
|
if (node == peer_list)
|
||||||
|
peer_list = next_node;
|
||||||
|
if (last_node != NULL)
|
||||||
|
last_node->next = next_node;
|
||||||
|
next:
|
||||||
|
node = next_node;
|
||||||
|
}
|
||||||
|
return pruned;
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_send(void* buffer, size_t size)
|
||||||
|
{
|
||||||
|
if (!init) return 0;
|
||||||
|
if (peer_info.socket < 0) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_receive()
|
||||||
|
{
|
||||||
|
if (!init) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void net_print_peers()
|
||||||
|
{
|
||||||
|
if (!peer_list) return;
|
||||||
|
peer_addr_node* node = peer_list;
|
||||||
|
while (node) {
|
||||||
|
log_message(LOG_INFO,
|
||||||
|
"Peers :\n%s:%d",
|
||||||
|
inet_ntoa(node->addr.sin_addr),
|
||||||
|
ntohs(node->addr.sin_port));
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int net_get_peers(struct sockaddr_in** peer_addr)
|
||||||
|
{
|
||||||
|
if (!peer_list) return 0;
|
||||||
|
static peer_addr_node* node = NULL;
|
||||||
|
static int done = 0;
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
done = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!node) node = peer_list;
|
||||||
|
*peer_addr = &node->addr;
|
||||||
|
|
||||||
|
if (!node->next) done = 1;
|
||||||
|
node = node->next;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int peer_list_add(struct sockaddr_in* addr)
|
||||||
|
{
|
||||||
|
peer_addr_node* new_node = malloc(sizeof(peer_addr_node));
|
||||||
|
size_t size = sizeof(struct sockaddr_in);
|
||||||
|
memset(&new_node->addr, 0, sizeof(size));
|
||||||
|
memcpy(&new_node->addr, addr, sizeof(size));
|
||||||
|
new_node->time_last = time(NULL);
|
||||||
|
new_node->time_elapsed = 0;
|
||||||
|
new_node->next = peer_list ? peer_list : NULL;
|
||||||
|
peer_list = new_node;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int peer_list_exists(struct sockaddr_in* addr)
|
||||||
|
{
|
||||||
|
if (!peer_list) return 0;
|
||||||
|
peer_addr_node* node = peer_list;
|
||||||
|
while (node) {
|
||||||
|
int address_match = addr->sin_addr.s_addr == node->addr.sin_addr.s_addr;
|
||||||
|
int port_match = addr->sin_port == node->addr.sin_port;
|
||||||
|
if (address_match && port_match) {
|
||||||
|
node->time_elapsed = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void peer_list_destroy()
|
||||||
|
{
|
||||||
|
peer_addr_node* node = peer_list;
|
||||||
|
while (node) {
|
||||||
|
peer_addr_node* next_node = node->next;
|
||||||
|
free(node);
|
||||||
|
node = next_node;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef NET_H
|
||||||
|
#define NET_H
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
|
||||||
|
#define CLIENT_MAX_TIME 4
|
||||||
|
|
||||||
|
int net_init(unsigned int port);
|
||||||
|
void net_destroy();
|
||||||
|
int net_broadcast();
|
||||||
|
int net_listen_peers();
|
||||||
|
int net_prune_peers();
|
||||||
|
int net_send(void* buffer, size_t size);
|
||||||
|
int net_receive();
|
||||||
|
void net_print_peers();
|
||||||
|
int net_get_peers(struct sockaddr_in** peer_addr);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
#include "texture.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer, int font_size)
|
||||||
|
{
|
||||||
|
text_texture->texture = NULL;
|
||||||
|
text_texture->renderer = renderer;
|
||||||
|
text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", font_size);
|
||||||
|
if (!text_texture->font) {
|
||||||
|
log_message(LOG_ERROR, "Failed to load font! TTF_Error: %s", TTF_GetError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
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 };
|
||||||
|
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());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(text_texture->renderer, text_surface);
|
||||||
|
if (!texture) {
|
||||||
|
log_message(LOG_ERROR, "Unable to create texture from rendered text! SDL_Error: %s", SDL_GetError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
text_texture->texture = texture;
|
||||||
|
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) {
|
||||||
|
log_message(LOG_ERROR, "text_texture uninitialized");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (!text_texture->texture) {
|
||||||
|
log_message(LOG_ERROR, "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(text_texture->renderer, text_texture->texture, NULL, &render_quad);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void text_texture_destroy(text_texture* text_texture)
|
||||||
|
{
|
||||||
|
if (!text_texture->texture) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SDL_DestroyTexture(text_texture->texture);
|
||||||
|
TTF_CloseFont(text_texture->font);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_load(&text_texture_frame->textures[i], "");
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void 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);
|
||||||
|
text_texture_frame->textures = NULL;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef TEXTURE_H
|
||||||
|
#define TEXTURE_H
|
||||||
|
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include "SDL2/SDL.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SDL_Texture* texture;
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
TTF_Font* font;
|
||||||
|
int width;
|
||||||
|
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);
|
||||||
|
void text_texture_destroy(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_frame_init(text_texture_frame* text_texture_frame, int len, SDL_Renderer* renderer, int font_size);
|
||||||
|
void text_texture_frame_destroy(text_texture_frame* text_texture_frame);
|
||||||
|
int text_texture_frame_render(text_texture_frame* text_texture_frame, int x, int y);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user