Author Topic: How to play a sound "n" seconds after the layout start?  (Read 17522 times)

fbs777

  • Jr. Member
  • **
  • Posts: 15
    • View Profile
How to play a sound "n" seconds after the layout start?
« on: August 17, 2016, 01:17:31 AM »
Im trying to do this without success. I want to start the layout then after "x" seconds call the mp3:

local sound = fe.add_sound( "sound.mp3" );
sound.playing=true;
« Last Edit: August 18, 2016, 12:39:51 AM by fbs777 »

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: How to play a sound "x" seconds after the layout start?
« Reply #1 on: August 17, 2016, 12:30:32 PM »
You need to loop your audio and then add a transition callback. If the transition time is greater than X, then stop playing the audio.

http://attractmode.org/docs/Layouts.html#add_transition_callback

fbs777

  • Jr. Member
  • **
  • Posts: 15
    • View Profile
Re: How to play a sound "x" seconds after the layout start?
« Reply #2 on: August 17, 2016, 04:51:45 PM »
i have tried a lot of things and nothing works
Btw, i dont want to play in loop, just once

this will play the file once just after start the layout:
Code: [Select]
local sound = fe.add_sound( "sound.mp3" );local i=0; i<30000; i++;
if ( i = 30000 ) {
sound.playing=true;
}

this will try to play the file every single milisecond just after start the layout:
Code: [Select]
fe.add_ticks_callback( this, "_timer" );
local timer = 0;
function _timer( ticks ) {
timer = ticks;
if ( timer = 30000 ) {
local sound = fe.add_sound( "sound.mp3" );
sound.playing=true;
return false;
}
else
{

return false;
   }
}


This will work almost as expected, but this is inside the Transition.StartLayout, so none of the layers/surfaces will start until the time "x", so its useless...
Code: [Select]
fe.add_transition_callback( "orbit_transition" );
function orbit_transition( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.StartLayout:
if ( ttime < 30000 ) {
return true;
}
else
{
local sound = fe.add_sound( "sound.mp3" );
sound.playing=true;
return false;
                }
break;
}
return false;
}

This is even worse because will wait for the "x" time before load any layout content, so is even more useless:
Code: [Select]
local sound = fe.add_sound( "sound.mp3" );
for ( local i=0; i<30000; i++ )
sound.playing=true;

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: How to play a sound "x" seconds after the layout start?
« Reply #3 on: August 17, 2016, 11:07:19 PM »
Try something like the following.

Code: [Select]
local sound = fe.add_sound( "sound.mp3" );
sound.playing=false;
fe.add_transition_callback( "sound" );
function sound( ttype, var, ttime ) {
  switch ( ttype ) {
    case Transition.StartLayout:
      if ( sound.playing == flase) sound.playing = true;
    break;
  }
}

fbs777

  • Jr. Member
  • **
  • Posts: 15
    • View Profile
Re: How to play a sound "x" seconds after the layout start?
« Reply #4 on: August 18, 2016, 12:34:06 AM »
Try something like the following.

Code: [Select]
local sound = fe.add_sound( "sound.mp3" );
sound.playing=false;
fe.add_transition_callback( "sound" );
function sound( ttype, var, ttime ) {
  switch ( ttype ) {
    case Transition.StartLayout:
      if ( sound.playing == flase) sound.playing = true;
    break;
  }
}
Sorry, but where is the code to declare the amount of time to wait before the mp3 starts?
And as i said before, using the "case Transition.StartLayout:" will work almost as expected, but this is inside the Transition.StartLayout, so none of the layers/surfaces will start until the code reach the time declared


Maybe the title of this topic is a bit confuse because the "x" instead of "n", so i will write again: How to play a sound 30 seconds after the layout start?

The point is: how to use an delay option like the delay = n used in the animation module to declare the amount of time (in miliseconds since the layout start) to wait to execute the command?
« Last Edit: August 18, 2016, 12:38:36 AM by fbs777 »

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #5 on: August 18, 2016, 03:23:16 AM »
I'm doing this on a cell phone as I no longer have a home isp. I can't do everything for you.

You could have your ticks function in the root of your code. Have the transition function set a boolean variable to true, and have your ticks function look for that variable. Then it will wait until ticks is greater than the current tick time + wait time, then play sound.

I personally would make all this an object. Use a contructor to set your variables and trigger your functions.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #6 on: August 18, 2016, 03:43:34 AM »
Untested, best I can do. Something like this. In a hurry before work.

Edit: forgot add ticks callback for timer function

fbs777

  • Jr. Member
  • **
  • Posts: 15
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #7 on: August 19, 2016, 12:08:19 AM »
Untested, best I can do. Something like this. In a hurry before work.

Edit: forgot add ticks callback for timer function
Thank you, you even added an option to modify the path and timer in the display menu, but unfortunately this dont worked too.

This is the code without options in the display menu testing with 5 seconds of delay declared:

Code: [Select]
class StartLayoutAudio {
active = false;
time =0;
delay = null;
starttime =0;
sound = null;

constructor( path, sec ) {
delay = sec.tointeger()*1000;

sound = fe.add_sound( path );

sound.playing = false;

fe.add_transition_callback( this, "Trigger" );
fe.add_ticks_callback( this, "Start" );
}

function Timer( ttime ) {
time = ttime;
}

function Trigger( ttype, var, ttime ) {
switch ( ttype ) {
case Transition.StartLayout:
starttime = time + delay;
active = true;
break;
}
}

function Start( ttime ) {
if ( active && (ttime >= starttime ) )
sound.playing = true;
active = false;
}
}

local start_layout_audio = StartLayoutAudio( "sound.mp3", "5" );

Using this code the sound doesnt play in anytime and dont show any error on the console

The only way to work is changing the delay value to "1" or "2" ("3" or any number higher will not work) but the sound will play just after start the layout...

If i change the >= in the line    if ( active && (ttime >= starttime ) ) the result is:
from ">=" to "<=", "<", or "=": The sound starts with zero delay
from ">=" to ">", or "==": The sound doesnt play in anytime
« Last Edit: August 19, 2016, 12:28:50 AM by fbs777 »

raygun

  • Administrator
  • Sr. Member
  • *****
  • Posts: 393
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #8 on: September 06, 2016, 01:42:11 PM »
Untested, best I can do. Something like this. In a hurry before work.

Edit: forgot add ticks callback for timer function
Thank you, you even added an option to modify the path and timer in the display menu, but unfortunately this dont worked too.

This is the code without options in the display menu testing with 5 seconds of delay declared:

Code: [Select]
class StartLayoutAudio {
active = false;
time =0;
delay = null;
starttime =0;
sound = null;

constructor( path, sec ) {
delay = sec.tointeger()*1000;

sound = fe.add_sound( path );

sound.playing = false;

fe.add_transition_callback( this, "Trigger" );
fe.add_ticks_callback( this, "Start" );
}

function Timer( ttime ) {
time = ttime;
}

function Trigger( ttype, var, ttime ) {
switch ( ttype ) {
case Transition.StartLayout:
starttime = time + delay;
active = true;
break;
}
}

function Start( ttime ) {
if ( active && (ttime >= starttime ) )
sound.playing = true;
active = false;
}
}

local start_layout_audio = StartLayoutAudio( "sound.mp3", "5" );

Using this code the sound doesnt play in anytime and dont show any error on the console

The only way to work is changing the delay value to "1" or "2" ("3" or any number higher will not work) but the sound will play just after start the layout...

If i change the >= in the line    if ( active && (ttime >= starttime ) ) the result is:
from ">=" to "<=", "<", or "=": The sound starts with zero delay
from ">=" to ">", or "==": The sound doesnt play in anytime

try updating your Start function in StartLayoutAudio to this:

Code: [Select]
function Start( ttime ) {
if ( active && (ttime >= starttime ) )
                {
sound.playing = true;
active = false;
                }
                Timer( ttime );
}

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #9 on: September 06, 2016, 09:14:36 PM »
^^^^ yep. Forgot to call the timer function.

fbs777

  • Jr. Member
  • **
  • Posts: 15
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #10 on: October 01, 2016, 05:14:34 PM »
try updating your Start function in StartLayoutAudio to this:

Code: [Select]
function Start( ttime ) {
if ( active && (ttime >= starttime ) )
                {
sound.playing = true;
active = false;
                }
                Timer( ttime );
}
Yes, now its working, thank you guys

incy2k

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: How to play a sound "n" seconds after the layout start?
« Reply #11 on: October 07, 2016, 11:13:16 PM »
Ok so this may be somewhat off topic sorry.
Thought id seen something like this somewhere on the forum after discussing the use of per layout ambient audio.Would it be possible to use this code for background ambient audio similar to what recalbox uses on the main console selection menu (not sure how well this would work alongside snap video audio) or to trigger per layout console power on sounds (sega) . Also i see retrofe has triggers on the following events: "load" (page load), "unload" (page exit), "highlight" (scroll), "select" (entering game/sub-menu) mentioned in the configuration documentation.

Attract mode appears to have far more configurable sounds : select, up, down, page up, page down, previous display, next display, displays menu, previous filter, next filter, filters menu, toggle layout, toggle movie,toggle mute, toggle rotate right, toggle flip, toggle rotate left, exit (confirm), exit to desktop, screenshot, configure, random game, replay last game, add/remove favourite,previous favourite, next favourite, add/remove tags, screen saver, previous letter, next letter, intro, exit game, custom1-6, ambient soundtrack, startup sound, game return sound.

But as there is little documentation on sound (I had to manually check my config menu to list these). I am curious can sound files be applied to these settings by a layout or must they be pre configured by user and thus static. There seems to be little work done on using sounds within attract mode i see plenty of great work on graphics for layouts. But i have yet to come across a layout that has included sounds or simply a stand alone sound pack.
So thank you for coding something related to sound and for reading this long and off topic post. I know some may feel sounds are annoying but i enjoyed switching sound packs on my pc and the classic windows 95 startup and shutdown sounds and hyperspins wheel switch sounds. I just hope to improve my attract mode experience.