Help with CSND_playsound

Discussion in '3DS - Flashcards & Custom Firmwares' started by Rinnegatamante, Dec 7, 2014.

  1. Rinnegatamante
    OP

    Rinnegatamante GBAtemp Psycho!

    Member
    3,127
    3,270
    Nov 24, 2014
    Italy
    Bologna
    Hello,
    according to this sample provided with libctru, i'm trying to add WAV support to my LUA interpreter but i'm having issues with this.

    This is my actual code:
    Code:
    /*----------------------------------------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #------  This File is Part Of : ----------------------------------------------------------------------------------------#
    #------- _  -------------------  ______  _  --------------------------------------------------------------------------#
    #------ | | ------------------- (_____ \ | |  --------------------------------------------------------------------------#
    #------ | | ---  _  _  ____    _____) )| |  ____  _  _  ____  ____  ----------------------------------------------#
    #------ | | --- | | | | / _  |  |  ____/ | | / _  || | | | / _  ) / ___)  ----------------------------------------------#
    #------ | |_____| |_| |( ( | |  | |      | |( ( | || |_| |( (/ / | |  --------------------------------------------------#
    #------ |_______)\____| \_||_|  |_|      |_| \_||_| \__  | \____)|_|  --------------------------------------------------#
    #------------------------------------------------- (____/  -------------------------------------------------------------#
    #------------------------  ______  _  -------------------------------------------------------------------------------#
    #------------------------  (_____ \ | |  -------------------------------------------------------------------------------#
    #------------------------  _____) )| | _  _  ___  ------------------------------------------------------------------#
    #------------------------  |  ____/ | || | | | /___)  ------------------------------------------------------------------#
    #------------------------  | |      | || |_| ||___ |  ------------------------------------------------------------------#
    #------------------------  |_|      |_| \____|(___/  ------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #- Licensed under the GPL License --------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #- Copyright (c) Nanni <lpp.nanni@gmail.com> ---------------------------------------------------------------------------#
    #- Copyright (c) Rinnegatamante <rinnegatamante@gmail.com> -------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #- Credits : -----------------------------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------#
    #- Smealum for ctrulib -------------------------------------------------------------------------------------------------#
    #- Aurelio for testing & bug-fixing ------------------------------------------------------------------------------------#
    #-----------------------------------------------------------------------------------------------------------------------*/
     
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <3ds.h>
    #include "include/luaplayer.h"
     
    struct wav{
    u32 samplerate;
    u8* audiobuf;
    u32 size;
    };
     
    static int lua_openwav(lua_State *L)
    {
        int argc = lua_gettop(L);
        if (argc != 1) return luaL_error(L, "wrong number of arguments");
        const char *file_tbo = luaL_checkstring(L, 1);
        Handle fileHandle;
        FS_archive sdmcArchive=(FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
        FS_path filePath=FS_makePath(PATH_CHAR, file_tbo);
        Result ret=FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
        if(ret) return luaL_error(L, "error opening file");
        u32 samplerate,bytesRead;
        u64 size;
        FSFILE_GetSize(fileHandle, &size);
        u8* audiobuf = (u8*)(malloc(size-44));
        FSFILE_Read(fileHandle, &bytesRead, 24, &samplerate, 4);
        FSFILE_Read(fileHandle, &bytesRead, 44, audiobuf, size-44);
        wav *wav_file = (wav*)malloc(sizeof(wav));
        wav_file->audiobuf = audiobuf;
        wav_file->samplerate = samplerate;
        wav_file->size = size-44;
        lua_pushnumber(L,(u32)wav_file);
        return 1;
    }
     
    static int lua_soundinit(lua_State *L)
    {
        int argc = lua_gettop(L);
        if (argc != 0) return luaL_error(L, "wrong number of arguments");
        CSND_initialize(NULL);
        return 0;
    }
     
    static int lua_playWav(lua_State *L)
    {
        int argc = lua_gettop(L);
        if (argc != 1) return luaL_error(L, "wrong number of arguments");
        wav* src = (wav*)luaL_checkint(L, 1);
        GSPGPU_FlushDataCache(NULL, src->audiobuf, src->size);
        CSND_playsound(0x8, CSND_LOOP_DISABLE, CSND_ENCODING_PCM16, src->samplerate, (u32*)src->audiobuf, NULL, src->size, 2, 0);
        return 0;
    }
     
    static int lua_soundend(lua_State *L)
    {
        int argc = lua_gettop(L);
        if (argc != 0) return luaL_error(L, "wrong number of arguments");
        CSND_shutdown();
        return 0;
    }
     
    //Register our Sound Functions
    static const luaL_Reg Sound_functions[] = {
      {"openWav",                lua_openwav},
      {"playWav",                lua_playWav},
      {"init",                    lua_soundinit},
      {"term",                    lua_soundend},
      {0, 0}
    };
     
    void luaSound_init(lua_State *L) {
        lua_newtable(L);
        luaL_setfuncs(L, Sound_functions, 0);
        lua_setglobal(L, "Sound");
    }
    And i call it in my LUA sample with this:
    Code:
    white = Color.new(255,255,255)
    black = Color.new(0,0,0)
    oldpad = Controls.read()
    Sound.init()
    test = Sound.openWav("/test.wav")
    while true do
    Screen.waitVblankStart()
    Screen.refresh()
    Controls.init()
    pad = Controls.read()
    if (Controls.check(pad,KEY_A)) then
    System.exit()
    end
    Sound.playWav(test)
    Screen.flip()
    end
    Sound.term()
    Which is like a thing like this in C:
    Code:
    struct wav{
    u32 samplerate;
    u8* audiobuf;
    u32 position;
    };
     
    int main(){
    srvInit();
    aptInit();
    gfxInit();
    hidInit(NULL);
    irrstInit(NULL);
    fsInit();
    CSND_initialize(NULL);
    //CODE TO LOAD WAV, SEE lua_openwav
    while(aptMainLoop()){
    gspWaitForVBlank();
    GSPGPU_FlushDataCache(NULL, src->audiobuf, src->position);
    CSND_playsound(0x8, CSND_LOOP_DISABLE, CSND_ENCODING_PCM16, src->samplerate, (u32*)src->audiobuf, NULL, src->size, 2, 0);
    gfxFlushBuffers();
    gfxSwapBuffers();
    }
    }
    When i launch my interpreter, i get no sound but the rest of the homebrew works fine. Where i am wrong?
     
  2. clienthax

    clienthax GBAtemp Regular

    Member
    100
    62
    Oct 21, 2009
    any chance your using the new 3ds?
     
  3. Rinnegatamante
    OP

    Rinnegatamante GBAtemp Psycho!

    Member
    3,127
    3,270
    Nov 24, 2014
    Italy
    Bologna
    No, i'm using a fat 3DS (first model).
     
  4. Rinnegatamante
    OP

    Rinnegatamante GBAtemp Psycho!

    Member
    3,127
    3,270
    Nov 24, 2014
    Italy
    Bologna
    Bump, someone knows where i'm wrong?