How do I use CSND with my homebrew program?

Discussion in '3DS - Homebrew Development and Emulators' started by JackMacWindows, Feb 25, 2016.

  1. JackMacWindows
    OP

    JackMacWindows Member

    Newcomer
    41
    20
    Jan 16, 2016
    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?
     
  2. The Real Jdbye

    The Real Jdbye Always Remember 30/07/08

    Member
    GBAtemp Patron
    The Real Jdbye is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    11,435
    4,751
    Mar 17, 2010
    Norway
    Alola
    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.
     
  3. JackMacWindows
    OP

    JackMacWindows Member

    Newcomer
    41
    20
    Jan 16, 2016
    United States
    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.
     
  4. The Real Jdbye

    The Real Jdbye Always Remember 30/07/08

    Member
    GBAtemp Patron
    The Real Jdbye is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    11,435
    4,751
    Mar 17, 2010
    Norway
    Alola
    lpp-3ds can play WAV/AIFF/OGG files. Maybe you can borrow some code from it.
     
  5. mashers

    mashers Stubborn ape

    Member
    3,837
    5,153
    Jun 10, 2015
    Kongo Jungle
  6. JackMacWindows
    OP

    JackMacWindows Member

    Newcomer
    41
    20
    Jan 16, 2016
    United States
    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, Feb 25, 2016
  7. mashers

    mashers Stubborn ape

    Member
    3,837
    5,153
    Jun 10, 2015
    Kongo Jungle
    How are you including sound.h?
     
  8. JackMacWindows
    OP

    JackMacWindows Member

    Newcomer
    41
    20
    Jan 16, 2016
    United States
    I included only sound.c, all the required headers are in the source folder (next to main.cpp).
     
    Last edited by JackMacWindows, Feb 25, 2016
  9. mashers

    mashers Stubborn ape

    Member
    3,837
    5,153
    Jun 10, 2015
    Kongo Jungle
    There's the problem. You need to include sound.h, not sound.c.
     
  10. JackMacWindows
    OP

    JackMacWindows Member

    Newcomer
    41
    20
    Jan 16, 2016
    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.
     
  11. Rinnegatamante

    Rinnegatamante GBAtemp Psycho!

    Member
    3,127
    3,260
    Nov 24, 2014
    Italy
    Bologna
    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.
     
  12. mashers

    mashers Stubborn ape

    Member
    3,837
    5,153
    Jun 10, 2015
    Kongo Jungle
    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?
     
  13. Woni

    Woni Member

    Newcomer
    30
    11
    Jan 2, 2016
    France