Homebrew I need help - Homebrew Audio Examples

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
Hi, im Lion and i am coding a fun app for homebrew 3ds in the "C" coding language and devkitpro. I really want to let my app play audio but all of the NDSP examples are complicated because the compiler says it never works

Please help me and tell me theres a simple code that allows me to play wave files that actually works and doesnt get loads of errors inside of my devkitpro compiler. Thanks.
 

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,827
Trophies
2
Location
home
XP
9,398
Country
Hungary
Hi, im Lion and i am coding a fun app for homebrew 3ds in the "C" coding language and devkitpro. I really want to let my app play audio but all of the NDSP examples are complicated because the compiler says it never works

Please help me and tell me theres a simple code that allows me to play wave files that actually works and doesnt get loads of errors inside of my devkitpro compiler. Thanks.

Well, I admit the "streaming" example also looks a slight bit complicated to me.

What do you mean by "the compiler says it never works" ? Please post compiler output in a
Code:
[SPOILER]
[CODE]
paste compiler output here
[‌/CODE]
[/SPOILER]

Sadly there is no "simple" code to play an audio like you can in "high level" languages like Python or C#. You either need to parse the file manually, or you have to use a library for decompressing the audio file. What kind of audio file is it? wav? mp3? ogg? wma?

Also, on a sidenote devkitPro is the organization's name, devkitARM is the compiler for the 3DS and some other consoles.
 
Last edited by Sono,

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
Well, I admit the "streaming" example also looks a slight bit complicated to me.

What do you mean by "the compiler says it never works" ? Please post compiler output in a
Code:
[SPOILER]
[CODE]
paste compiler output here
[‌/CODE]
[/SPOILER]

Sadly there is no "simple" code to play an audio like you can in "high level" languages like Python or C#. You either need to parse the file manually, or you have to use a library for decompressing the audio file. What kind of audio file is it? wav? mp3? ogg? wma?

Also, on a sidenote devkitPro is the organization's name, devkitARM is the compiler for the 3DS and some other consoles.


its a wav file. all i plan to do is make the wav file play when an event happens and make it stop playing when an event happens, if i take the ndsp-example from curryguy on github, cant post link cus im new

a bunch of errors come up, this isnt because lack of import files but mainly i have no clue

if you want ill show you the compiler errors later but rn i need to know because this is really sad xd

--------------------- MERGED ---------------------------

btw heres the ndsp example.
Code:
#include <3ds.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char* argv[])
{
    gfxInitDefault();
   
    consoleInit(GFX_TOP, nullptr);
   
    // The dsp channel number
    constexpr int channel = 1;
   
    u32 sampleRate;
    u32 dataSize;
    u16 channels;
    u16 bitsPerSample;
   
    // Initialize ndsp
    ndspInit();
   
    ndspSetOutputMode(NDSP_OUTPUT_STEREO);
    ndspSetOutputCount(1); // Num of buffers
   
    // Reading wav file
    FILE* fp = fopen("./example.wav", "rb");
   
    if(!fp)
    {
        printf("Could not open the example.wav file.\n");
        return 1;
    }
   
    char signature[4];
   
    fread(signature, 1, 4, fp);
   
    if( signature[0] != 'R' &&
        signature[1] != 'I' &&
        signature[2] != 'F' &&
        signature[3] != 'F')
    {
        printf("Wrong file format.\n");
        fclose(fp);
        return 1;
    }
   
    fseek(fp, 40, SEEK_SET);
    fread(&dataSize, 4, 1, fp);
    fseek(fp, 22, SEEK_SET);
    fread(&channels, 2, 1, fp);
    fseek(fp, 24, SEEK_SET);
    fread(&sampleRate, 4, 1, fp);
    fseek(fp, 34, SEEK_SET);
    fread(&bitsPerSample, 2, 1, fp);
   
    if(dataSize == 0 || (channels != 1 && channels != 2) ||
        (bitsPerSample != 8 && bitsPerSample != 16))
    {
        printf("Corrupted wav file.\n");
        fclose(fp);
        return 1;
    }
   
    // Allocating and reading samples
    u8* data = static_cast<u8*>(linearAlloc(dataSize));
   
    if(!data)
    {
        fclose(fp);
        return 1;
    }
   
    fseek(fp, 44, SEEK_SET);
    fread(data, 1, dataSize, fp);
    fclose(fp);
   
    // Find the right format
    u16 ndspFormat;
   
    if(bitsPerSample == 8)
    {
        ndspFormat = (channels == 1) ?
            NDSP_FORMAT_MONO_PCM8 :
            NDSP_FORMAT_STEREO_PCM8;
    }
    else
    {
        ndspFormat = (channels == 1) ?
            NDSP_FORMAT_MONO_PCM16 :
            NDSP_FORMAT_STEREO_PCM16;
    }
   
    ndspChnReset(channel);
    ndspChnSetInterp(channel, NDSP_INTERP_NONE);
    ndspChnSetRate(channel, float(sampleRate));
    ndspChnSetFormat(channel, ndspFormat);
   
    // Create and play a wav buffer
    ndspWaveBuf waveBuf;
    std::memset(&waveBuf, 0, sizeof(ndspWaveBuf));
   
    waveBuf.data_vaddr = reinterpret_cast<u32>(data);
    waveBuf.nsamples = dataSize / (bitsPerSample >> 3);
    waveBuf.looping = true; // Loop enabled
    waveBuf.status = NDSP_WBUF_FREE;
   
    DSP_FlushDataCache(data, dataSize);
   
    ndspChnWaveBufAdd(channel, &waveBuf);
   
    while(aptMainLoop())
    {
        hidScanInput();
       
        u32 keys = hidKeysDown();
       
        if(keys & KEY_START)
            break;
       
        gfxFlushBuffers();
        gfxSwapBuffers();
        gspWaitForVBlank();
    }
   
    ndspChnWaveBufClear(channel);
   
    linearFree(data);
   
    gfxExit();
    ndspExit();
   
    return 0;
}
[CODE]
[SPOILER]

and if you compile that with the make command in devkitpro u get errors

--------------------- MERGED ---------------------------

if u compile that u get errors for like no reason. Remember im trying out all sorts of examples to make it work and none of them work

--------------------- MERGED ---------------------------

cant show a screenie cus im new ;-;
 

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,827
Trophies
2
Location
home
XP
9,398
Country
Hungary

That looks like C++, so no wonder you're getting errors :P

remove all constexpr, replace reinterpret_cast<type>(content) with (type)content, same with static_cast, then try again.

Examples:
constexpr int channel = 1;
-->
int channel = 1;

static_cast<u8*>(linearAlloc(dataSize));
-->
(u8*)linearAlloc(dataSize)
 

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
That looks like C++, so no wonder you're getting errors :P

remove all constexpr, replace reinterpret_cast<type>(content) with (type)content, same with static_cast, then try again.

Examples:
constexpr int channel = 1;
-->
int channel = 1;

static_cast<u8*>(linearAlloc(dataSize));
-->
(u8*)linearAlloc(dataSize)


Ok ill try but i dont know what you mean by " reinterpret_cast<type>(content)" im trying to remove and replace what you said by using the find and replace tools on notepad++

--------------------- MERGED ---------------------------

even if i try doing what you say i keep getting errors, such as...

C:\Users\Owner\Desktop\dev\Test3DS>make
main.c
arm-none-eabi-gcc -MMD -MP -MF /home/Owner/Desktop/dev/Test3DS/build/main.d -g -Wall -O2 -mword-relocations -fomit-frame-pointer -ffast-math -march=armv6k -mtune=mpcore -mfloat-abi=hard -I/home/Owner/Desktop/dev/Test3DS/include -I/opt/devkitpro/libctru/include -I/home/Owner/Desktop/dev/Test3DS/build -DARM11 -D_3DS -c /home/Owner/Desktop/dev/Test3DS/source/main.c -o main.o
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c: In function 'main':
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:11:23: error: 'nullptr' undeclared (first use in this function)
consoleInit(GFX_TOP, nullptr);
^~~~~~~
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:11:23: note: each undeclared identifier is reported only once for each function it appears in
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:98:26: error: expected expression before 'float'
ndspChnSetRate(channel, float(sampleRate));
^~~~~
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:103:6: error: expected expression before ':' token
std::memset(&waveBuf, 0, sizeof(ndspWaveBuf));
^
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:105:21: warning: assignment to 'const void *' from 'long unsigned int' makes pointer from integer without a cast [-Wint-conversion]
waveBuf.data_vaddr = (u32)data;
^
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:103:2: warning: label 'std' defined but not used [-Wunused-label]
std::memset(&waveBuf, 0, sizeof(ndspWaveBuf));
^~~
make[1]: *** [/opt/devkitpro/devkitARM/base_rules:85: main.o] Error 1
make: *** [Makefile:127: build] Error 2
.-.
 

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,827
Trophies
2
Location
home
XP
9,398
Country
Hungary
Ok ill try but i dont know what you mean by " reinterpret_cast<type>(content)" im trying to remove and replace what you said by using the find and replace tools on notepad++

Don't use this anywhere else, only on this source code!

Set replace mode to regular expression in the replace dialog.

Replace
Code:
reinterpret_cast<([^>]+)>\((.*)\);
and
Code:
static_cast<([^>]+)>\((.*)\);

with
Code:
(\1)\2;


Now set back the replace mode to normal mode, and replace constexpr with nothing.

--------------------- MERGED ---------------------------

Oops, I missed a few more C++ identifiers :wacko:

Replace std:: with nothing, replace float(sampleRate) with sampleRate, and replace nullptr with 0
 

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
Nearly worked but i got some new errors for ya.

C:\Users\Owner\Desktop\dev\Test3DS>make
main.c
arm-none-eabi-gcc -MMD -MP -MF /home/Owner/Desktop/dev/Test3DS/build/main.d -g -Wall -O2 -mword-relocations -fomit-frame-pointer -ffast-math -march=armv6k -mtune=mpcore -mfloat-abi=hard -I/home/Owner/Desktop/dev/Test3DS/include -I/opt/devkitpro/libctru/include -I/home/Owner/Desktop/dev/Test3DS/build -DARM11 -D_3DS -c /home/Owner/Desktop/dev/Test3DS/source/main.c -o main.o
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c: In function 'main':
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:68:13: error: expected expression before 'u8'
u8* data = u8*linearAlloc(dataSize);
^~
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:105:23: error: 'u32data' undeclared (first use in this function); did you mean 'data'?
waveBuf.data_vaddr = u32data;
^~~~~~~
data
C:/Users/Owner/Desktop/dev/Test3DS/source/main.c:105:23: note: each undeclared identifier is reported only once for each function it appears in
make[1]: *** [/opt/devkitpro/devkitARM/base_rules:85: main.o] Error 1
make: *** [Makefile:127: build] Error 2

C:\Users\Owner\Desktop\dev\Test3DS>
 

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
Congratulations, it works. :D time to test it out and play some sick silvagunner music

--------------------- MERGED ---------------------------

mfw compiler compiles in the first time in ages

--------------------- MERGED ---------------------------

sorry but WHAT THE ACTUAL FUUU** ALL I CAN HEAR IS A REALLY REALLY LOUD EAR PIERCING BEEPING NOISE
 
  • Like
Reactions: Sono

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,827
Trophies
2
Location
home
XP
9,398
Country
Hungary
sorry but WHAT THE ACTUAL FUUU** ALL I CAN HEAR IS A REALLY REALLY LOUD EAR PIERCING BEEPING NOISE

I forgot to tell, but by the looks of the example this is a very primitive one. The maximum wav size you can load is a bit less than 32Megabytes.

Another look at the code shows that it's so primitive that it has no checks other than a header check if the WAV is really valid. It loads garbage values from random places in the wav, so no wonder you're hearing garbage.
 

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
Can you suggest another piece of code i can use that wont beep and pierce my ears that can use sounds correctly. like the ones used in the non stop nyan cat audio one
 

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,827
Trophies
2
Location
home
XP
9,398
Country
Hungary
Can you suggest another piece of code i can use that wont beep and pierce my ears that can use sounds correctly. like the ones used in the non stop nyan cat audio one

Well... you can always write your own :P

Also, what other programming languages do you know? I think you should learn the basics of it on PC instead of the 3DS, because it's easier to experiment on PC.
 

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
I dont care about computers i just wanna mess with the 3ds, so how do i write my own C thing that allows me to play music files on the 3ds
 

Sono

cripple piss
Developer
Joined
Oct 16, 2015
Messages
2,827
Trophies
2
Location
home
XP
9,398
Country
Hungary
I dont care about computers i just wanna mess with the 3ds, so how do i write my own C thing that allows me to play music files on the 3ds

Well I'm sorry, but if you don't know the basics of C then I can't help you fix the code you have there :/
 

LionMemess

Member
OP
Newcomer
Joined
Jul 28, 2018
Messages
14
Trophies
0
Age
20
XP
66
Country
United Kingdom
Ill test out with fopen and other stuff

ill probs have to find out on my own

--------------------- MERGED ---------------------------

By the way, i do know other languages that can make this easier for me. but. heres the problem.

I know Lua but all Lua players crash on a yellow screen when you load a game engine
I know python but im not sure if that has a engine or something like devkitpro.

and another problem is that my original app used a code which allows the nintendo 3ds built in keyboard to show up, im using this for an upcoming project i might release in a few months, but im new to 3ds development so people dont expect much of it.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: Lmao now I can live the life of Juan...