Ability to load all types of uncompiled assets from datastore directory?

related Discord:

https://discord.com/channels/590783499494228008/712709706984194178/745028644749181019

[ 22:17 ] Dave :bust_in_silhouette: :
You can save a sample.. but only to WAV format, which can't then be read back in without a pass through the compiler. ugh.
[ 22:19 ] Dave :bust_in_silhouette: :
The data formats are the same, they just have different headers. We could either give you the option of which format to save to, or allow loading wav files as long as they're in a format we can handle (not 24 bit, not compressed). Or both!(edited)
[ 22:22 ] Toni (CET) :
I would be happy with either solution :slight_smile:
[ 22:23 ] Dave :bust_in_silhouette: :
for now I'll hack together a Lua function to convert a wav to a pda. one sec!
[ 23:06 ] Dave :bust_in_silhouette: : Here you go! Press A to record a 2 second sample, then B to play it back:

function wav2pda(filename)
    local fil = playdate.file
    local wav = fil.open(filename, fil.kFileRead)
    local datalen = fil.getSize(filename) - 44
    local pda = fil.open(string.sub(filename,0,#filename-4)..".pda", fil.kFileWrite)
    
    wav:seek(44)
    pda:write("Playdate AUD\68\172\0\2") -- 44kHz 16 bit mono
    
    local offset = 0
    
    while offset < datalen do
        local n = math.min(datalen-offset, 1024)
        pda:write(wav:read(n))
        offset += n
    end
    
    wav:close()
    pda:close()
end

local snd = playdate.sound

function playdate.AButtonDown()
    local s = snd.sample.new(2, snd.kFormat16bitMono) -- 2 seconds
    snd.micinput.recordToSample(s,
        function()
            s:save("test.wav")
            wav2pda("test.wav")
        end
    )
end

function playdate.BButtonDown()
    snd.sample.new("test"):play()
end

function playdate.update() end
1 Like