Metronome and Button clicks

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
GregWills
Posts: 69
Joined: Sun Aug 30, 2015 7:51 am

Metronome and Button clicks

Post by GregWills » Wed May 03, 2017 12:37 am

Hi Everyone

I have a metronome type setup, where the user sets the speed of the audio click sound (an audio file). This works fine when running by itself.

The user then clicks on a button to match the speed of the audio sound click. When this happens (or any button click) there is a delay in the audio, and the timer counter (which is displayed on screen).

I'm sure there is a simple solution, I just can't see it at the moment!

Thanks

Greg

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Metronome and Button clicks

Post by [-hh] » Fri May 05, 2017 8:21 pm

We don't have enough info.
Do you have 'blocking' actions?
Did you already try to use "send in <time>"?
shiftLock happens

GregWills
Posts: 69
Joined: Sun Aug 30, 2015 7:51 am

Re: Metronome and Button clicks

Post by GregWills » Sun May 07, 2017 5:04 am

Thank you -hh for replying.

My apologies for forgetting that not everyone can read my mind!!

This is the script in the button to start and stop the metronome. The speed is set by the variable tSpeed

on mouseup
set the cSwitch of me to not (the cSwitch of me is true) -- to start or stop the metronome
if the cSwitch of me is false then set the label of me to "Metronome - Start"
if the cSwitch of me is true then set the label of me to "Metronome - Stop"
metronome
end mouseup

on metronome
if the cSwitch of me is true then
play audioClip id 2196
send "metronome" to me in tSpeed secs
end if
end metronome

Cheers

Greg

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Metronome and Button clicks

Post by [-hh] » Sun May 07, 2017 11:07 am

Your timer sends "metronome" based on its last "round", so it changes the pulse whenever interruptions occur. Thus we have to set the next "pulse" with subtracting the current offset from it:

The following slightly changed setup works here.
It starts the sound exactly every needed pulse for the beats-per-minute. If interruptions occur it goes back into the pulse which is relative to your system clock.

Setting bpm to 60 will pulse exactly with the seconds of your system clock.
[I chose ticks for the timing because this is, TMHO, a "realistic" unit for playing sounds.]

Code: Select all

## Script of button "metronome"
local cSwitch=false

-- tSpeed is trunc(3600/beats per minute) = pulse in ticks
-- sb "speed" is a slider with startvalue 20, endvalue 240 (= bpm)
on metronome
  if not the cSwitch of me then exit metronome
  put trunc(3600/the thumbpos of sb "speed") into tSpeed
  play audioClip id 1005
  -- the ticks mod tSpeed is the current "offset" from tSpeed
  send "metronome" to me in (tSpeed - (the ticks mod tSpeed)) ticks
end metronome

on mouseUp
  set the cSwitch of me to not the cSwitch of me
  if the cSwitch of me then set the label of me to "Metronome - Stop"
  else set the label of me to "Metronome - Start"
  put trunc(3600/the thumbpos of sb "speed") into tSpeed
  send "metronome" to me in (tSpeed - (the ticks mod tSpeed)) ticks
end mouseUp

## Script of scrollbar "Speed"
on scrollbarDrag
end scrollbarDrag

on mouseUp
  play stop
end mouseUp
This works here even if LC is in background.
And it is interrupted in LC only by a menuClick or click on a menu button.

I tested with a button that the user can click and the difference to the next metronome "beat" is displayed in a field "diff" in seconds.

Here is the full setup of my test-stack (incl. sound-clip).
Attachments
soundTest.livecode.zip
(9.25 KiB) Downloaded 266 times
shiftLock happens

GregWills
Posts: 69
Joined: Sun Aug 30, 2015 7:51 am

Re: Metronome and Button clicks

Post by GregWills » Mon May 08, 2017 1:21 am

Thank you -hh for taking the time to provide such a brilliant example and explanation. I’ve looked closely at your stack and learnt from it. However, when I use this code in my stack, I still get a pause in the metronome when I click another button. The 'other' button is sorting a list of words and putting them into a hidden field. Would this be enough of a distraction to 'pause' the metronome? The next metronome beat is very quick (for one beat) after the 'other' button is clicked. It's like these two commands can't be processed at the one time. Can the metronome run in the background without interruptions?

Thanks

Greg

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Metronome and Button clicks

Post by [-hh] » Mon May 08, 2017 2:42 am

GregWills wrote:Can the metronome run in the background without interruptions?
I fear no, in general no. You can only try to shorten or split your other actions.

For example: Did you already try to enclose your blocking-button's script with "locks"?
So that changing the field's contents doesn't start a bunch of other messages that go into the pending messages row before your next metronome pulse?

Code: Select all

on mouseUp
   lock screen; lock messages
   ...
   unlock screen; unlock messages
end mouseUp
shiftLock happens

GregWills
Posts: 69
Joined: Sun Aug 30, 2015 7:51 am

Re: Metronome and Button clicks

Post by GregWills » Mon May 08, 2017 5:19 am

Thanks -hh

No, I haven't tried that. I'll have a play around with it and perhaps change how the user interacts with the metronome and other actions. From what you say, if other commands can be executed quickly, then the interruption, either won't occur, or won't be seen?

Cheers

Greg

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Metronome and Button clicks

Post by [-hh] » Mon May 08, 2017 12:44 pm

GregWills wrote:If other commands can be executed quickly, then the interruption, either won't occur, or won't be seen?
Depends also upon the "play time" of your clip. Make it as short as possible. To have the gaps in between as long as possible.

But we can gain a few millisecs by switching to millisecs as unit. Although I doubt every current hardware is able to play these exactly, this can have influence on the message queuing:

Code: Select all

-- change also other scripts accordingly
on metronome
  if not the cSwitch of me then exit metronome
  -- compute beat-interval in millisecs
  put trunc(60000/the thumbpos of sb "speed") into tSpeed
  play audioClip id 1005
  -- the millisecs mod tSpeed is the current "offset" from tSpeed
  send "metronome" to me in (tSpeed - (the millisecs mod tSpeed)) millisecs
end metronome
Optimize your actions:
  • Surround them by "locks" (lock screen; lock messages).
  • Use variables or custom properties instead of fields for temporary listings.
  • Use items instead of lines or words so that an evaluation becomes fast and easy.
    For example.
    If you collect the offsets of userclicks from the next beat by

    Code: Select all

    put comma & hitVal after hitList
    then you have very cheap, without any sorting, an evaluation by

    Code: Select all

    if char 1 of hitList is comma
    then delete char 1 of hitList -- because empty items are replaced by zero
    put (min(hitList), max(hitList)) & cr& \
    (mean(hitList), standardDeviation(hitList)) into field "result"
    Such an evaluation is so fast that you can probably do that with every new value hitVal.
  • Play the clip on openCard once to have it already loaded (you may use playLoudness 0 of the clip for that).
shiftLock happens

GregWills
Posts: 69
Joined: Sun Aug 30, 2015 7:51 am

Re: Metronome and Button clicks

Post by GregWills » Tue May 09, 2017 3:08 am

Thanks -hh

I appreciate the time you have put into explaining this, as I’m sure are all the many other people who have read this thread.
From your suggestions, I have what I was after for my metronome and 'other buttons': and a far better understanding of what was going on. Again, many thanks.

Cheers

Greg

Post Reply

Return to “Multimedia”