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,032
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,032
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
  • BakerMan @ BakerMan:
    ubisoft should #stopkillinggames ngl
  • Badcatalex @ Badcatalex:
    sony should #stopkillinggames
  • Badcatalex @ Badcatalex:
    they killed LittleBigPlanet online, which was the main core of every LBP game
  • BakerMan @ BakerMan:
    for real
  • BakerMan @ BakerMan:
    at least with them, it was because of the DDOS attacks, ubisoft was just scummy
  • BakerMan @ BakerMan:
    fuck ubisoft, and fuck activision
    +1
  • realtimesave @ realtimesave:
    Nintendo needs to release a new console, switch is getting such shitty little games lately lol it's pathetic
  • Purple_Heart @ Purple_Heart:
    Lmao a new flashcart... The Unlock Switch... I knew it's not fake xD
    +1
  • NinStar @ NinStar:
    A new consoles won't solve that problem
  • NinStar @ NinStar:
    It will actually make it worse
  • The Real Jdbye @ The Real Jdbye:
    well actually
    a new console won't do anything right now, because the games are still in development, that's why there are few games being released
  • The Real Jdbye @ The Real Jdbye:
    it won't make the games finish any faster
  • Veho @ Veho:
    2/3rds of launch titles for the Switch 2 will just be lazy ports of Switch games anyway.
  • The Real Jdbye @ The Real Jdbye:
    probably
  • The Real Jdbye @ The Real Jdbye:
    maybe mario kart 9 will be a launch title
  • The Real Jdbye @ The Real Jdbye:
    i really want a new mario kart
  • Veho @ Veho:
    What, you mean the endless stream of DLCs doesn't count?
  • Veho @ Veho:
    Why develop a new game when you can just sell season passes forever?
  • Veho @ Veho:
    I'm still on MKDS so I'm not bothered :tpi:
  • The Real Jdbye @ The Real Jdbye:
    i like the dlc tbh, i'd like a new game more
  • ZeroT21 @ ZeroT21:
    but the current version is still selling fine at full price
  • SylverReZ @ SylverReZ:
    Hello
  • ZeroT21 @ ZeroT21:
    sup
    +1
  • SylverReZ @ SylverReZ:
    @realtimesave, You seen the Unlock Switch flashcart yet?
    SylverReZ @ SylverReZ: @realtimesave, You seen the Unlock Switch flashcart yet?