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
258
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,256
Trophies
4
Location
Space
XP
13,816
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
258
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
258
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
258
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

Recent Content

General chit-chat
Help Users
  • No one is chatting at the moment.
    Xdqwerty @ Xdqwerty: good night