Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions shared/audio/cuatro.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "math/math.h"
#include "filesystem/filesystem.h"
#include "files/fs.h"
#include "syscalls/syscalls.h"
#include "cuatro.h"
#include "wav.h"
Expand Down Expand Up @@ -30,35 +30,35 @@ int8_t play_audio_async(audio_samples *audio, int16_t amplitude){
static file mixer = { .id = 0 }; // 0 ok as filesystem ids > 256

static bool mixer_open_file(){
if (mixer.id == 0 && FS_RESULT_SUCCESS != open_file("/dev/audio/output", &mixer)) return false;
if (mixer.id == 0 && FS_RESULT_SUCCESS != fopen("/dev/audio/output", &mixer)) return false;
return true;
}

int8_t mixer_open_line(){
if (!mixer_open_file()) return NULL;
mixer_line_data data = { -1, {0, 0} };
if (sizeof(mixer_line_data) != read_file(&mixer, (char*)&data, sizeof(mixer_line_data))) return NULL;
if (sizeof(mixer_line_data) != fread(&mixer, (char*)&data, sizeof(mixer_line_data))) return NULL;
return data.lineId;
}

void mixer_close_line(int8_t lineId){
if (mixer_open_file()){
mixer_command command = { lineId, MIXER_CLOSE_LINE, .value = 0 };
write_file(&mixer, (char*)&command, sizeof(mixer_command));
fwrite(&mixer, (char*)&command, sizeof(mixer_command));
}
}

void mixer_play_async(int8_t lineId, audio_samples* audio){
if (mixer_open_file()){
mixer_command command = { lineId, MIXER_PLAY, .audio = audio };
write_file(&mixer, (char*)&command, sizeof(mixer_command));
fwrite(&mixer, (char*)&command, sizeof(mixer_command));
}
}

bool mixer_still_playing(int8_t lineId){
if (mixer_open_file()){
mixer_line_data data = { lineId, {1, 1} };
if (sizeof(mixer_line_data) == read_file(&mixer, (char*)&data, sizeof(mixer_line_data))){
if (sizeof(mixer_line_data) == fread(&mixer, (char*)&data, sizeof(mixer_line_data))){
if (data.count[0] != 0 || data.count[1] != 0){
// TODO: this won't work for streaming outputs (when implemented) - race condition
return true;
Expand All @@ -71,23 +71,23 @@ bool mixer_still_playing(int8_t lineId){
bool mixer_mute(){
if (mixer_open_file()){
mixer_command command = { -1, MIXER_MUTE, .value=0 };
write_file(&mixer, (char*)&command, sizeof(mixer_command));
fwrite(&mixer, (char*)&command, sizeof(mixer_command));
}
return false; // TODO: return prev setting
}

bool mixer_unmute(){
if (mixer_open_file()){
mixer_command command = { -1, MIXER_UNMUTE, .value=0 };
write_file(&mixer, (char*)&command, sizeof(mixer_command));
fwrite(&mixer, (char*)&command, sizeof(mixer_command));
}
return true; // TODO: return prev setting
}

uint32_t mixer_set_level(int16_t level){
if (mixer_open_file()){
mixer_command command = { -1, MIXER_SETLEVEL, .value = (uintptr_t)level };
write_file(&mixer, (char*)&command, sizeof(mixer_command));
fwrite(&mixer, (char*)&command, sizeof(mixer_command));
}
return level; // TODO: return prev setting
}
16 changes: 8 additions & 8 deletions shared/audio/wav.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "types.h"
#include "filesystem/filesystem.h"
#include "files/fs.h"
#include "memory/talloc.h"
#include "std/memory.h"
#include "math/math.h"
Expand Down Expand Up @@ -31,7 +31,7 @@ typedef struct wav_header {

static void transform_16bit(wav_header* hdr, audio_samples* audio, uint32_t upsample, file* fd){
int16_t* tbuf = (int16_t*)malloc(hdr->data_size);
read_file(fd, (char*)tbuf, hdr->data_size);
fread(fd, (char*)tbuf, hdr->data_size);
audio->samples.size = hdr->data_size * upsample;
audio->samples.ptr = (uintptr_t)malloc(audio->samples.size);
audio->smpls_per_channel = audio->samples.size / (sizeof(int16_t) * hdr->channels);
Expand All @@ -51,7 +51,7 @@ static void transform_16bit(wav_header* hdr, audio_samples* audio, uint32_t upsa

static void transform_8bit(wav_header* hdr, audio_samples* audio, uint32_t upsample, file* fd){
uint8_t* tbuf = (uint8_t*)malloc(hdr->data_size);
read_file(fd, (char*)tbuf, hdr->data_size);
fread(fd, (char*)tbuf, hdr->data_size);
audio->samples.size = hdr->data_size * upsample * sizeof(int16_t);
audio->samples.ptr = (uintptr_t)malloc(audio->samples.size);
audio->smpls_per_channel = audio->samples.size / (sizeof(int16_t) * hdr->channels);
Expand All @@ -72,14 +72,14 @@ static void transform_8bit(wav_header* hdr, audio_samples* audio, uint32_t upsam
bool wav_load_as_int16(const char* path, audio_samples* audio){
file fd = {};

if (FS_RESULT_SUCCESS != open_file(path, &fd))
if (FS_RESULT_SUCCESS != fopen(path, &fd))
{
//printf("[WAV] File not found: %s", path);
return false;
}

wav_header hdr = {};
size_t read_size = read_file(&fd, (char*)&hdr, sizeof(wav_header));
size_t read_size = fread(&fd, (char*)&hdr, sizeof(wav_header));
if (read_size != sizeof(wav_header) ||
hdr.id != 0x46464952 || // 'RIFF'
hdr.wave_id != 0x45564157 || // 'WAVE'
Expand All @@ -93,7 +93,7 @@ bool wav_load_as_int16(const char* path, audio_samples* audio){
hdr.data_size == 0
)
{
close_file(&fd);
fclose(&fd);
printf("[WAV] Unsupported file format %s", path);
printf("=== Sizes %i, %i, %i", read_size, fd.size, hdr.data_size);
printf("=== id %x", hdr.id);
Expand All @@ -113,7 +113,7 @@ bool wav_load_as_int16(const char* path, audio_samples* audio){
// simple case: slurp samples direct from file to wav buffer
audio->samples.size = hdr.data_size;
audio->samples.ptr = (uintptr_t)malloc(audio->samples.size);
read_file(&fd, (char*)audio->samples.ptr, audio->samples.size);
fread(&fd, (char*)audio->samples.ptr, audio->samples.size);
audio->smpls_per_channel = hdr.data_size / (sizeof(int16_t) * hdr.channels);
audio->channels = hdr.channels;
audio->secs = audio->smpls_per_channel / 44100.f;
Expand All @@ -124,6 +124,6 @@ bool wav_load_as_int16(const char* path, audio_samples* audio){
}else{
result = false;
}
close_file(&fd);
fclose(&fd);
return result;
}