Using Tables With Audio Files??

Background
I am currently working on a voice notes app. This app is basically an attempt to learn programming and also become comfortable with CRUD (create, read, update, delete) activities in Corona.

I’ve gotten to a point where I can create new audio files (recordings) and save them to the Documents directory. I’ve achieved this by using a variable that increments each time a new recording is saved. The next time a recording is started, the incremented variable is used to create a unique file name. I can then save that variable to persist across sessions.

The problem I’m foreseeing is that this system isn’t very robust or flexible. I’m quite new to programming, but I get the feeling that I should be storing all this data in a table so that I can perform all my CRUD operations through that table.

The Problem
I’m having difficulty understanding how to use a table with audio files. I can understand the use of a table that is just storing data like numbers and strings, but audio files are actual objects that exist in my Documents Directory, apart from any kind of data file. I guess my overall question is how can I perform operations on these files and keep everything in sync? Am I thinking of this all wrong?

Any help would be greatly appreciated, thanks.

  • Mike

[import]uid: 14700 topic_id: 28823 reply_id: 328823[/import]

Consider this.

Audio files you have in directory:

test1.wav
test2.mp3
You want to store that data and use a table to keep things neat, obviously you can’t save the actual files in the table but you can store a reference to them.

  
local audioTable = {   
 ["test1"] = fileName = "test1", extension = ".wav", path = system.DocumentsDirectory, soundFile = nil, handle = nil },  
 ["test2"] = fileName = "test2", extension = ".mp3", path = system.DocumentsDirectory, soundFile = nil, handle = nil }  
 }  
  
--Load up the sounds (loop through table to do so)  
for k, v in pairs( audioTable ) do  
 if audioTable[k].extension == ".wav" then  
 --Assign the loaded audio to the entries soundFile  
 audioTable[k].soundFile = audio.loadSound( audioTable[k].fileName, audioTable[k].path )  
 elseif audioTable[k].extension == ".mp3" then  
 --Assign the loaded audio to the entries soundFile  
 audioTable[k].soundFile = audio.loadStream( audioTable[k].fileName, audioTable[k].path )  
 end  
end  
  
--Play a sound  
 --Assign the loaded audio to the entries handle (so you can pause/resume/stop it later)  
audioTable["test2"].handle = audio.play( audioTable["test2"].soundFile )  

Hope that example makes sense. Basically you would store that table using JSON and load it back when needed into your app.

As for saving/loading the data, i would recommend JSON: http://www.coronalabs.com/blog/2012/02/14/reading-and-writing-files-in-corona/

[import]uid: 84637 topic_id: 28823 reply_id: 116150[/import]

Wow, thank you so much for the detailed response Danny!!

Not sure I really understand the concept of “handles”. The API documentation under events -> audio -> event.handle appears to be empty.

Aside from that, I think I’m still confused about how to add a new file to the table.

So let’s say I wanted to create a new audio file and add it to the table, would it be like this?

[blockcode]
local r
local fileName
local fileID = 1

local audioTable = { }
– When Record Button Is Pressed

– Create unique file name and save it in fileName variable.
fileName = “note” … fileID … “.mp3”

– Create a new file at the specified path
local filePath = system.pathForFile( fileName, system.DocumentsDirectory )

– Assign a new recording to the variable “r”
r = media.newRecording( filePath )

– Insert new file into audioTable
table.insert(audioTable, [fileName] = {name = fileName, extension = “.mp3”, path = system.DocumentsDirectory, soundFile = nil, handle = nil })

– Start Recording
r:startRecording()
– When Stop Button Is Pressed

– Stop Recording
r:stopRecording( )

– Increment the fileID Variable
fileID = fileID + 1
[/blockcode]
[import]uid: 14700 topic_id: 28823 reply_id: 116158[/import]

Just to resolve this thread, I got the following code working- not sure how good it is, but it works:

[blockcode]
– require JSON
local json = require(“json”)

– variable to hold the current file ID
currentID = 1

– notes table
notes = {}

– function to add a new entry to the notes table
local function addEntry()

local newEntry = { title = “note” … currentID, name = “note”, extension = “.aif” }

table.insert(notes, newEntry )

– increment the current file id by one
currentID = currentID + 1

end

– Save Table Function
function saveTable(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, “w”)
if file then
local contents = json.encode(t)
file:write( contents )
io.close( file )
return true
else
return false
end
end

– Load Table Function
function loadTable(filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local contents = “”
local myTable = {}
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
myTable = json.decode(contents);
io.close( file )
return myTable
end
return nil
end

– add a few entries for testing
addEntry()
addEntry()
addEntry()
addEntry()
addEntry()

– save the table to JSON
saveTable(notes,“notes.json”)
print(“saving notes.json”)

– load the JSON back in and print the contents of the table
local function printNotes()

local allNotes = loadTable(“notes.json”)
print(“loading notes.json”)

for k, v in pairs( allNotes ) do
print(notes[k].title, notes[k].name, notes[k].extension)
end

end

– fire off the print function to load and print the table
printNotes()

– print the current ID (for reference)
print(currentID)
[/blockcode] [import]uid: 14700 topic_id: 28823 reply_id: 116350[/import]