Skip to content
This repository has been archived by the owner on Oct 29, 2022. It is now read-only.

Commit

Permalink
Now dsp::DSP service works near to perfect!
Browse files Browse the repository at this point in the history
Now dsp::DSP service works near to perfect!
  • Loading branch information
Rinnegatamante committed Dec 13, 2015
1 parent ef65127 commit 94a67c5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 28 deletions.
Binary file modified libctru/lib/libctru.a
Binary file not shown.
2 changes: 1 addition & 1 deletion libctru/source/ndsp/ndsp-channel.c
Expand Up @@ -37,7 +37,7 @@ typedef struct

} ndspChnSt;

static ndspChnSt ndspChn[24];
ndspChnSt ndspChn[24];

void ndspChnReset(int id)
{
Expand Down
2 changes: 1 addition & 1 deletion libctru/source/ndsp/ndsp.c
Expand Up @@ -484,7 +484,7 @@ Result ndspInit(void)
rc = svcCreateEvent(&sleepEvent, 0);
if (R_FAILED(rc)) goto _fail2;

rc = svcCreateThread(&ndspThread, ndspThreadMain, 0x0, (u32*)(&ndspThreadStack[NDSP_THREAD_STACK_SIZE/8]), 0x31, -2);
rc = svcCreateThread(&ndspThread, ndspThreadMain, 0x0, (u32*)(&ndspThreadStack[NDSP_THREAD_STACK_SIZE/8]), 0x18, 1);
if (R_FAILED(rc)) goto _fail3;

aptHook(&aptCookie, ndspAptHook, NULL);
Expand Down
29 changes: 27 additions & 2 deletions source/include/luaAudio.h
Expand Up @@ -31,6 +31,7 @@
#- Special thanks to Aurelio for testing, bug-fixing and various help with codes and implementations -------------------#
#-----------------------------------------------------------------------------------------------------------------------*/


// Additional encoding
typedef enum{
CSND_ENCODING_VORBIS = 4
Expand All @@ -42,6 +43,30 @@ struct PurgeTable{
PurgeTable* next;
};

typedef struct
{
u32 flags;

LightLock lock;
u16 syncCount, waveBufSeqPos;
u32 samplePos;

ndspWaveBuf* waveBuf;
u16 wavBufCount, wavBufIdNext;

bool playing;
u8 interpType, iirFilterType;

u16 format;
u16 wavBufSeq;

float rate;
float mix[12];

u16 adpcmCoefs[16];

} ndspChnSt;

struct Music{
u32 magic;
u32 samplerate;
Expand All @@ -50,14 +75,14 @@ struct Music{
u8* audiobuf2;
ndspWaveBuf* wavebuf;
ndspWaveBuf* wavebuf2;
u16 lastCheck;
ndspChnSt* savestate;
u32 lastCheck;
u32 size;
u32 mem_size;
Handle sourceFile;
u32 startRead;
char author[256];
char title[256];
u32 resumeSample;
u64 tick;
bool isPlaying;
u32 ch;
Expand Down
67 changes: 43 additions & 24 deletions source/luaSound.cpp
Expand Up @@ -48,6 +48,7 @@
#define VariableRegister(lua, value) do { lua_pushinteger(lua, value); lua_setglobal (lua, stringify(value)); } while(0)
#define STREAM_MAX_ALLOC 524288

extern ndspChnSt ndspChn[24];
extern bool audioChannels[32];
extern bool isNinjhax2;
extern bool csndAccess;
Expand Down Expand Up @@ -856,21 +857,17 @@ void streamOGG_DSP(void* arg){
// Create a new block for DSP service
u32 bytesRead;
src->wavebuf2 = (ndspWaveBuf*)calloc(1,sizeof(ndspWaveBuf));
createDspBlock(src->wavebuf2, src->bytepersample, src->mem_size, 0, (u32*)src->audiobuf);
createDspBlock(src->wavebuf2, src->bytepersample, src->mem_size, false, (u32*)src->audiobuf);
populatePurgeTable(src, src->wavebuf2);
ndspChnWaveBufAdd(src->ch, src->wavebuf2);
FSFILE_Read(src->sourceFile, &bytesRead, src->audio_pointer, src->audiobuf, src->mem_size);
src->audio_pointer = src->audio_pointer + src->mem_size;

// Decoding Vorbis audiobuffer
int i = 0;
while(!eof){
long ret=ov_read(vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
long ret=ov_read(vf,(char*)&src->audiobuf[i],2048,0,2,1,&current_section);
if (ret == 0) eof=1;
else{

// Copying decoded block to PCM16 audiobuffer
memcpy(&src->audiobuf[i],pcmout,ret);
i = i + ret;
if ((i >= src->mem_size)) break;

Expand Down Expand Up @@ -1092,7 +1089,7 @@ void streamWAV_DSP(void* arg){
// Create a new block for DSP service
u32 bytesRead;
src->wavebuf2 = (ndspWaveBuf*)calloc(1,sizeof(ndspWaveBuf));
createDspBlock(src->wavebuf2, src->bytepersample, src->mem_size, 0, (u32*)src->audiobuf);
createDspBlock(src->wavebuf2, src->bytepersample, src->mem_size, false, (u32*)src->audiobuf);
populatePurgeTable(src, src->wavebuf2);
ndspChnWaveBufAdd(src->ch, src->wavebuf2);
FSFILE_Read(src->sourceFile, &bytesRead, src->audio_pointer, src->audiobuf, src->mem_size);
Expand Down Expand Up @@ -1220,7 +1217,9 @@ static int lua_openogg(lua_State *L)
i = 0;
if (mem_size){
songFile->mem_size = songFile->size;
while (songFile->mem_size > STREAM_MAX_ALLOC){
u32 BUFFER_SIZE = STREAM_MAX_ALLOC;
if (songFile->audiotype == 2) BUFFER_SIZE = BUFFER_SIZE * 10; // Temporary patch for stereo files
while (songFile->mem_size > BUFFER_SIZE){
if ((songFile->mem_size % 2) == 1) songFile->mem_size++;
songFile->mem_size = songFile->mem_size / 2;
}
Expand All @@ -1233,8 +1232,9 @@ static int lua_openogg(lua_State *L)
songFile->mem_size = 0;
songFile->audiobuf2 = NULL;
}
int offs = 0;
while(!eof){
long ret=ov_read(vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
long ret=ov_read(vf,(char*)&songFile->audiobuf[offs],2048,0,2,1,&current_section);
if (ret == 0) eof=1;
else if (ret < 0) {

Expand All @@ -1244,9 +1244,8 @@ static int lua_openogg(lua_State *L)
} else {

// Copying decoded block to PCM16 audiobuffer
memcpy(&songFile->audiobuf[i],pcmout,ret);
i = i + ret;
if ((mem_size) && (i >= songFile->mem_size)) break;
offs = offs + ret;
if ((mem_size) && (offs >= songFile->mem_size)) break;

}
}
Expand Down Expand Up @@ -1364,7 +1363,7 @@ static int lua_openwav(lua_State *L)
songFile->isPlaying = false;
songFile->sourceFile = fileHandle;
if (mem_size){
songFile->mem_size = (size-(pos+4));
songFile->mem_size = size-(pos+4);
while (songFile->mem_size > STREAM_MAX_ALLOC){
if ((songFile->mem_size % 2) == 1) songFile->mem_size++;
songFile->mem_size = songFile->mem_size / 2;
Expand All @@ -1375,7 +1374,7 @@ static int lua_openwav(lua_State *L)
songFile->audiobuf2 = (u8*)linearAlloc(songFile->mem_size);
FSFILE_Read(fileHandle, &bytesRead, songFile->startRead, songFile->audiobuf, songFile->mem_size);
songFile->audio_pointer = songFile->startRead + songFile->mem_size;
songFile->size = size - (pos+4);
songFile->size = size;
}else{
songFile->audiobuf = (u8*)linearAlloc(size-(pos+4));
FSFILE_Read(fileHandle, &bytesRead, pos+4, songFile->audiobuf, size-(pos+4));
Expand Down Expand Up @@ -1496,6 +1495,7 @@ static int lua_openaiff(lua_State *L)
stbp = songFile->mem_size;
}else{
songFile->audiobuf = (u8*)linearAlloc(songFile->size);
songFile->audiobuf2 = NULL;
FSFILE_Read(fileHandle, &bytesRead, pos+4, songFile->audiobuf, songFile->size);
stbp = songFile->size;
}
Expand Down Expand Up @@ -1584,7 +1584,7 @@ static int lua_play(lua_State *L)
ndspChnSetRate(ch, float(src->samplerate));
ndspChnSetFormat(ch, raw_format);
ndspWaveBuf* waveBuf = (ndspWaveBuf*)calloc(1, sizeof(ndspWaveBuf));
if (src->mem_size > 0) createDspBlock(waveBuf, src->bytepersample, src->mem_size, 0, (u32*)src->audiobuf);
if (src->mem_size > 0) createDspBlock(waveBuf, src->bytepersample, src->mem_size, false, (u32*)src->audiobuf);
else createDspBlock(waveBuf, src->bytepersample, src->size, loop, (u32*)src->audiobuf);
src->blocks = NULL;
populatePurgeTable(src, waveBuf);
Expand All @@ -1603,7 +1603,7 @@ static int lua_play(lua_State *L)
u32 *threadStack = (u32*)memalign(32, 8192);
src->thread = threadStack;
svcSignalEvent(updateStream);
Result ret = svcCreateThread(&streamThread, streamFunction, (u32)src, &threadStack[2048], 0x18, 1);
Result ret = svcCreateThread(&streamThread, streamFunction, (u32)src, &threadStack[2048], 0x18, 0);
}

return 0;
Expand Down Expand Up @@ -1750,7 +1750,6 @@ static int lua_closesong(lua_State *L)

// Purging everything
purgeTable(src->blocks);
ndspChnReset(src->ch);
ndspChnWaveBufClear(src->ch);
audioChannels[src->ch] = false;
linearFree(src->audiobuf);
Expand Down Expand Up @@ -1840,10 +1839,18 @@ static int lua_pause(lua_State *L)
#ifndef SKIP_ERROR_HANDLING
if (src->magic != 0x4C534E44) return luaL_error(L, "attempt to access wrong memory block type");
#endif
src->resumeSample = ndspChnGetSamplePos(src->ch);
ndspChnWaveBufClear(src->ch);
src->isPlaying = false;
src->tick = (osGetTime()-src->tick);
if (src->isPlaying){
ndspChnSt* chn = &ndspChn[src->ch];
LightLock_Lock(&chn->lock);
src->savestate = (ndspChnSt*)malloc(sizeof(ndspChnSt));
memcpy(src->savestate,chn,sizeof(ndspChnSt));
LightLock_Unlock(&chn->lock);
u32 samplePos = ndspChnGetSamplePos(src->ch);
ndspChnWaveBufClear(src->ch);
src->savestate->waveBuf->offset = samplePos; // Think it as a temp variable
src->isPlaying = false;
src->tick = (osGetTime()-src->tick);
}
return 0;
}

Expand All @@ -1857,9 +1864,21 @@ static int lua_resume(lua_State *L)
#ifndef SKIP_ERROR_HANDLING
if (src->magic != 0x4C534E44) return luaL_error(L, "attempt to access wrong memory block type");
#endif
ndspChnWaveBufAdd(src->ch, src->wavebuf);
src->isPlaying = true;
src->tick = (osGetTime()-src->tick);
if (!src->isPlaying){
ndspChnSt* chn = &ndspChn[src->ch];
ndspWaveBuf* next = src->savestate->waveBuf->next;
u32 samplePos = src->savestate->waveBuf->offset;
u8* audiobuf = (u8*)src->savestate->waveBuf->data_vaddr;
src->savestate->waveBuf->data_vaddr = (void*)&audiobuf[samplePos * 2];
src->savestate->waveBuf->nsamples = src->savestate->waveBuf->nsamples - samplePos;
src->savestate->waveBuf->offset = 0;
ndspChnWaveBufAdd(src->ch, src->savestate->waveBuf);
ndspChnWaveBufAdd(src->ch, next);
free(src->savestate);
src->lastCheck = 0;
src->isPlaying = true;
src->tick = (osGetTime()-src->tick);
}
return 0;
}

Expand Down

0 comments on commit 94a67c5

Please sign in to comment.