Homebrew How do I use CSND with my homebrew program?

JackMacWindows

Well-Known Member
OP
Newcomer
Joined
Jan 16, 2016
Messages
53
Trophies
0
XP
268
Country
United States
Hello,
I am making a new game for my 3DS. I am currently working on the menu for it. I thought that it would be nice to have some background to the menu, but I have not been able to find good code showing how to play a song. I am currently using this code to play the file:
Code:
u8* sndbuffer;
if (fexists("data/background.raw")) { //fexists(std::string) will check if a file exists.
FILE *file = fopen("data/background.raw", "rb");
debugPrint("Song loaded."); //debugPrint(const char *) prints to the bottom screen.
fseek(file, 0, SEEK_END);
off_t sndsize = ftell(file);
sndbuffer = (u8*)linearAlloc(sndsize);
fseek(file, 0, SEEK_SET);
debugPrint("Reading file...");
off_t bytesRead = fread(sndbuffer, 1, sndsize, file);
fclose(file);
debugPrint("File read.");
csndPlaySound(0x8, CSND_LOOPMODE_NORMAL, 32000, 1.0, 0.0, sndbuffer, sndbuffer, (u32)sndsize);
I have made a loop from a royalty-free song, and I exported it to raw format with signed 16-bit PCM with Audacity. When it plays, I get static. What is wrong here?
 

The Real Jdbye

*is birb*
Member
Joined
Mar 17, 2010
Messages
23,330
Trophies
4
Location
Space
XP
13,905
Country
Norway
Hello,
I am making a new game for my 3DS. I am currently working on the menu for it. I thought that it would be nice to have some background to the menu, but I have not been able to find good code showing how to play a song. I am currently using this code to play the file:
Code:
u8* sndbuffer;
if (fexists("data/background.raw")) { //fexists(std::string) will check if a file exists.
FILE *file = fopen("data/background.raw", "rb");
debugPrint("Song loaded."); //debugPrint(const char *) prints to the bottom screen.
fseek(file, 0, SEEK_END);
off_t sndsize = ftell(file);
sndbuffer = (u8*)linearAlloc(sndsize);
fseek(file, 0, SEEK_SET);
debugPrint("Reading file...");
off_t bytesRead = fread(sndbuffer, 1, sndsize, file);
fclose(file);
debugPrint("File read.");
csndPlaySound(0x8, CSND_LOOPMODE_NORMAL, 32000, 1.0, 0.0, sndbuffer, sndbuffer, (u32)sndsize);
I have made a loop from a royalty-free song, and I exported it to raw format with signed 16-bit PCM with Audacity. When it plays, I get static. What is wrong here?
It might be a bad idea to load the entire song into memory. Raw audio files take a lot of space and the 3DS has a limited amount of memory. How big is the file?
You should look into playing back some lossy format, like mp3 or ogg.
 

JackMacWindows

Well-Known Member
OP
Newcomer
Joined
Jan 16, 2016
Messages
53
Trophies
0
XP
268
Country
United States
It might be a bad idea to load the entire song into memory. Raw audio files take a lot of space and the 3DS has a limited amount of memory. How big is the file?
You should look into playing back some lossy format, like mp3 or ogg.
The file is about thirty seconds long, and 3MB in size; loads in about a second. I previously used the whole 4 minutes (50MB), but that was too big to load, so I made the loop.
 

JackMacWindows

Well-Known Member
OP
Newcomer
Joined
Jan 16, 2016
Messages
53
Trophies
0
XP
268
Country
United States
Also have a look at sound.c from gridlauncher. It includes CSND wrapper functions to load a sound from a file on disk, play it (looping optional), stop playing it, and free the memory used by the sound when it is no longer needed.

https://github.com/mashers/3ds_hb_menu/blob/master/source/sound.c
I am going to try that, but I can't get the program to compile. I got this error:
Code:
sound.o: In function `audio_free':
sound.c:66: multiple definition of `audioActive'
main.cpp:135: first defined here
The code on line 135 is just opening a text file. I tried to do a "#ifndef thing; #define thing; code; #endif" to make the compiler define audioActive, but that does nothing. There is no trace of audioActive in my main file (a find search shows 0 matches).
(Line 66 is the audioFree function)
 
Last edited by JackMacWindows,

JackMacWindows

Well-Known Member
OP
Newcomer
Joined
Jan 16, 2016
Messages
53
Trophies
0
XP
268
Country
United States
I've been looking at this the whole day, and I got it to compile and load the file, but I don't get any sound output. I put the functions from sound.c in main.cpp, but using them did not play sound.
 

Rinnegatamante

Well-Known Member
Member
Joined
Nov 24, 2014
Messages
3,162
Trophies
2
Age
29
Location
Bologna
Website
rinnegatamante.it
XP
4,857
Country
Italy
It might be a bad idea to load the entire song into memory. Raw audio files take a lot of space and the 3DS has a limited amount of memory. How big is the file?
You should look into playing back some lossy format, like mp3 or ogg.

The only audiocodec supported by 3DS which isn't completely RAW is ADPCM so even if you're going to reproduce an OGG file, you'll need to convert it to a PCM buffer (so memory will always be fullified).
To solve memory problems, you should do a proper audio streaming playback (like lpp-3ds does with streaming flags for both Video and Sound modules).

If OP is interested in audio streaming playback, this is the reference file for lpp-3ds: https://github.com/Rinnegatamante/lpp-3ds/blob/master/source/luaSound.cpp
CSND wrapper is really "bad" written (in terms of readability) but nDSP ones is pretty well written and should be easily understandable.
 

mashers

Stubborn ape
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
40
Location
Kongo Jungle
XP
5,074
Country
I've been looking at this the whole day, and I got it to compile and load the file, but I don't get any sound output. I put the functions from sound.c in main.cpp, but using them did not play sound.
Did you initialise CSND before trying to load and play any sounds? What format are the sounds in? And do you get any return values from the CSND functions which might indicate an error?
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • K3Nv2 @ K3Nv2:
    I'll reformat and have a 3tb raid0 m. 2 at least
    +1
  • K3Nv2 @ K3Nv2:
    Lmao that sold out fast
    +1
  • Veho @ Veho:
    Yeet the cat.
    +1
  • K3Nv2 @ K3Nv2:
    Good idea
    +1
  • The Real Jdbye @ The Real Jdbye:
    i thought everybody knew cocktails are like 75% ice
  • Veho @ Veho:
    Yeah but not like this.
  • Veho @ Veho:
    It's not like they're complaining that their Slurpee is 99% ice or something, but if the cocktail calls for "shot of vodka, shot of vermouth, shot of gin, shot of Campari, three shots of juice, squirt of lemon" and ends up being a thimbleful of booze, that's a problem.
  • The Real Jdbye @ The Real Jdbye:
    the funny thing is cocktails in norway are only allowed to have 1 20ml shot of booze
  • The Real Jdbye @ The Real Jdbye:
    so..... yeah
  • The Real Jdbye @ The Real Jdbye:
    we're used to only having a thimbleful of booze
  • Veho @ Veho:
    Booo.
  • The Real Jdbye @ The Real Jdbye:
    same thing if you want whisky on the rocks or something, you can't get a double
  • The Real Jdbye @ The Real Jdbye:
    but you could buy as many shots of whisky (or anything else) as you want and ask for a glass of ice and pour them in
  • The Real Jdbye @ The Real Jdbye:
    it's dumb
  • Veho @ Veho:
    Maybe.
  • Veho @ Veho:
    There was a comparison of the number of Ibuprofen poisonings before and after they limited the maximum dosage per box or per pill (i'll look that up). No limit on the number of boxes you can still buy as many as you want, so people argued it was pointless.
  • Veho @ Veho:
    But the number of (accidental) poisonings dropped because drinking an entire package of ibuprofen pills went from "I need a new liver" to "I need a new box of Ibuprofen".
  • Veho @ Veho:
    Here we have ketoprofen that used to be prescription-only because of the risk of toxic dosages, but then they halved the dose per pill and sell them in bottles of six pills apiece instead of twenty and it doesn't need a prescription any more. Yes you can buy more than one bottle but people simply don't.
  • Psionic Roshambo @ Psionic Roshambo:
    Usually accidentally overdose of ibuprofen here is from people taking like cold medicine then ibuprofen for a headache and the combination is over what they need
    Veho @ Veho: https://imgur.com/gallery/QQkYnQu