Let’s Create a Corona SDK Soundtrack Module Part 1

Corona Level: Beginner/Intermediate

Creating a Soundtrack

A soundtrack should be considered a major component of your game. It provides the mood and can accentuate certain areas of gameplay, enhancing the overall experience for the player. While some games benefit from loops and simple repeating musical phrases, more atmospheric games might find a better fit with an overall soundtrack that runs independent of where you are in your application.

Doing this in Corona SDK is very simple. We can actually get our soundtrack module up and running in 50 lines of code, and still maintain readability (37 lines compacted). Let’s get started.

The Basic Project

First, go ahead and start a basic Corona project. Here is what our project structure looks like:

Project
 main.lua
 config.lua
 build.settings
 tracks
  track_1.mp3
  track_2.mp3
  track_3.mp3

You will need to supply three mp3 files to complete this exercise. Name them exactly as shown in the “tracks” folder in the above project structure details.

Outlining the Module

Let’s start by creating the shell for our module. This will provide us with a “to-do” list that we simply fill in as we work on each area of functionality.

Create a new file and save it as mod_soundtrack.lua in your project folder and enter the following:

--mod_soundtrack.lua
local soundtrack = {}

return soundtrack

If you’ve made a module before, then this format should look familiar. We assign an empty table to the soundtrack variable and return it back to where it’s been “required” from. You’ll see this a little later when we put it into action.

Setting Up Properties

We are going to need some properties to do our soundtrack work so let’s add the following to the file and then discuss them. Update the mod_soundtrack.lua like so:

--mod_soundtrack.lua
local soundtrack = {}

local trackIndex = 1
local trackMax = 3
local savedVolume = 0
local isActive = false

local ST_CHANNEL = 1

return soundtrack

In this module we are going to play through a set of tracks and we need a way to keep tabs on where we are in the track list. We go ahead and declare a trackIndex and trackMax with the values of 1 and 3 respectively. We assign the number 3 to the trackMax property to mirror the amount of tracks we have in our “tracks” folder.

We are going to implement a mute/un-mute function in our soundtrack module, so we need a way to keep track of the current volume. We do that with the savedVolume property.

The isActive flag is used to handle some conditional work in a future method that we will create. We’ll discuss the flag in greater detail further on.

The ST_CHANNEL is a constant that we use to set aside a channel for the soundtrack. Corona SDK will auto-assign channels as needed, but we want to make sure that we always have a channel available above and beyond any other audio requests. We use a constant because it’s more readable in our code and makes it simple to globally change the soundtrack channel, if the need ever arose. This alone will not reserve the audio channel though, we need to talk about…

Reserving The Channel

Now that we have our properties in order, we need to actually reserve the channel our soundtrack uses. We do that with audio.reserveChannels() which takes a number of channels to reserve. For instance, if you passed the number 3, then channels 1-3 would be reserved. In our case we are going to be using channel 1 for our soundtrack, so we only need to pass the number 1, which in turn will reserve channel 1.

It’s important to remember how this works. If you are trying to reserve channel 8, and pass 8 to the audio.reserveChannels() method, you will actually be reserving channels 1-8. So make sure that’s what you want.

--mod_soundtrack.lua
local soundtrack = {}

local trackIndex = 1
local trackMax = 3
local savedVolume = 0
local isActive = false

local ST_CHANNEL = 1

audio.reserveChannels( 1 )

return soundtrack

Creating The Methods

Now we can start adding our functionality to the soundtrack module. Let’s update the code as follows, and then we will flush it out:

--mod_soundtrack.lua
local soundtrack = {}

local trackIndex = 1
local trackMax = 3
local savedVolume = 0
local isActive = false
local ST_CHANNEL = 1

audio.reserveChannels( 1 )

function soundtrack:start()

end

function soundtrack:stop()

end

function soundtrack:mute()

end

function soundtrack:unmute()

end

return soundtrack

In part 2 of this article we’ll actually start making these methods do something.

Continue on to Let’s Create a Corona SDK Soundtrack Module Part 2

Advertisements

3 thoughts on “Let’s Create a Corona SDK Soundtrack Module Part 1

  1. loving your website. thnx for sharing your knowledge.. i’m a lil bit lagging behind when it comes to sounds in Corona SDK, so I’m really engrossed with this one. 😀 thnx develephant !

    Like

Comments are closed.