Incremental Color Change Over Long Periods

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Sat Mar 08, 2014 10:14 pm

You can use mp3 if you use a player object and set the player's filename to the mp3 file on disk. It will play back using QuickTime on Mac or Media Player on Windows. If you want to import the sound file into the stack directly then there are fewer options; only uncompressed wav, au, or aiff. Those formats are old, produce large files, and offer little control except for basic playback, so most of us use players. Imported audio clips are largely deprecated now.

But even if you use an mp3 you won't want to make one that plays for 24 hours, it would be huge. Since it's just white noise, you don't need much length, you can just loop a very short segment. The fades can be included in the sound file and then the script would just loop the center portion for the duration of the test, or you can fade the sound in/out with a script by controlling the volume in increments the same way the color is changed -- by sending a message at intervals to increase or decrease the volume. That's probably how I'd do it, for a couple of reasons: 1) you can change the fade durations as needed without needing to create new sound files, and 2) you won't have to calculate where the fades are in the file in order to script the main sound loop around them. Take a look at the "playLoudness" command, which lets you set the volume of either an embedded sound clip or a player object.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Sun Mar 09, 2014 10:15 am

I knew a 23-hour mp3 would be huge, even at 128 or 96kbps... but I guess I had just sort of accepted that as inevitable. I was planning to do this because the idea was to use a logarithmic fade, but I suppose a linear increase at regular intervals would be okay too.

Probably what I will do is integrate a short WAV then, perhaps 400 seconds or so. So I will simply need to loop the sound continuously (can this looping be gapless?) until the backgroundcolor reaches its maximum value (255), though the volume increases will occur at different intervals (equal to the intervals for light change though) in each condition to produce different rate increases.

Is that a good way to think about it? If so, I would think the code would be really similar to the light code, even linking to the same variable conditions most of the time..

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Sun Mar 09, 2014 9:14 pm

My brain kind of freezes up whenever someone uses a math term, but in general I don't see why you couldn't implement a logarithmic fade as well as any other kind. The only difference would be the <shudder> math involved. You'd still send an updated volume setting at timed intervals. If the timing isn't regular, then you just calculate and change the amount of time in every "send" command.

Isn't white noise pretty consistent throughout? I would think a 5 second looping clip would be plenty. The player object has a "loop" setting that you just turn on in the property inspector (or via script.) Just load the file name into a looping player , script it to start up, and you're done -- except for the fades of course, but that doesn't affect what the player is actually doing, only its volume.

The loop will be gapless if you don't have any silences at the beginning and end of the audio clip. That said, an audiophile has told me that he finds it impossible to seamlessly loop an mp3. Try it, I guess. If you hear a gap, go with the .wav instead.

The volume fade will be very similar to the light script, you're right. Ask if you get stuck.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Mon Mar 10, 2014 8:09 am

Okay, I took your advice. I took a 10-second clip of white noise, tacked 10 seconds of the clip playing backward (reversed) onto the end--and saved this as a 20-second sample (I reversed it so that the end of the file and the beginning of the file have the exact same amplitude, to be sure there would be no audible click when looped). Then I was perplexed because it imported fine but wouldn't play when called in the script... it turns out 32-bit versions won't work, only 16-bit ones. When I figured that out and it played fine, I just linked sound volume increases to light level increases by rounding (tValue / 2.55) <that is, (tValue/255)*100> to the nearest integer to get a proportional percentage to put into playLoudness. This is just linear volumization, but that's okay for now. Next, I'm going to have to integrate the sound into the F6 pausing function as well--this seems to be a bit more complicated because I'll have to stop the file from playing and then call it again at the previous level... tomorrow's task.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Mon Mar 10, 2014 6:19 pm

Good move matching up the beginning and end of the sound file. I hadn't thought of that.

Integrating a pause into the F6 function is a piece of cake. See the "paused" property for players. The player will retain its current playback position, so all you need to do is set the paused of the player to true/false.

You're coming right along here.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Mon Mar 10, 2014 8:35 pm

Well I have the audio clip integrated into the program... so I'm technically not using a player right? I'm just using the play and play stop functions. Can I still easily set up the pause?

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Tue Mar 11, 2014 5:34 am

Never mind about the pause for the sound... I've got all of that the way I want it.

My next question is this: I want to put an options menu on Card 1 that affects which R,B,G values change on the next card that opens. Basically, this would let the user select the color (variable) that is faded in slowly over time. Right now, the color variable that is slowly faded is called "tValue." So, for instance, if the case of the options menu is "Red" when the next card is called, I need some script to place tValue,0,0 in the backgroundcolor (a slow fade to red) rather than tValue,tValue,tValue, which would be (a slow fade to) "White." I'm just not sure how to call states between cards to reference different commands etc.

I just want to reference a certain command within the script of Card 2 based on the case of an options menu label located on Card 1.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Tue Mar 11, 2014 5:54 am

Check out the "global" command in the dictionary.
Pay particular attention to this;
* If you declare a global command in a script, but outside any handlers in the script, the global can be used by any handler that comes after the global declaration in that script. You don't need to declare such a global again in the handler itself.
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Tue Mar 11, 2014 6:38 am

Yes, I looked into global for a good deal of time. But in the end I'm not sure if I should be declaring the options menu items as global, the commands on the subsequent cards, or something else... plus I couldn't figure out the syntax for using a global later. Maybe I'm missing something simple...

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Tue Mar 11, 2014 6:52 am

It's really easy!
At the top of the script write
global myGlobal, myOtherGlobal (that makes two of them).
Then

Code: Select all

put "red" into myGlobal
Every time you want to use it you just declare it again

Code: Select all

global myGlobal
on mouseUp
answer myGlobal
end mouseUp
Anywhere you want to use it.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Tue Mar 11, 2014 10:12 am

Sorry, I still can't figure out exactly how to implement this. Here's what I have for Card 2:

Code: Select all

local sOnState -- our on/off flag; this variable is available to all handlers in this script

on openCard
   send "Startsequence" to me in .1 secs
end openCard

on startSequence
  set the fullscreen of this stack to true
  set the backgroundcolor of this stack to "0,0,0" -- black
  set the playLoudness to 0--of audioclip WN20_16bit.wav to 0
  play audioClip WN20_16bit.wav looping
  put true into sOnState -- set flag to "on"
  send "do_nextsetting" to me in 1 second -- start the timer
end startSequence

-- this handler does the work. If the state flag is "on" it updates the color.
-- if the flag is "off" it does nothing. Since the next timer message
-- is only sent when "on" is true, the timer automatically stops if the on-state is false
command do_nextsetting -- we'll read the color directly here
  if sOnState = true then -- state is "on"
    -- since all 3 items are the same, we only need to look at one of them:
    put item 1 of the backgroundcolor of this stack into tValue
    if tValue < 255 then  -- it isn't white yet
      add 1 to tValue -- increment
      set the backgroundcolor of this stack to tValue,tValue,tValue
      set the playLoudness to the round of (tValue / 2.55)
      send "do_nextsetting" to me in .1 second -- keep the timer going
    end if
  end if
end do_nextsetting

on functionKey pKey -- "pKey" will be the number of the function key that was pressed, this is the state toggle
  if pKey = 6 then -- check for the correct key
    put not sOnState into sOnState -- toggles the state flag true/false
    if sOnState = true then -- state is "on", resume:
      send "do_nextsetting" to me in 1 second -- restart the timer
    end if
    -- we don't need to do anything if it is off; 
    -- the do_nextsetting handler won't run because the flag is false
  end if
end functionKey

on escapeKey -- abort everything
  put false into sOnState
  set the fullscreen of this stack to false
  -- optional:
  set the backgroundcolor of this stack to "255,255,255" -- white
  play stop
end escapeKey

on mouseUp
    send "Startsequence" to me in .1 secs
end mouseUp
All I want to do is have an option menu on the previous card that has four settings: "white", "red", "green", and "blue". When a separate start button is pressed, the color-change card is initated. But the color specified in the menu label on Card 1 at the time the start button is clicked should determine which of the backgroundcolor variables will be linked to the tValue changes over time. (white: "tValue,tValue,tValue"; red: "tValue,0,0"; green: "0,tValue,0"; blue: "0,0,tValue") I know this must be really simple, I just can't seem to get the methodology.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Incremental Color Change Over Long Periods

Post by Simon » Wed Mar 12, 2014 12:20 am

Hi,
I can see you are using a local, a global is just the same but remains from card to card.
global gMyColor
on mouseUp
put "red" into gMyColor
end mouseUp
Now as long as the app is running and you don't put anything else into gMyColor it will always contain "red".
Oh you will have to script each condition.
if gMyColor = "red" then
tValue,0,0
end if

You could just use separate cards and skip the whole global thing, then you wouldn't need the conditional.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Wed Mar 12, 2014 2:51 am

I thought about using separate cards... But I got the global variables working and I'm satisfied for the moment. White, red, green, and blue are set up, and now I'm working on cyan, yellow, and magenta.

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: Incremental Color Change Over Long Periods

Post by theotherbassist » Wed Mar 12, 2014 2:59 am

I think I will also add options for sound: white noise, brown noise, pink noise, or tone. Livecode is not so daunting once you get the patterns down.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Incremental Color Change Over Long Periods

Post by jacque » Wed Mar 12, 2014 3:47 am

I think you're on the right track by keeping the script to a single card and just changing the variables. Have you programmed in any other languages? You're picking this up pretty quick.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”