:setPitch() problem for a Tone Matrix Program

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Karasuro
Prole
Posts: 19
Joined: Wed Aug 21, 2013 5:32 am

:setPitch() problem for a Tone Matrix Program

Post by Karasuro »

I'm working on a Tone Matrix and the only thing I need to finish it is having it actually play the correct tone depending on the button that is active.

I have a table for each button that includes the value of its pitch (e.g 1, 1.25, 1.42, etc.) and I use a FOR loop to get the Pitch value from the table. However, it's only giving me the first button's pitch value.

Code: Select all

buttons = {
        {
            {["state"]=3, ["posX"]=16, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=56, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=96, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=136, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=176, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=216, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=256, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=296, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=336, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=376, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=416, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=456, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=496, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=536, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=576, ["posY"]=16, ["pitch"]=4},
            {["state"]=3, ["posX"]=616, ["posY"]=16, ["pitch"]=4}
        },
... etc etc
and the function for getting the pitch:

Code: Select all

function getBtnPitch()
    for k in pairs(buttons) do
        for l in pairs(buttons[k]) do
            return buttons[k][l].pitch;
        end
    end
end

function playTone(pitch)
    source:setPitch(pitch);
    love.audio.play(source);
end

here's the function that calls the getBtnPitch():

Code: Select all

        -- Get Active Notes
        for a in pairs(buttons) do
            for b in pairs(buttons[a]) do
                if (header.posX == buttons[a][b].posX) then
                    if (buttons[a][b].state == 1) then
                        buttons[a][b].state = 2;
                        getBtnNote();
                        pitch = getBtnPitch();
                        playTone(pitch);
                    end
                else
                    if (buttons[a][b].state == 2) then
                        buttons[a][b].state = 1;
                    end
                end
            end
        end

I may be going about it the wrong way..

Updated for 11.2
Attachments
AudynMatrix.love
(38.52 KiB) Downloaded 22 times
AudynMatrix.love
(38.52 KiB) Downloaded 24 times
Last edited by Karasuro on Sun Feb 24, 2019 5:53 am, edited 4 times in total.
User avatar
Karasuro
Prole
Posts: 19
Joined: Wed Aug 21, 2013 5:32 am

Re: :setPitch() problem for a Tone Matrix Program

Post by Karasuro »

I've fixed my pitch updating problem but love's :setPitch crashes whenever I give it a "2.32" value.

Now I just need to clean up the code. x.x
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: :setPitch() problem for a Tone Matrix Program

Post by slime »

Does it cause a Lua error (blue screen in the love window with an error message), or a hard crash ("love has stopped working" or similar in Windows)?
User avatar
Karasuro
Prole
Posts: 19
Joined: Wed Aug 21, 2013 5:32 am

Re: :setPitch() problem for a Tone Matrix Program

Post by Karasuro »

Hard Crash. I tested it with all my tones, I even changed the pitches by 0.01.. :setPitch(2.32), every time it would hit the program would crash. White out, pop-up with "Love has stopped Working" window.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: :setPitch() problem for a Tone Matrix Program

Post by micha »

Your function

Code: Select all

function getBtnPitch()
    for k in pairs(buttons) do
        for l in pairs(buttons[k]) do
            return buttons[k][l].pitch;
        end
    end
end
Does not iterate over all entries. As soon as the first inner loop is run, it returns, which means that the function is aborted and the value of buttons[k][l].pitch is handed back.

In the code, that calls it, just replace this

Code: Select all

pitch = getBtnPitch();
by

Code: Select all

pitch = buttons[a][b].pitch
Because at this point you already know which button you want.
User avatar
Karasuro
Prole
Posts: 19
Joined: Wed Aug 21, 2013 5:32 am

Re: :setPitch() problem for a Tone Matrix Program

Post by Karasuro »

Thanks Micha. That's what I did wrong. I got rid of the function altogether. :)
The finished program is in the original post.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: :setPitch() problem for a Tone Matrix Program

Post by kikito »

This seems like a good moment to plug the First Rule of Programming :D
When I write def I mean function.
User avatar
Karasuro
Prole
Posts: 19
Joined: Wed Aug 21, 2013 5:32 am

Re: :setPitch() problem for a Tone Matrix Program

Post by Karasuro »

I totally agree Kikito. Which is why I was toying with the pitches, tones, etc. Honestly, I don't even need the pitch so It's irrelevant at this time.
Post Reply

Who is online

Users browsing this forum: No registered users and 215 guests