Move load_audio into loading.c for consistency

This commit is contained in:
2018-05-10 07:40:28 -04:00
parent f2aa7420df
commit 5520ec2b2f
5 changed files with 49 additions and 43 deletions

View File

@@ -28,6 +28,7 @@
#define LOADING_H #define LOADING_H
#include "common.h" #include "common.h"
#include "music.h"
#include <jansson.h> #include <jansson.h>
enum ICON_IDS_OFFSET { enum ICON_IDS_OFFSET {
@@ -115,6 +116,7 @@ void sort_by_filename(Entry_List_s * list);
void delete_entry(Entry_s * entry, bool is_file); void delete_entry(Entry_s * entry, bool is_file);
Result load_entries(const char * loading_path, Entry_List_s * list); Result load_entries(const char * loading_path, Entry_List_s * list);
bool load_preview(Entry_List_s list, int * preview_offset); bool load_preview(Entry_List_s list, int * preview_offset);
Result load_audio(Entry_s, audio_s *);
void load_icons_first(Entry_List_s * current_list, bool silent); void load_icons_first(Entry_List_s * current_list, bool silent);
void handle_scrolling(Entry_List_s * list); void handle_scrolling(Entry_List_s * list);
void load_icons_thread(void * void_arg); void load_icons_thread(void * void_arg);

View File

@@ -30,7 +30,6 @@
#include "common.h" #include "common.h"
#include "fs.h" #include "fs.h"
#include "unicode.h" #include "unicode.h"
#include "loading.h"
#include <tremor/ivorbisfile.h> #include <tremor/ivorbisfile.h>
#include <tremor/ivorbiscodec.h> #include <tremor/ivorbiscodec.h>
@@ -48,7 +47,6 @@ typedef struct {
u32 filesize; u32 filesize;
} audio_s; } audio_s;
Result load_audio(Entry_s, audio_s *); void play_audio(audio_s *);
Result play_audio(audio_s *);
#endif #endif

View File

@@ -28,6 +28,7 @@
#include "pp2d/pp2d/pp2d.h" #include "pp2d/pp2d/pp2d.h"
#include "fs.h" #include "fs.h"
#include "unicode.h" #include "unicode.h"
#include "music.h"
#include "draw.h" #include "draw.h"
void delete_entry(Entry_s * entry, bool is_file) void delete_entry(Entry_s * entry, bool is_file)
@@ -458,3 +459,42 @@ bool load_preview(Entry_List_s list, int * preview_offset)
return ret; return ret;
} }
// Initialize the audio struct
Result load_audio(Entry_s entry, audio_s *audio)
{
audio->filesize = load_data("/bgm.ogg", entry, &audio->filebuf);
audio->mix[0] = audio->mix[1] = 1.0f; // Determines volume for the 12 (?) different outputs. See http://smealum.github.io/ctrulib/channel_8h.html#a30eb26f1972cc3ec28370263796c0444
ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
ndspChnSetRate(0, 44100);
ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16); // Tremor outputs ogg files in 16 bit PCM stereo
ndspChnSetMix(0, audio->mix); // See mix comment above
FILE *file = fmemopen(audio->filebuf, audio->filesize, "rb");
if(file != NULL)
{
int e = ov_open(file, &audio->vf, NULL, 0);
if (e < 0)
{
char error[50];
sprintf(error, "Vorbis: %d\n", e);
DEBUG(error);
return MAKERESULT(RL_FATAL, RS_INVALIDARG, RM_APPLICATION, RD_NO_DATA);
}
vorbis_info *vi = ov_info(&audio->vf, -1);
ndspChnSetRate(0, vi->rate);// Set sample rate to what's read from the ogg file
audio->wave_buf[0].nsamples = audio->wave_buf[1].nsamples = vi->rate / 4; // 4 bytes per sample, samples = rate (bytes) / 4
audio->wave_buf[0].status = audio->wave_buf[1].status = NDSP_WBUF_DONE; // Used in play to stop from writing to current buffer
audio->wave_buf[0].data_vaddr = linearAlloc(BUF_TO_READ); // Most vorbis packets should only be 4 KB at most (?) Possibly dangerous assumption
audio->wave_buf[1].data_vaddr = linearAlloc(BUF_TO_READ);
DEBUG("Success!");
return MAKERESULT(RL_SUCCESS, RS_SUCCESS, RM_APPLICATION, RD_SUCCESS);
} else {
DEBUG("File not found!\n");
return MAKERESULT(RL_FATAL, RS_NOTFOUND, RM_APPLICATION, RD_NOT_FOUND);
}
}

View File

@@ -437,6 +437,7 @@ int main(void)
preview_mode = load_preview(*current_list, &preview_offset); preview_mode = load_preview(*current_list, &preview_offset);
audio = calloc(sizeof(audio_s), 1); audio = calloc(sizeof(audio_s), 1);
load_audio(current_list->entries[current_list->selected_entry], audio); load_audio(current_list->entries[current_list->selected_entry], audio);
play_audio(audio);
} }
else else
{ {

View File

@@ -25,9 +25,10 @@
*/ */
#include "music.h" #include "music.h"
#include "loading.h"
// Play a given audio struct // Play a given audio struct
Result play_audio(audio_s *audio) Result update_audio(audio_s *audio)
{ {
long size = audio->wave_buf[audio->buf_pos].nsamples * 4 - audio->data_read; long size = audio->wave_buf[audio->buf_pos].nsamples * 4 - audio->data_read;
char size_info[50] = {0}; char size_info[50] = {0};
@@ -67,48 +68,12 @@ Result play_audio(audio_s *audio)
void thread_audio(void* data) { void thread_audio(void* data) {
audio_s *audio = (audio_s*)data; audio_s *audio = (audio_s*)data;
while(!audio->stop) { while(!audio->stop) {
play_audio(audio); update_audio(audio);
} }
free(audio->filebuf); free(audio->filebuf);
free(audio); free(audio);
} }
// Initialize the audio struct void play_audio(audio_s *audio) {
Result load_audio(Entry_s entry, audio_s *audio) threadCreate(thread_audio, audio, 0x1000, 0x3F, 1, true);
{
audio->filesize = load_data("/bgm.ogg", entry, &audio->filebuf);
audio->mix[0] = audio->mix[1] = 1.0f; // Determines volume for the 12 (?) different outputs. See http://smealum.github.io/ctrulib/channel_8h.html#a30eb26f1972cc3ec28370263796c0444
ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
ndspChnSetRate(0, 44100);
ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16); // Tremor outputs ogg files in 16 bit PCM stereo
ndspChnSetMix(0, audio->mix); // See mix comment above
FILE *file = fmemopen(audio->filebuf, audio->filesize, "rb");
if(file != NULL)
{
int e = ov_open(file, &audio->vf, NULL, 0);
if (e < 0)
{
char error[50];
sprintf(error, "Vorbis: %d\n", e);
DEBUG(error);
return MAKERESULT(RL_FATAL, RS_INVALIDARG, RM_APPLICATION, RD_NO_DATA);
}
vorbis_info *vi = ov_info(&audio->vf, -1);
ndspChnSetRate(0, vi->rate);// Set sample rate to what's read from the ogg file
audio->wave_buf[0].nsamples = audio->wave_buf[1].nsamples = vi->rate / 4; // 4 bytes per sample, samples = rate (bytes) / 4
audio->wave_buf[0].status = audio->wave_buf[1].status = NDSP_WBUF_DONE; // Used in play to stop from writing to current buffer
audio->wave_buf[0].data_vaddr = linearAlloc(BUF_TO_READ); // Most vorbis packets should only be 4 KB at most (?) Possibly dangerous assumption
audio->wave_buf[1].data_vaddr = linearAlloc(BUF_TO_READ);
DEBUG("Success!");
threadCreate(thread_audio, audio, 0x1000, 0x3F, 1, true);
return MAKERESULT(RL_SUCCESS, RS_SUCCESS, RM_APPLICATION, RD_SUCCESS);
} else {
DEBUG("File not found!\n");
return MAKERESULT(RL_FATAL, RS_NOTFOUND, RM_APPLICATION, RD_NOT_FOUND);
}
} }