Rockbox Technical Forums

Rockbox Development => Starting Development and Compiling => Topic started by: LoneWolf_Recon on August 02, 2011, 01:51:54 PM

Title: mp3 playback in a LUA program
Post by: LoneWolf_Recon on August 02, 2011, 01:51:54 PM
Hello everyone,

I've been trying to get this function working for weeks now and still no dice. At the most basic level I want to stop the current background playback (if already playing), load up an mp3 from a specified path, and play the mp3.

I've read through all of the plugin.h file as well as its subsidiary plugins that use that mp3_play_data function. I load the contents of the mp3 to a variable just fine(Its a small mp3 of about 92kB so memory overflow shouldn't be a problem). Then I feed the contents of that variable into the first argument of the mp3_play_data function along with a file size of what I've measured the file to be.

Most of the time it comes up with an error saying:
Quote
attempt to call field 'mp3_play_data' (a nil value)
stack traceback:
         /MusicTest.lua:76: in main chunk
or on rare occasions it just ignores the function altogether.. -_-

Here's my code so far, I used some of the helper funcs from helloworld.lua to load the mp3 contents into memory:
Code: [Select]
--Music Test
--Test the sound playback commands

--Includes
require("actions")

--Vars
local BtnInput
track = "/test.mp3"

-- Helper function which reads the contents of a file
function file_get_contents(filename)
    local file = io.open(filename, "r")
    if not file then
        return nil
    end

    local contents = file:read("*all") -- See Lua manual for more information
    file:close() -- GC takes care of this if you would've forgotten it

    return contents
end

--MP3 test
rb.lcd_clear_display()
printf("Test to play sample MP3")
rb.lcd_update()

rb.mp3_play_data(file_get_contents(track), 98304, nil)

while rb.mp3_is_playing() do
rb.sleep(rb.HZ)
end
printf("Hit Select..")
rb.lcd_update()
repeat
BtnInput = rb.get_action(rb.contexts.CONTEXT_STD, rb.HZ)
until BtnInput == rb.actions.ACTION_STD_OK

Any help is appreciated  ;D
,LoneWolf
Title: Re: mp3 playback in a LUA program
Post by: saratoga on August 02, 2011, 02:32:23 PM
Hello everyone,

I've been trying to get this function working for weeks now and still no dice. At the most basic level I want to stop the current background playback (if already playing), load up an mp3 from a specified path, and play the mp3.

I've read through all of the plugin.h file as well as its subsidiary plugins that use that mp3_play_data function. I load the contents of the mp3 to a variable just fine(Its a small mp3 of about 92kB so memory overflow shouldn't be a problem). Then I feed the contents of that variable into the first argument of the mp3_play_data function along with a file size of what I've measured the file to be.

I'm not an expert on this part of rockbox, but I think mp3_play_data is a function used to talk to the DSP chip on the Archos players.  On newer targets I don't think the function exists since theres no hardware DSP chip handling decoding.   Instead they use codecs running on the CPU.

I'm not sure if theres an easy way to decode MP3 from Lua.  Its certainly possible from plugins (see test_codec.c) but it involves some work to make the codecs work outside of ordinary playback.  This might be a good thing to ask about in IRC. 
Title: Re: mp3 playback in a LUA program
Post by: LoneWolf_Recon on August 02, 2011, 02:40:14 PM
Thanks Saratoga. I noticed the debug when I ran my program under the simulator where it ran the Codec library. And the nil value error message makes sense now if the Sansa is decoding it in software.

I'll check with they guys over in IRC.
Title: Re: mp3 playback in a LUA program
Post by: torne on August 02, 2011, 04:17:33 PM
You probably want to control the playback engine, rather than providing MP3 data, i.e. have the rockbox code handle reading the file and so on...
Title: Re: mp3 playback in a LUA program
Post by: chrisjj on December 12, 2016, 08:25:28 PM
LoneWolf_Recon, Did you get this working?
Title: Re: mp3 playback in a LUA program
Post by: Bilgus on December 27, 2016, 08:30:24 AM
Even if he did I don't think he'll be answering you any time soon plus he's like 57 now (in canine years)