Homebrew Homebrew Development

cracker

Nyah!
Member
Joined
Aug 24, 2005
Messages
3,619
Trophies
1
XP
2,213
Country
United States
Simple is a relative term. What platform are you targeting? There are so many takes on ogg decoders but without knowing what hardware you want to use it on there is no way to search for an easy solution for you.
 

cracker

Nyah!
Member
Joined
Aug 24, 2005
Messages
3,619
Trophies
1
XP
2,213
Country
United States
Don't quote me on this, but I'm fairly sure he wants to use it on, you know, 3DS. Since this is the 3DS homebrew development section.

There have been OT talks of general programming info so I wanted the clarification. :P

Yeah, I was hoping for one already ported to the 3ds because I'm not experienced with audio enough to port one myself.

By simple, I mean something like this C code I made up that's similar to SoLoud.
Code:
OggSound myGoodOggSound;
OggStream myGoodOggStream;

PlayHandle myPlayHandle1;
PlayHandle myPlayHandle2;

initAudio(OGG);

loadSound(&myGoodOggSound,"sfx.ogg");
loadStream(&myGoodOggStream,"music.ogg");

myPlayHandle1= playSound(myGoodOggSound);
myPlayHandle2 = playStream(myGoodOggStream);

while(1);

If there are no alternatives, I'll try SDL_Mixer. I don't want to though because it can only play one music stream at a time.

LPP has ogg support that you could borrow from. Here is the piece of code that handles the stream decoding.
 
  • Like
Reactions: Deleted User
D

Deleted User

Guest
Yeah, I was hoping for one already ported to the 3ds because I'm not experienced with audio enough to port one myself.

By simple, I mean something like this C code I made up that's similar to SoLoud.
Code:
OggSound myGoodOggSound;
OggStream myGoodOggStream;

PlayHandle myPlayHandle1;
PlayHandle myPlayHandle2;

initAudio(OGG);

loadSound(&myGoodOggSound,"sfx.ogg");
loadStream(&myGoodOggStream,"music.ogg");

myPlayHandle1= playSound(myGoodOggSound);
myPlayHandle2 = playStream(myGoodOggStream);

while(1);

If there are no alternatives, I'll try SDL_Mixer. I don't want to though because it can only play one music stream at a time.
You could also borrow some some of the code from ctrmus. It's licensed under the GPL v3.0.
 
  • Like
Reactions: Deleted User

tgaiu

Active Member
Newcomer
Joined
Sep 10, 2017
Messages
33
Trophies
0
XP
75
Country
Japan
I attempted to play the ogg file using libtremor but failed.
Is there a mistake in the code?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <tremor/ivorbiscodec.h>
#include <tremor/ivorbisfile.h>
#include <3ds.h>

#define MUSIC_CHANNEL 1
#define BUFFER_SIZE 4096
#define STACKSIZE (4 * 1024)

typedef struct {
	
	float rate;
	u32 channels;
	u32 encoding;
	u32 nsamples;
	u32 size;
	char* data;
	bool loop;
	int audiochannel;

	float mix[12];
	ndspInterpType interp;

	OggVorbis_File ovf;
} Music;
Music music;

void load() {
	FILE * file = fopen("example.ogg", "rb");
	if (ov_open(file, &music.ovf, NULL, 0) < 0) {
		printf("ogg vorbis file error\n");
	}
	vorbis_info * vorbisInfo = ov_info(&music.ovf, -1);
	if (vorbisInfo == NULL) {
		printf("could not retrieve ogg audio stream information\n");
	}
	music.rate = (float)vorbisInfo->rate;
	music.channels = (u32)vorbisInfo->channels;
	music.encoding = NDSP_ENCODING_PCM16;
	music.nsamples = (u32)ov_pcm_total(&music.ovf, -1);
	music.size = music.nsamples * music.channels * 2;
	music.audiochannel = 0;
	music.loop = false;
	if (linearSpaceFree() < music.size) {
		printf("not enough linear memory available\n");
	}
	music.data = (char*)linearAlloc(music.size);

	printf("rate:%f\n", music.rate);
	printf("channels:%ld\n", music.channels);
	printf("encoding:%ld\n", music.encoding);
	printf("nsamples:%ld\n", music.nsamples);
	printf("size:%ld\n", music.size);

	int offset = 0;
	int eof = 0;
	int currentSection;

	while (!eof) {
		long ret = ov_read(&music.ovf, &music.data[offset], 4096, &currentSection);
		if (ret == 0) {
			eof = 1;
		}
		else if (ret < 0) {
			ov_clear(&music.ovf);
			linearFree(music.data);
			printf("error in the ogg vorbis stream\n");
		}
		else {
			offset += ret;
		}
	}
	linearFree(&music.ovf);
	ov_clear(&music.ovf);
	fclose(file);
}

int play() {
	if (music.audiochannel == -1) {
		printf("No available audio channel\n");
		return -1;
	}
	ndspChnWaveBufClear(music.audiochannel);
	ndspChnReset(music.audiochannel);
	ndspChnInitParams(music.audiochannel);
	ndspChnSetMix(music.audiochannel, music.mix);
	ndspChnSetInterp(music.audiochannel, music.interp);
	ndspChnSetRate(music.audiochannel, music.rate);
	ndspChnSetFormat(music.audiochannel, NDSP_CHANNELS(music.channels) | NDSP_ENCODING(music.encoding));
	
	ndspWaveBuf waveBuf;

	waveBuf.data_vaddr = music.data;
	waveBuf.nsamples = music.nsamples;
	waveBuf.looping = music.loop;

	DSP_FlushDataCache((u32*)music.data, music.size);

	ndspChnWaveBufAdd(music.audiochannel, &waveBuf);
	return 0;
}


int main() {
	gfxInitDefault();
	consoleInit(GFX_TOP, nullptr);

	load();
	play();
	while (1) {
		hidScanInput();
		u32 kDown = hidKeysDown();
		if (kDown & KEY_START) break;

		gfxFlushBuffers();
		gfxSwapBuffers();
		gspWaitForVBlank();
	}
	gfxExit();
	ndspExit();
	return 0;
}
 

elhobbs

Well-Known Member
Member
Joined
Jul 28, 2008
Messages
1,044
Trophies
1
XP
3,030
Country
United States
I attempted to play the ogg file using libtremor but failed.
Is there a mistake in the code?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <tremor/ivorbiscodec.h>
#include <tremor/ivorbisfile.h>
#include <3ds.h>

#define MUSIC_CHANNEL 1
#define BUFFER_SIZE 4096
#define STACKSIZE (4 * 1024)

typedef struct {
   
    float rate;
    u32 channels;
    u32 encoding;
    u32 nsamples;
    u32 size;
    char* data;
    bool loop;
    int audiochannel;

    float mix[12];
    ndspInterpType interp;

    OggVorbis_File ovf;
} Music;
Music music;

void load() {
    FILE * file = fopen("example.ogg", "rb");
    if (ov_open(file, &music.ovf, NULL, 0) < 0) {
        printf("ogg vorbis file error\n");
    }
    vorbis_info * vorbisInfo = ov_info(&music.ovf, -1);
    if (vorbisInfo == NULL) {
        printf("could not retrieve ogg audio stream information\n");
    }
    music.rate = (float)vorbisInfo->rate;
    music.channels = (u32)vorbisInfo->channels;
    music.encoding = NDSP_ENCODING_PCM16;
    music.nsamples = (u32)ov_pcm_total(&music.ovf, -1);
    music.size = music.nsamples * music.channels * 2;
    music.audiochannel = 0;
    music.loop = false;
    if (linearSpaceFree() < music.size) {
        printf("not enough linear memory available\n");
    }
    music.data = (char*)linearAlloc(music.size);

    printf("rate:%f\n", music.rate);
    printf("channels:%ld\n", music.channels);
    printf("encoding:%ld\n", music.encoding);
    printf("nsamples:%ld\n", music.nsamples);
    printf("size:%ld\n", music.size);

    int offset = 0;
    int eof = 0;
    int currentSection;

    while (!eof) {
        long ret = ov_read(&music.ovf, &music.data[offset], 4096, &currentSection);
        if (ret == 0) {
            eof = 1;
        }
        else if (ret < 0) {
            ov_clear(&music.ovf);
            linearFree(music.data);
            printf("error in the ogg vorbis stream\n");
        }
        else {
            offset += ret;
        }
    }
    linearFree(&music.ovf);
    ov_clear(&music.ovf);
    fclose(file);
}

int play() {
    if (music.audiochannel == -1) {
        printf("No available audio channel\n");
        return -1;
    }
    ndspChnWaveBufClear(music.audiochannel);
    ndspChnReset(music.audiochannel);
    ndspChnInitParams(music.audiochannel);
    ndspChnSetMix(music.audiochannel, music.mix);
    ndspChnSetInterp(music.audiochannel, music.interp);
    ndspChnSetRate(music.audiochannel, music.rate);
    ndspChnSetFormat(music.audiochannel, NDSP_CHANNELS(music.channels) | NDSP_ENCODING(music.encoding));
   
    ndspWaveBuf waveBuf;

    waveBuf.data_vaddr = music.data;
    waveBuf.nsamples = music.nsamples;
    waveBuf.looping = music.loop;

    DSP_FlushDataCache((u32*)music.data, music.size);

    ndspChnWaveBufAdd(music.audiochannel, &waveBuf);
    return 0;
}


int main() {
    gfxInitDefault();
    consoleInit(GFX_TOP, nullptr);

    load();
    play();
    while (1) {
        hidScanInput();
        u32 kDown = hidKeysDown();
        if (kDown & KEY_START) break;

        gfxFlushBuffers();
        gfxSwapBuffers();
        gspWaitForVBlank();
    }
    gfxExit();
    ndspExit();
    return 0;
}
I don’t see a call to ndspInit?
 

tgaiu

Active Member
Newcomer
Joined
Sep 10, 2017
Messages
33
Trophies
0
XP
75
Country
Japan
I don’t see a call to ndspInit?
I made those improvements but got errors.
Code:
[HW.Memory <Error> core\memory.cpp:GetPointer:283: unknown GetPointer @ 0x00000000
[Audio.DSP <Warning> audio_core\hle\source.cpp:DequeueBuffer:312: source_id=0 buffer_id=20 length=0: Invalid physical address 0x00000000
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <tremor/ivorbiscodec.h>
#include <tremor/ivorbisfile.h>
#include <3ds.h>

#define MUSIC_CHANNEL 1
#define BUFFER_SIZE 4096
#define STACKSIZE (4 * 1024)

typedef struct {

	float rate;
	u32 channels;
	u32 encoding;
	u32 nsamples;
	u32 size;
	u8* data;
	bool loop;
	int audiochannel;

	float mix[12];
	ndspInterpType interp;

	OggVorbis_File ovf;
} Music;
Music music;

void load() {
	FILE * file = fopen("example.ogg", "rb");
	if (ov_open(file, &music.ovf, NULL, 0) < 0) {
		printf("ogg vorbis file error\n");
	}
	vorbis_info * vorbisInfo = ov_info(&music.ovf, -1);
	if (vorbisInfo == NULL) {
		printf("could not retrieve ogg audio stream information\n");
	}
	music.rate = (float)vorbisInfo->rate;
	music.channels = (u32)vorbisInfo->channels;
	music.encoding = NDSP_ENCODING_PCM16;
	music.nsamples = (u32)ov_pcm_total(&music.ovf, -1);
	music.size = music.nsamples * music.channels * 2;
	music.audiochannel = 0;
	music.loop = false;
	if (linearSpaceFree() < music.size) {
		printf("not enough linear memory available\n");
	music.data = static_cast<u8*>(linearAlloc(music.size));

	printf("rate:%f\n", music.rate);
	printf("channels:%ld\n", music.channels);
	printf("encoding:%ld\n", music.encoding);
	printf("nsamples:%ld\n", music.nsamples);
	printf("size:%ld\n", music.size);

	int offset = 0;
	int eof = 0;
	int currentSection;

	while (!eof) {
		long ret = ov_read(&music.ovf, &music.data[offset], 4096, &currentSection);
		if (ret == 0) {
			eof = 1;
		}
		else if (ret < 0) {
			ov_clear(&music.ovf);
			linearFree(music.data);
			printf("error in the ogg vorbis stream\n");
		}
		else {
			offset += ret;
		}
	}
	linearFree(&music.ovf);
	ov_clear(&music.ovf);
	fclose(file);
}

int play() {
	if (music.audiochannel == -1) {
		printf("No available audio channel\n");
		return -1;
	}
	ndspChnWaveBufClear(music.audiochannel);
	ndspChnReset(music.audiochannel);
	ndspChnInitParams(music.audiochannel);
	ndspChnSetMix(music.audiochannel, music.mix);
	ndspChnSetInterp(music.audiochannel, music.interp);
	ndspChnSetRate(music.audiochannel, music.rate);
	ndspChnSetFormat(music.audiochannel, NDSP_CHANNELS(music.channels) | NDSP_ENCODING(music.encoding));

	ndspWaveBuf waveBuf;
	memset(&waveBuf, 0, sizeof(ndspWaveBuf));
	waveBuf.data_vaddr = music.data;
	waveBuf.nsamples = music.nsamples;
	waveBuf.looping = music.loop;
	waveBuf.status = NDSP_WBUF_FREE;

	DSP_FlushDataCache(music.data, music.size);

	ndspChnWaveBufAdd(music.audiochannel, &waveBuf);
	return 0;
}


int main() {
	gfxInitDefault();
	consoleInit(GFX_TOP, nullptr);

	ndspInit();
	ndspSetOutputMode(NDSP_OUTPUT_STEREO);
	ndspSetOutputCount(1);

	load();
	play();
	while (aptMainLoop()) {
		hidScanInput();
		u32 keys = hidKeysDown();
		if (keys & KEY_START)
			break;

		gfxFlushBuffers();
		gfxSwapBuffers();
		gspWaitForVBlank();
	}
	ndspChnWaveBufClear(music.audiochannel);
	gfxExit();
	ndspExit();
	return 0;
}
 

elhobbs

Well-Known Member
Member
Joined
Jul 28, 2008
Messages
1,044
Trophies
1
XP
3,030
Country
United States
I made those improvements but got errors.
Code:
[HW.Memory <Error> core\memory.cpp:GetPointer:283: unknown GetPointer @ 0x00000000
[Audio.DSP <Warning> audio_core\hle\source.cpp:DequeueBuffer:312: source_id=0 buffer_id=20 length=0: Invalid physical address 0x00000000
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <tremor/ivorbiscodec.h>
#include <tremor/ivorbisfile.h>
#include <3ds.h>

#define MUSIC_CHANNEL 1
#define BUFFER_SIZE 4096
#define STACKSIZE (4 * 1024)

typedef struct {

    float rate;
    u32 channels;
    u32 encoding;
    u32 nsamples;
    u32 size;
    u8* data;
    bool loop;
    int audiochannel;

    float mix[12];
    ndspInterpType interp;

    OggVorbis_File ovf;
} Music;
Music music;

void load() {
    FILE * file = fopen("example.ogg", "rb");
    if (ov_open(file, &music.ovf, NULL, 0) < 0) {
        printf("ogg vorbis file error\n");
    }
    vorbis_info * vorbisInfo = ov_info(&music.ovf, -1);
    if (vorbisInfo == NULL) {
        printf("could not retrieve ogg audio stream information\n");
    }
    music.rate = (float)vorbisInfo->rate;
    music.channels = (u32)vorbisInfo->channels;
    music.encoding = NDSP_ENCODING_PCM16;
    music.nsamples = (u32)ov_pcm_total(&music.ovf, -1);
    music.size = music.nsamples * music.channels * 2;
    music.audiochannel = 0;
    music.loop = false;
    if (linearSpaceFree() < music.size) {
        printf("not enough linear memory available\n");
    music.data = static_cast<u8*>(linearAlloc(music.size));

    printf("rate:%f\n", music.rate);
    printf("channels:%ld\n", music.channels);
    printf("encoding:%ld\n", music.encoding);
    printf("nsamples:%ld\n", music.nsamples);
    printf("size:%ld\n", music.size);

    int offset = 0;
    int eof = 0;
    int currentSection;

    while (!eof) {
        long ret = ov_read(&music.ovf, &music.data[offset], 4096, &currentSection);
        if (ret == 0) {
            eof = 1;
        }
        else if (ret < 0) {
            ov_clear(&music.ovf);
            linearFree(music.data);
            printf("error in the ogg vorbis stream\n");
        }
        else {
            offset += ret;
        }
    }
    linearFree(&music.ovf);
    ov_clear(&music.ovf);
    fclose(file);
}

int play() {
    if (music.audiochannel == -1) {
        printf("No available audio channel\n");
        return -1;
    }
    ndspChnWaveBufClear(music.audiochannel);
    ndspChnReset(music.audiochannel);
    ndspChnInitParams(music.audiochannel);
    ndspChnSetMix(music.audiochannel, music.mix);
    ndspChnSetInterp(music.audiochannel, music.interp);
    ndspChnSetRate(music.audiochannel, music.rate);
    ndspChnSetFormat(music.audiochannel, NDSP_CHANNELS(music.channels) | NDSP_ENCODING(music.encoding));

    ndspWaveBuf waveBuf;
    memset(&waveBuf, 0, sizeof(ndspWaveBuf));
    waveBuf.data_vaddr = music.data;
    waveBuf.nsamples = music.nsamples;
    waveBuf.looping = music.loop;
    waveBuf.status = NDSP_WBUF_FREE;

    DSP_FlushDataCache(music.data, music.size);

    ndspChnWaveBufAdd(music.audiochannel, &waveBuf);
    return 0;
}


int main() {
    gfxInitDefault();
    consoleInit(GFX_TOP, nullptr);

    ndspInit();
    ndspSetOutputMode(NDSP_OUTPUT_STEREO);
    ndspSetOutputCount(1);

    load();
    play();
    while (aptMainLoop()) {
        hidScanInput();
        u32 keys = hidKeysDown();
        if (keys & KEY_START)
            break;

        gfxFlushBuffers();
        gfxSwapBuffers();
        gspWaitForVBlank();
    }
    ndspChnWaveBufClear(music.audiochannel);
    gfxExit();
    ndspExit();
    return 0;
}
Check if linearAlloc succeeded?
Why are you calling linearFree in the load function - on non “linear” region memory(music.ovf)?
 

tgaiu

Active Member
Newcomer
Joined
Sep 10, 2017
Messages
33
Trophies
0
XP
75
Country
Japan
Check if linearAlloc succeeded?
Why are you calling linearFree in the load function - on non “linear” region memory(music.ovf)?
I made a mistake in placing linearFree.
linearAlloc seemed to be successful.
It seems that gspWaitForVBlank () is causing problems, but is it OK to delete this?
 

niBBasian

Member
Newcomer
Joined
Nov 27, 2017
Messages
7
Trophies
0
Age
28
XP
63
Country
Singapore
guys i know this sounds silly but i am new to the modding scene and also to gba temp . BUT I DONT KNOW HOW TO POST A FU#%ING question!!!!!!!!!!!!!!!HOW DO U POST ON GBATEMP NET!!!!!!!!!!!!!!!!!!
 

NatsumiX

Well-Known Member
Newcomer
Joined
Oct 9, 2017
Messages
69
Trophies
0
XP
110
Country
United States
guys i know this sounds silly but i am new to the modding scene and also to gba temp . BUT I DONT KNOW HOW TO POST A FU#%ING question!!!!!!!!!!!!!!!HOW DO U POST ON GBATEMP NET!!!!!!!!!!!!!!!!!!
aoyNMDx.png
 

tgaiu

Active Member
Newcomer
Joined
Sep 10, 2017
Messages
33
Trophies
0
XP
75
Country
Japan
I made those improvements but got errors.
Code:
[HW.Memory <Error> core\memory.cpp:GetPointer:283: unknown GetPointer @ 0x00000000
[Audio.DSP <Warning> audio_core\hle\source.cpp:DequeueBuffer:312: source_id=0 buffer_id=20 length=0: Invalid physical address 0x00000000
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <tremor/ivorbiscodec.h>
#include <tremor/ivorbisfile.h>
#include <3ds.h>

#define MUSIC_CHANNEL 1
#define BUFFER_SIZE 4096
#define STACKSIZE (4 * 1024)

typedef struct {

    float rate;
    u32 channels;
    u32 encoding;
    u32 nsamples;
    u32 size;
    u8* data;
    bool loop;
    int audiochannel;

    float mix[12];
    ndspInterpType interp;

    OggVorbis_File ovf;
} Music;
Music music;

void load() {
    FILE * file = fopen("example.ogg", "rb");
    if (ov_open(file, &music.ovf, NULL, 0) < 0) {
        printf("ogg vorbis file error\n");
    }
    vorbis_info * vorbisInfo = ov_info(&music.ovf, -1);
    if (vorbisInfo == NULL) {
        printf("could not retrieve ogg audio stream information\n");
    }
    music.rate = (float)vorbisInfo->rate;
    music.channels = (u32)vorbisInfo->channels;
    music.encoding = NDSP_ENCODING_PCM16;
    music.nsamples = (u32)ov_pcm_total(&music.ovf, -1);
    music.size = music.nsamples * music.channels * 2;
    music.audiochannel = 0;
    music.loop = false;
    if (linearSpaceFree() < music.size) {
        printf("not enough linear memory available\n");
    music.data = static_cast<u8*>(linearAlloc(music.size));

    printf("rate:%f\n", music.rate);
    printf("channels:%ld\n", music.channels);
    printf("encoding:%ld\n", music.encoding);
    printf("nsamples:%ld\n", music.nsamples);
    printf("size:%ld\n", music.size);

    int offset = 0;
    int eof = 0;
    int currentSection;

    while (!eof) {
        long ret = ov_read(&music.ovf, &music.data[offset], 4096, &currentSection);
        if (ret == 0) {
            eof = 1;
        }
        else if (ret < 0) {
            ov_clear(&music.ovf);
            linearFree(music.data);
            printf("error in the ogg vorbis stream\n");
        }
        else {
            offset += ret;
        }
    }
    linearFree(&music.ovf);
    ov_clear(&music.ovf);
    fclose(file);
}

int play() {
    if (music.audiochannel == -1) {
        printf("No available audio channel\n");
        return -1;
    }
    ndspChnWaveBufClear(music.audiochannel);
    ndspChnReset(music.audiochannel);
    ndspChnInitParams(music.audiochannel);
    ndspChnSetMix(music.audiochannel, music.mix);
    ndspChnSetInterp(music.audiochannel, music.interp);
    ndspChnSetRate(music.audiochannel, music.rate);
    ndspChnSetFormat(music.audiochannel, NDSP_CHANNELS(music.channels) | NDSP_ENCODING(music.encoding));

    ndspWaveBuf waveBuf;
    memset(&waveBuf, 0, sizeof(ndspWaveBuf));
    waveBuf.data_vaddr = music.data;
    waveBuf.nsamples = music.nsamples;
    waveBuf.looping = music.loop;
    waveBuf.status = NDSP_WBUF_FREE;

    DSP_FlushDataCache(music.data, music.size);

    ndspChnWaveBufAdd(music.audiochannel, &waveBuf);
    return 0;
}


int main() {
    gfxInitDefault();
    consoleInit(GFX_TOP, nullptr);

    ndspInit();
    ndspSetOutputMode(NDSP_OUTPUT_STEREO);
    ndspSetOutputCount(1);

    load();
    play();
    while (aptMainLoop()) {
        hidScanInput();
        u32 keys = hidKeysDown();
        if (keys & KEY_START)
            break;

        gfxFlushBuffers();
        gfxSwapBuffers();
        gspWaitForVBlank();
    }
    ndspChnWaveBufClear(music.audiochannel);
    gfxExit();
    ndspExit();
    return 0;
}

I tried a variety of hands, but I get a similar error.
Please let me know if you know the solution.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <tremor/ivorbiscodec.h>
#include <tremor/ivorbisfile.h>
#include <3ds.h>

typedef struct {
	float rate;
	u32 channels;
	u32 encoding;
	u32 nsamples;
	u32 size;
	char* data;
	bool loop;
	int audiochannel;
	float mix[12];
	ndspInterpType interp;
	OggVorbis_File ovf;
} Music;
Music music;

void load() {
	FILE * file = fopen("test.ogg", "rb");
	if (ov_open(file, &music.ovf, NULL, 0) < 0) {
		printf("ogg vorbis file error\n");
	}
	vorbis_info * vorbisInfo = ov_info(&music.ovf, -1);
	if (vorbisInfo == NULL) {
		printf("could not retrieve ogg audio stream information\n");
	}
	music.rate = (float)vorbisInfo->rate;
	music.channels = (u32)vorbisInfo->channels;
	music.encoding = NDSP_ENCODING_PCM16;
	music.nsamples = (u32)ov_pcm_total(&music.ovf, -1);
	music.size = music.nsamples * music.channels * 2;
	music.audiochannel = 0;
	music.loop = false;
	music.interp = NDSP_INTERP_NONE;
	if (linearSpaceFree() < music.size) {
		printf("not enough linear memory available\n");
	}
	music.data = static_cast<char*>(linearAlloc(music.size));
	if (!music.data){
		fclose(file);
	}
	printf("buffersize:%d\n", linearGetSize(music.data));
	printf("rate:%f\n", music.rate);
	printf("channels:%ld\n", music.channels);
	printf("encoding:%ld\n", music.encoding);
	printf("nsamples:%ld\n", music.nsamples);
	printf("size:%ld\n", music.size);

	// Decoding loop
	int offset = 0;
	int eof = 0;
	int currentSection = 0;
	int readSize = 0;
	int comSize = 0;
	char* tmpBuffer = static_cast<char*>(linearAlloc(music.size));
	while (1) {
		readSize = ov_read(&music.ovf, (char*)tmpBuffer, 4096, &currentSection);
		if (readSize == 0) break; //EOF
		memcpy(music.data + comSize, tmpBuffer, readSize);
		comSize += readSize;
	}
	fclose(file);
}

int play() {
	if (music.audiochannel == -1) {
		printf("No available audio channel\n");
		return -1;
	}
	ndspChnReset(music.audiochannel);
	ndspChnInitParams(music.audiochannel);
	ndspChnSetMix(music.audiochannel, music.mix);
	ndspChnSetInterp(music.audiochannel, music.interp);
	ndspChnSetRate(music.audiochannel, music.rate);
	ndspChnSetFormat(music.audiochannel, NDSP_CHANNELS(music.channels) | NDSP_ENCODING(music.encoding));
	
	ndspWaveBuf waveBuf;
	memset(&waveBuf, 0, sizeof(ndspWaveBuf));
	waveBuf.data_vaddr = (const void*)music.data;
	waveBuf.nsamples = music.nsamples;
	waveBuf.looping = music.loop;
	waveBuf.status = NDSP_WBUF_FREE;
	DSP_FlushDataCache(music.data, music.size);
	ndspChnWaveBufAdd(music.audiochannel, &waveBuf);
	return 0;
}


int main() {
	gfxInitDefault();
	consoleInit(GFX_TOP, nullptr);

	ndspInit();
	ndspSetOutputMode(NDSP_OUTPUT_STEREO);
	ndspSetOutputCount(1);

	load();
	play();
	while (aptMainLoop()) {
		hidScanInput();
		u32 keys = hidKeysDown();
		if (keys & KEY_START)
			break;
		gfxFlushBuffers();
		gfxSwapBuffers();
		gspWaitForVBlank();
	}
	linearFree(&music.ovf);
	ov_clear(&music.ovf);
	ndspChnWaveBufClear(music.audiochannel);
	gfxExit();
	ndspExit();
	return 0;
}
 

cracker

Nyah!
Member
Joined
Aug 24, 2005
Messages
3,619
Trophies
1
XP
2,213
Country
United States
That's not necessarily a problem with your code. Even legit games have that error in Citra. You will need to use the real hardware to test it.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Xdqwerty @ Xdqwerty:
    also gonna install twilight menu in my r4 flashcard
  • Psionic Roshambo @ Psionic Roshambo:
    One thing that just occurred to me.... The sound on the 2600 sucked less back then the harsh sound we hear now is from infinitely better speakers we have now, back when the 2600 was new speakers produced a almost muffled sound, like CRTs made old graphics look slightly better.
  • Psionic Roshambo @ Psionic Roshambo:
    I wonder if I could recommend that to some emulation devs that perhaps the sound could use some smoothing out to simulate those old TVs
  • Psionic Roshambo @ Psionic Roshambo:
    I think a few of the early systems could benefit from that, at least up to the 8 bit generation, by the 16 bit generation I think TVs had gotten a lot better in almost every way
  • Xdqwerty @ Xdqwerty:
    i dont have an sd card adapter but I have an usb sd card adapter
  • K3Nv2 @ K3Nv2:
    Old people games
  • Xdqwerty @ Xdqwerty:
    its not the one that comes with the r4
  • Xdqwerty @ Xdqwerty:
    doesnt work (my flashcard is from r4isdhc.com)
  • Xdqwerty @ Xdqwerty:
    might install ysmenu first
  • Psionic Roshambo @ Psionic Roshambo:
    Try Wood firmware
  • Psionic Roshambo @ Psionic Roshambo:
    For your R4
  • Psionic Roshambo @ Psionic Roshambo:
    It's old but it's the best firmware out for DS stuff
  • Xdqwerty @ Xdqwerty:
    it says it only works for the original R4, R4i Gold (r4ids.cn), R4iDSN (r4idsn.com) and Acekard R.P.G.
  • Xdqwerty @ Xdqwerty:
    nvm it does support mine
  • Xdqwerty @ Xdqwerty:
    but why choose it over ysmenu @Psionic Roshambo?
  • Xdqwerty @ Xdqwerty:
    bc im stupid?
  • Xdqwerty @ Xdqwerty:
    yea ik im stupid
  • Xdqwerty @ Xdqwerty:
    good night
  • Psionic Roshambo @ Psionic Roshambo:
    Just give it a try, but honestly if you have a 3DS you can play DS games without a card just off the internal SD card
  • Psionic Roshambo @ Psionic Roshambo:
    Slightly slower loading but a bit more convenient
  • BakerMan @ BakerMan:
    guys, my fuckin headphones have an out of place speaker
  • K3Nv2 @ K3Nv2:
    Did you try wearing them?
    B @ btjunior: @Xdqwerty 16