Board index FlightGear Development Canvas

canvas.playthesong.new method in How to control sound

Canvas is FlightGear's new fully scriptable 2D drawing system that will allow you to easily create new instruments, HUDs and even GUI dialogs and custom GUI widgets, without having to write C++ code and without having to rebuild FlightGear.

canvas.playthesong.new method in How to control sound

Postby Michat » Sat Jul 05, 2014 6:08 pm

Howdy. I wish to know if there is a method to play a sound via canvas, or How I could play a sound. My idea is to launch a sound when chrono starts and stop, maybe a recorded voice or maybe via TTS more complex method. So I'm interested in learning some basic canvas nasal code. Enjoy with the Spaghetti and be free to share your ideas comments and mirths. Initial code By TheTom, Revisited by D-LEON (main loop). Dirt by me.

Code: Select all
var dlg = canvas.Window.new([152,74]);
var my_canvas = dlg.createCanvas()
                   .setColorBackground(0,0,0);
var root = my_canvas.createGroup();
my_canvas.addEventListener("drag", func(e) { dlg.move(e.deltaX, e.deltaY); });
root.createChild("image")
          .set("file", getprop("/sim/fg-home") ~ "/Nasal/rallytimer/rallytimer.jpg")

# Some users experimented problem with the path to the root so image won't display. I.e following D-LEON tip I had to create a copy of the nasal folder under my GNu .fgfs directory. While my ppasarcot distribution put Nasal under usr/share/games/flightgear. Users using windows have similar problem with the path to the image causing same result image didn't shown. I'm scared about this, because I need full compatibility Among OS and easy install method for the rally timer.
          .setSize(152,74);

# I wonder here how to manage a sound. So I've decided first creating a Child from root like Child("Sound") .set ting  and getprop 'in to  the sound file.
root.createChild("Sound")
          .set("file", getprop("/sim/fg-home") ~ "/Nasal/rallytimer/startingtimer.ogg")

# Do I'll need here a  .set like          .setStart(play/value); ??????

var text =
  root.createChild("text", "dialog-caption")
          .set("text", "00:00:00")
          .set("alignment", "center-center")
          .set("character-size", 18)
          .set("font", "LiberationFonts/LiberationSans-Bold.ttf")
          .set("fill", "#ffffff")
          .setTranslation(76, 45);
var textadv =
  root.createChild("text", "dialog-caption")
          .set("text", "00:00:00")
          .set("alignment", "center-center")
          .set("character-size", 18)
          .set("font", "LiberationFonts/LiberationSans-Bold.ttf")
          .set("fill", "#ffffff")
          .setTranslation(76, 45);

# pointed by Hooray

var sound = {
path : DIRECTORYPATH,
file : FILENAME ,
volume : VOLUME};

fgcommand("play-audio-sample", props.Node.new(sound) );
------------------------------------------------------------------------------------------


[s]
var soundstart =
  root.createChild("Sound", "dialog-caption")
         # .set("text", "Start00:00:00")
         #. set("alignment", "center-center")
         # .set("character-size", 18)
         #.set("font", "LiberationFonts/LiberationSans-Bold.ttf")
         #.set("fill", "#ffffff")
         # .setTranslation(76, 45);
[/s]

# I wonder here if I have .set for sounds like .set (play)

var speed = props.globals.getNode("/velocities/groundspeed-kt", 1);
var dt = props.globals.getNode("/sim/time/delta-sec", 1);
var passed_time = 0;

var timer = maketimer(0, func {
  if( speed.getValue() > 16 )
    passed_time += dt.getValue();
 
  var frac = passed_time - int(passed_time);
  text.set("text", sprintf("%02d:%02d:%02d", passed_time / 60, math.mod(passed_time, 60), frac * 100));
# Chrono Timer code is working okay but now I wish to play some other text variables as I declared after the original vartext

 # creating here a new print

 textadv.set("text", sprintf("Rally Timer Activated));

# so doing the same I suppose here I need to set a value in order to launch the sound  with soundstart.set("sound", play ("please)); ???
});
timer.start();



Salut.
Last edited by Michat on Sat Jul 05, 2014 8:53 pm, edited 3 times in total.
User avatar
Michat
 
Posts: 975
Joined: Mon Jan 25, 2010 6:24 pm
Location: Spain
Version: 191b
OS: GNewSense

Re: canvas.playthesong.new method in How to control sound

Postby Hooray » Sat Jul 05, 2014 6:22 pm

Canvas is primarily about 2D rendering, i.e. drawing images, symbols etc.
Obviously, playing sounds is not part of Canvas - but you can play sounds using the same method that aircraft are using: http://wiki.flightgear.org/Nasal_FAQ#Ho ... _script.3F

for examples on how to do this from Nasal, please refer to the chatter.nas module in $FG_ROOT/Nasal
EDIT: chatter.nas has been recently removed, but it works via property-fgcommands as per $FG_ROOT/Docs/README.commands:
Code: Select all
var sound = {
path : DIRECTORYPATH,
file : FILENAME ,
volume : VOLUME};

fgcommand("play-audio-sample", props.Node.new(sound) );


You'll want to adjust the capital variables with your own path/file name and volume.

TTS etc are all property-based, so can be accessed from nasal via setprop/getprop or props.nas
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11427
Joined: Tue Mar 25, 2008 8:40 am

Re: canvas.playthesong.new method in How to control sound

Postby Michat » Sat Jul 05, 2014 9:13 pm

Thank you for the Tip I'm reading now wiki nasal. About chatter.nas that you say is removed I have here Nasal/atc-chatter/atc-chatter.nas I suppose is not the same you mention, right?

In your opinion what is the best way to use a sound for this timer?. Mounting sound via TTS or with var sound path. I imagine the last one. But also I mean in the sense of better performance, also one thing concerns to me, the package must be easy to install. For the moment I have one folder Nasal/canvas/rallytimer/rallytimer. jpg/png and one rallytimer.nas. Imago is all I need. So I call access to the chatter properties via my.nas file. Right? I wish and easy drag and drop single folder method for install. Just a single folder. Right?

Thank you for your tips.
User avatar
Michat
 
Posts: 975
Joined: Mon Jan 25, 2010 6:24 pm
Location: Spain
Version: 191b
OS: GNewSense

Re: canvas.playthesong.new method in How to control sound

Postby Philosopher » Sat Jul 05, 2014 9:47 pm

I think you could put your sound file in the same folder and load it from there, if you aren't using an existing sound.
Thanks,
Philosopher
(inactive but lurking occasionally...)
Philosopher
 
Posts: 1590
Joined: Sun Aug 12, 2012 6:29 pm
Location: Stuck in my head...
Callsign: AFTI
Version: Git
OS: Mac OS X 10.7.5

Re: canvas.playthesong.new method in How to control sound

Postby Michat » Sat Jul 05, 2014 11:35 pm

Sounds look to me. Thank you.
User avatar
Michat
 
Posts: 975
Joined: Mon Jan 25, 2010 6:24 pm
Location: Spain
Version: 191b
OS: GNewSense

Re: canvas.playthesong.new method in How to control sound

Postby Hooray » Sun Jul 06, 2014 3:54 am

Besides, regarding your file (""/Nasal/rallytimer/startingtimer.ogg"") I am not sure if FG supports many formats beyond just wav files - check $FG_ROOT to be sure, I think there are a few MP3 files, too.
Also, you may want to move your pseudo code over to the wiki where others can review, help and edit the code more easily. There are some issues with it, and some of the original "canvas/sound" ideas should probably be removed. But otherwise, I see no problem adding a little tutorial that demonstrates how to create a widget that plays a sound using the method I posted - it's a Nasal callback (function) after all, and it doesn't matter what you do there. I can help and clean up the code accordingly, and also make it work with Canvas/GUI Widgets.

However, your original goal was to just play a sound once the timer starts and stops - and that is something that would not have to do anything with Canvas at all. You would merely add the corresponding Nasal lines to the beginning of your timer, and the end respectivly - the code would not need to do anything related to Canvas at all, it would not even need to be aware of the fact that it got triggered through canvas.

If you could post your rallye/timer Nasal code again, we can easily show you where to put the corresponding lines of code, even without any Canvas dependencies.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11427
Joined: Tue Mar 25, 2008 8:40 am

Re: canvas.playthesong.new method in How to control sound

Postby Michat » Sun Jul 06, 2014 1:27 pm

Regarding the sound format. It is mandatory that the mp3 must be created in Mono?

Agree with you, I believe that rally timer can be a good example for canvas widgets, open a window with a background image and calling to a sound (nasal code). So maybe can include in wiki canvas widgets section.

My original idea also should include a way to report results via http:// thing that for sure will be more difficult to achieve.

So I will share here my initial pseudo code with some comments , then we can discuss what is the better way to do it: advantages and limitations. Also I will explain exactly in what the FGrally consists. How does it works, and how it can help to the entire FGMP community events.

I'll do this evening.

Thanks for your help.
User avatar
Michat
 
Posts: 975
Joined: Mon Jan 25, 2010 6:24 pm
Location: Spain
Version: 191b
OS: GNewSense

Re: canvas.playthesong.new method in How to control sound

Postby Michat » Sun Jul 06, 2014 11:58 pm

I'm running out of time, next Tuesday I'll move out of my cockpit and I'll have no internet where I go to.

I'm sorry that I have no time to create a new wiki page, please be free to place the code where you want.


Here you have the original code by the tom, checked by D-Leon (main loop). https://cloud.openmailbox.org/public.php?service=files&t=41a997dad032579ca7ab93b717eeab89


Rally timer. How it works?.

With the flight test results (linear stage, no live weather and wind calms) that pilots have reported to us using the rally timer during first week of competition, we can calculate a time/distance/speed relation for next non linear stages that will use live weather and winds.

I.e. Test fly 50 nm. Pilot report to us 50 minutes of flight time. So speed was 50 Kias. With that data related we can estimate a target time to each pilot's next stage.
I.e Stage 2 distance 100 nm, so target time is 100 minutes (1 hrs 40 minutes) = double time. Considering 100 minutes the 100% of the target time reached. We can calculate flight accuracy with respect of each pilots/aircrafts target times. The more accurate pilot wins. Pilots not only compete each others but also they compete against themselves. Winds will do the rest :) Each week FGrally org will unveil info, results and target times for next leg.

Pseudo code: Please be free to avoid any hard technical problem point that you find impossible to reach.

Wishes:

Find a way where pilot could participate in the rally from the simulator (FGrally information center via canvas), avoiding to use emails and so to report flight times.
Find a way to http:// links to next leg information and target times to the pilots via rally org web page or http canvas get method..
Find a way from rally timer canvas dialog to report the time result.
Find a way to write results to the FG spreadsheet directly to tables fields. If not, look for a hashtable method in order to save/store registers and send them via http:// Something like that.
Find an sponsor for prizes.
Offer more than one rally timer designs, so pilot can choose from several designs.
Many more.



Program Started

FlightGear missions selector.

FGRally -> select an go
-others

cnvas.windows.welcomefgrally.new

Welcome message. (FG Rally Information Center) FgRally Registration is Open/Close (max registration date).

if reg=open then print dlg do you want to join the rally ? Flight Now the flight test. -> Open flight test briefing canvas - > dlg accept the quest - dlg (if not return back) -> position aircraft in ground set winds to 0 set weather off. - > open fgrally timer canvas.

if reg= close then print the (year) fg rally edition is close. Do you wan to compete against the historic record ?(only if there is a second edition). Next expected date for next fgrally is (next rally date)
If accept and historic record exists then position aircraft in ground set winds to 0 set weather off. - > open fgrally timer canvas.


FGrallytimer

Launch sound welcome to the fg rally flight test. Bip Bip.

Taking off (rolling)
If speed > 16 kt

then launch voice Rally Timer activated. Time is running.

Chrono starts.

Landings
If speed<16

then launch voice rally timer disengaged. Timer stopped.

Chrono Stop

Open Dialog :
do you want to report your time? (http:// or whatever method to report)
do you want to try with another aircraft ?(close FG)
Do you want to try again> start flight test routine again.

end of program

--------------------------------------


if you consider this pseudocode :mrgreen:



Thanks for your help.
User avatar
Michat
 
Posts: 975
Joined: Mon Jan 25, 2010 6:24 pm
Location: Spain
Version: 191b
OS: GNewSense


Return to Canvas

Who is online

Users browsing this forum: No registered users and 1 guest