Changing sounds

So I am making a simple piano app (my first project) and im very new to Corona. Basically I want piano to have some different types of sounds that can be choosen in small table view widget, here’s my problem- how to make it with that feature? If it will work something like this: Piano plays normal. You’re tapping on button that activates the table view, then you’re choosing for example ‘pedal sustain’ option and keys play now another type of sound. How do I make that? I use this code for all of the keys on keyboard:


local widget = require(“widget”) 

local C = media.newEventSound(“C.mp3”)

local button_C_Press = function(event)
media.playEventSound(C,button_C_Press)
end

local button_C = widget.newButton
{
defaultFile = “NewKey.png”,
overFile = “NewKey2.png”,
onPress = button_C_Press,
}
button_C.x = 20; button_C.y = 295


…Thanks for help

You could add a boolean variable:

local isPedalActive = false

And when they have touched the pedal button, then set it to true:

isPedalActive = true

And then add this to your button_C_press function:

if event.phase == "began" then if isPedalActive = true then media.playEventSound(cPedal) --assuming you already loaded your audio above end end

Of course, if you have a large number of piano keys, you don’t want to do this individually for each function, rather:

  1. Set a specific id for each key, in the widget.newButton table.

  2. In the if statement, load the sound there but instead you would retrieve the button’s id and play that mp3 file.

    –create table of key button ids and mp3 files for their pedal noises local keys = { {buttonId = “C”, pedalNoise = “Cpedal.mp3”}, {buttonId = “D”, pedalNoise = “Dpedal.mp3”} } function pianoKeys(event) for i = 1, #keys do – for each table in the keys table, load the sound for each key local keySound = media.newEventSound(keys[i].buttonId … “.mp3”) – normal sound loaded local keypedalSound = media.newEventSound(keys[i].pedalNoise) --pedal sound loaded function buttonPress(event) --When they press the key, detect if the pedal is active if event.phase == “began” then if isPedalActive == true then media.playEventSound(keyPedalSound) --is active, play pedal sound else media.playEventSound(keySound) – is not active, play regular sound end end end local pianoKey = widget.newButton({ id = keys[i].buttonId, – place appropriate id defaultFile = “new” … keys[i].buttonId … “key.png”, – place appropriate defaultFile overFile = “new” … keys[i].buttonId … “key2.png”, – place appropriate overFile onPress = buttonPress – apply above function to each key }) end end

But leave this for later.   :wacko:  Just focus on the top part of my response. How many keys are you planning to have?

Thanks for feedback! Sorry for so long reply, I had lots of important thing to do, but now I can continue Corona work

I did this:    ( I know I have stack of syntax problems and its incorrect overall but as I said Im total newbie to Corona )

but it keeps saying: main.lua:200:'then’expected near ‘=’
that’s this:

if event.phase == “began” then
  if isPedalActive = true then  <------ main lua 200
    media.playEventSound(cPedal)
  end
end

I know the ID table method but I think I’d rather to make them (keys) individually, maybe that will take some time but it’s still easier for me, first project shouldn’t be the hardest

I also have idea to make note letters on keys that can be required by button, so all in all, what I would like to do - 2 pedals and note button

So can you give me down here, if you can of course, a sample of one key’s complete code, including those features and needed locals, falses and trues etc.?

and I’m planning to have all 88 keys i guess :mellow:

thanks

Hi @llordglassenward87,

Welcome to Corona! One starting piece of advice is, I don’t recommend that you use the “media.*”-based audio functions like “media.playEventSound()”. These are very old and have a number of limitations, whereas the OpenAL APIs in the “audio.*” library will typically serve you much better.

Here’s a guide for understanding how it all works:

https://docs.coronalabs.com/guide/media/audioSystem/index.html

Best regards,

Brent

As for your error, when using if statements and checking for equality you need to use two equal signs, not one.

local num = 0 if num == 0 then \<-- See the difference? end

Also, I recommend you follow Brent’s advice, I’ve seen the audio libraries a couple of times, but I have never seen the media library before now.

https://docs.coronalabs.com/guide/media/audioSystem/index.html

I know you want this project to be simple but building 88 keys separately is not a good idea. Time-consuming and redundant, I recommend not to do it this way, to be frank, it’s painful.

As for the sample of one key’s complete code, do you have the audio and image files for one key and two pedals? If you do, zip it up and post it here and I will try to help in my spare time. (9th grade is starting to pile on the homework)

Also, to test this project out properly, you can’t use Corona Simulator. I am assuming you want the user to press pedals and keys at the same time.

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html#multitouch

At this moment I dont have audio files and graphics. Just using one sound and regular white rectangle a as key beascue at first I’d like to figure out all needed coding stuff and when it’s done then just assort best sounds etc.

I’ve seen some libraries but still dont know how to compactibile everything into one :frowning:

I know multitouch is needed. By the way how about zooming? If out there’s 88 keys all of them would be tiny. I don’t want to make scrollview, my idea was to see whole keyobard when you open app and just individually pinch zoom it in in your own preference. Is it possible as I had in my plans to just zoom it by your mobile like you can do the same with photos? I dont know how would it work with running corona if it would ever work (now I see it’s nooby since ive been learning some more things) So what about zooming with code… but key work isnt still done. Ummmm what shall we do for now, what’s more important, what do you recommend?

Sorry for my a bit awkward english :unsure:

@llordglassenward87 - definitely switch the audio library (guide) for improved functionality.

For zooming:  I think you will need a zoom button as multitouch will be busy sensing multiple fingers on the keyboard and won’t know when you want to zoom instead of play without some sort of toggle button.

Keyboard: Each octave will have 12 unique keys so concentrate on getting one octave right and then it will be easy to duplicate it 7 times.

Sound table:  Again, I’d say just start with one octave.  Make a table that contains each keyboard key which in turn will be a table of sounds (audio.loadSound( ) ) arranged in some orderly fashion so they are easy to reference later.

[lua]

local soundTable = {

     a1 = {

          regular      =  audio.loadSound(“a1.mp3”),  

          sustained    =  audio.loadSound(“a1sustained.mp3”),    

          effectX      =  audio.loadSound(“a1EffectX.mp3”)

          },

    b1 = {

          regular      =  audio.loadSound(“b1.mp3”),  

          sustained    =  audio.loadSound(“b1sustained.mp3”),    

          effectX      =  audio.loadSound(“b1EffectX.mp3”)

          },

     c1 = {

          regular      =  audio.loadSound(“c1.mp3”),  

          sustained    =  audio.loadSound(“c1sustained.mp3”),    

          effectX      =  audio.loadSound(“c1EffectX.mp3”)

          }

}

audio.play( soundTable.a1[“regular”] )

audio.play( soundTable.a1[“sustained”] )  

audio.play( soundTable.a1[“effectX”] ) 

[/lua]

You could add a boolean variable:

local isPedalActive = false

And when they have touched the pedal button, then set it to true:

isPedalActive = true

And then add this to your button_C_press function:

if event.phase == "began" then if isPedalActive = true then media.playEventSound(cPedal) --assuming you already loaded your audio above end end

Of course, if you have a large number of piano keys, you don’t want to do this individually for each function, rather:

  1. Set a specific id for each key, in the widget.newButton table.

  2. In the if statement, load the sound there but instead you would retrieve the button’s id and play that mp3 file.

    –create table of key button ids and mp3 files for their pedal noises local keys = { {buttonId = “C”, pedalNoise = “Cpedal.mp3”}, {buttonId = “D”, pedalNoise = “Dpedal.mp3”} } function pianoKeys(event) for i = 1, #keys do – for each table in the keys table, load the sound for each key local keySound = media.newEventSound(keys[i].buttonId … “.mp3”) – normal sound loaded local keypedalSound = media.newEventSound(keys[i].pedalNoise) --pedal sound loaded function buttonPress(event) --When they press the key, detect if the pedal is active if event.phase == “began” then if isPedalActive == true then media.playEventSound(keyPedalSound) --is active, play pedal sound else media.playEventSound(keySound) – is not active, play regular sound end end end local pianoKey = widget.newButton({ id = keys[i].buttonId, – place appropriate id defaultFile = “new” … keys[i].buttonId … “key.png”, – place appropriate defaultFile overFile = “new” … keys[i].buttonId … “key2.png”, – place appropriate overFile onPress = buttonPress – apply above function to each key }) end end

But leave this for later.   :wacko:  Just focus on the top part of my response. How many keys are you planning to have?

Thanks for feedback! Sorry for so long reply, I had lots of important thing to do, but now I can continue Corona work

I did this:    ( I know I have stack of syntax problems and its incorrect overall but as I said Im total newbie to Corona )

but it keeps saying: main.lua:200:'then’expected near ‘=’
that’s this:

if event.phase == “began” then
  if isPedalActive = true then  <------ main lua 200
    media.playEventSound(cPedal)
  end
end

I know the ID table method but I think I’d rather to make them (keys) individually, maybe that will take some time but it’s still easier for me, first project shouldn’t be the hardest

I also have idea to make note letters on keys that can be required by button, so all in all, what I would like to do - 2 pedals and note button

So can you give me down here, if you can of course, a sample of one key’s complete code, including those features and needed locals, falses and trues etc.?

and I’m planning to have all 88 keys i guess :mellow:

thanks

Hi @llordglassenward87,

Welcome to Corona! One starting piece of advice is, I don’t recommend that you use the “media.*”-based audio functions like “media.playEventSound()”. These are very old and have a number of limitations, whereas the OpenAL APIs in the “audio.*” library will typically serve you much better.

Here’s a guide for understanding how it all works:

https://docs.coronalabs.com/guide/media/audioSystem/index.html

Best regards,

Brent

As for your error, when using if statements and checking for equality you need to use two equal signs, not one.

local num = 0 if num == 0 then \<-- See the difference? end

Also, I recommend you follow Brent’s advice, I’ve seen the audio libraries a couple of times, but I have never seen the media library before now.

https://docs.coronalabs.com/guide/media/audioSystem/index.html

I know you want this project to be simple but building 88 keys separately is not a good idea. Time-consuming and redundant, I recommend not to do it this way, to be frank, it’s painful.

As for the sample of one key’s complete code, do you have the audio and image files for one key and two pedals? If you do, zip it up and post it here and I will try to help in my spare time. (9th grade is starting to pile on the homework)

Also, to test this project out properly, you can’t use Corona Simulator. I am assuming you want the user to press pedals and keys at the same time.

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html#multitouch

At this moment I dont have audio files and graphics. Just using one sound and regular white rectangle a as key beascue at first I’d like to figure out all needed coding stuff and when it’s done then just assort best sounds etc.

I’ve seen some libraries but still dont know how to compactibile everything into one :frowning:

I know multitouch is needed. By the way how about zooming? If out there’s 88 keys all of them would be tiny. I don’t want to make scrollview, my idea was to see whole keyobard when you open app and just individually pinch zoom it in in your own preference. Is it possible as I had in my plans to just zoom it by your mobile like you can do the same with photos? I dont know how would it work with running corona if it would ever work (now I see it’s nooby since ive been learning some more things) So what about zooming with code… but key work isnt still done. Ummmm what shall we do for now, what’s more important, what do you recommend?

Sorry for my a bit awkward english :unsure:

@llordglassenward87 - definitely switch the audio library (guide) for improved functionality.

For zooming:  I think you will need a zoom button as multitouch will be busy sensing multiple fingers on the keyboard and won’t know when you want to zoom instead of play without some sort of toggle button.

Keyboard: Each octave will have 12 unique keys so concentrate on getting one octave right and then it will be easy to duplicate it 7 times.

Sound table:  Again, I’d say just start with one octave.  Make a table that contains each keyboard key which in turn will be a table of sounds (audio.loadSound( ) ) arranged in some orderly fashion so they are easy to reference later.

[lua]

local soundTable = {

     a1 = {

          regular      =  audio.loadSound(“a1.mp3”),  

          sustained    =  audio.loadSound(“a1sustained.mp3”),    

          effectX      =  audio.loadSound(“a1EffectX.mp3”)

          },

    b1 = {

          regular      =  audio.loadSound(“b1.mp3”),  

          sustained    =  audio.loadSound(“b1sustained.mp3”),    

          effectX      =  audio.loadSound(“b1EffectX.mp3”)

          },

     c1 = {

          regular      =  audio.loadSound(“c1.mp3”),  

          sustained    =  audio.loadSound(“c1sustained.mp3”),    

          effectX      =  audio.loadSound(“c1EffectX.mp3”)

          }

}

audio.play( soundTable.a1[“regular”] )

audio.play( soundTable.a1[“sustained”] )  

audio.play( soundTable.a1[“effectX”] ) 

[/lua]