Synth PlayTone

110 views
Skip to first unread message

Alan Hendry

unread,
Nov 19, 2021, 12:22:36 PM11/19/21
to DroidScript
HI,
I expected the code below to play middle C for one second, but it just goes on and on.
Even changing both lengths to .001 it does the same
Similarly for PlayNote(72)
    syn = app.CreateSynth("");
    syn.SetNoteLength(1) ;
    syn.PlayTone(264,1) ;
Regards, ah

Dave

unread,
Nov 19, 2021, 2:12:58 PM11/19/21
to DroidScript
It's in milliseconds. Not sure why it goes on forever (probably a bug), but try this - 

syn = app.CreateSynth("");
    syn.PlayTone(264,1000 ) ;

SetNoteLength() is for use with PlayNote() or PlayMidiTune()

Alan Hendry

unread,
Nov 20, 2021, 7:35:23 AM11/20/21
to DroidScript
Hi,

It looks like the short duration was confusing it.

    syn.SetNoteLength(1) ;
    syn.PlayNote(72)
Gives me a one second note, but I can hear another note that goes on longer.

    syn.PlayTone(264,1000) ;
Plays a note for a second, then another note can be heard that tails off.

I suspect that there's some default of attack/decay/release.

If I syn = app.CreateSynth("Signal"); I don't seem to get any sound at all.

    syn = app.CreateSynth("VCA");
    syn.SetVca(0,0,1,0) ;
    syn.PlayTone(264,1000) ;
Is much better

    syn = app.CreateSynth("VCF");
    syn.SetVcf(0,0,1,0) ;
    syn.PlayTone(264,1000) ;
Plays a very short sound, then about a second of quiet fast clicks

There's SetFrequency, but I don't see what goes with it to play a note.

Ultimately I want to do something like an organ where a note keeps on while you have your finger down.

Regards, ah

Dave

unread,
Nov 20, 2021, 2:58:29 PM11/20/21
to DroidScript
Actually, you might want to look at this very nice piano sample by CandleLight which was created to test/demo the Music component. The music component uses WebAudio BTW.

There are some more very good samples by him that need to be uploaded onto the Store but they are all held in a single SPK and should be broken out into separate demos for clarity...  I'll ask him to do that. 

I broke out the piano sample already though (attached)

I also noticed the Music docs are not showing properly in the WiFi editor, do you see that issue?
Piano.spk

Dave

unread,
Nov 20, 2021, 3:35:58 PM11/20/21
to DroidScript
I've just broken out Cemal's Sequencer demo too.  I think I'll put this in the DS samples as it runs from a single script file.

This is another nice demo and a good starting point for a more complex/capable sequencer :)

/*
 * Simple Music Sequencer by CandleLight
 */

//Declare global variables.
var items = []
var index = 0
var step, blockWidth = (1 / 7) - 0.002 // 7 columns
var synth, note

//Set available notes and colors.
var notes  = ["E5", "D4", "B4", "A#4", "A4", "G4", "E4"]
var colors = ["#A3EEFF", "#FFFDC2", "#C4FFD7"]


//Called when application is started.
function OnStart()
{
    //Configure app to landscape mode, full screen.
    app.SetOrientation("Landscape")
    app.SetScreenMode( "Game" )

//Create a layout with objects vertically centered.
lay = app.CreateLayout( "Linear", "VCenter,FillXY" )

    //Draw sequencer to main layout.
    Draw( lay )
//Add layout to app.
app.AddLayout( lay )
}

//Draw the sequencer.
function Draw( lay ) 
{
    lay.SetBackColor("#EEEEEE")
    var layHoriz, block
    
    for(var row=0; row<3; row++) { // 3 row
        item = []
        layHoriz = app.CreateLayout("Linear", "Horizontal,FillX")
        
        for(var col=0; col<notes.length; col++) { // 7 column
            block = AddBlock(layHoriz, colors[row])
            item.push(block)
        }
        items.push(item)
        
        // lay from main screen.
        lay.AddChild(layHoriz)
    }
    
    //Create the music component.
    music = app.CreateMusic()
    
    //Create a Synthesizer in repeat mode.
    synth = music.CreatePolySynth("Synth", {maxPolyhony: 3});
    music.RepeatTransport( repeat, "4n" )
    music.StartTransport()
}

//Add a sequencer block to the screen.
function AddBlock(layHoriz, color) 
{
    card = app.AddLayout(layHoriz, "Card")
    card.SetMargins(0.001, 0.001, 0.001, 0.002)
    card.SetCornerRadius(8)
    
    block = app.AddText(card, "", blockWidth, 0.33 - 0.002) // margin 0.002
    block.SetOnTouchDown(block_OnTouchDown)
    block.GetChecked = function() { return this.data.checked }
    block.SetBackColor("#FFFFFF")
    block.data.checked = false
    block.data.color = color
    return block
}

//Handle touching a block
function block_OnTouchDown()
{
    this.data.checked = !this.data.checked
    if(this.data.checked)
        this.SetBackColor(this.data.color)
    else
        this.SetBackColor("#FFFFFF")
}

//Repeat the sequence
function repeat(time) 
{
    step = index % 7           // if goto end return 0. column
    for(var i=0; i<3; i++) {   // loop 3 row
        note  = notes[i]       // find note.
        block = items[i][step] // find block.
        
        block.Animate("Swing", null, 500)
        
        if(block.GetChecked())
            synth.PlayStopTone(note, "8n", time)
    }
    
    index++
}



Dave

unread,
Nov 20, 2021, 3:41:35 PM11/20/21
to DroidScript

Note: The Music component is more capable and better supported as it is based on WebAudio, so I would recommend that over the Synth object for most applications.  You can get at the underlying WebAudio objects too and do more sophisticated things if you need to.

Dave

unread,
Nov 20, 2021, 4:08:51 PM11/20/21
to DroidScript
Here is another extract - 


/*
 * Synth Song player demo by CandleLight
*/

//Setup notes in the song.
var HeyHoNotes = [
    "D4","C4","D4","D4","D4","A3",  
    "D4","D4","E4","E4","F4","F4","F4","F4","E4",   
    "A4","G4","A4","G4","A4","G4","A4","G4","F4","E4"
  ]

//Setup note durations.
var HeyHoDurations = [
    "2n","2n","4n","8n","8n","2n", 
    "4n","4n","4n","4n","8n","8n","8n","8n","2n",
    "4n+8n","8n","4n+8n","8n","4n+8n","8n","8n","8n","8n","8n"
  ]
  
//Setup note velocities.
var HeyHoVelocity = [ 
    0.9,0.9,0.9,0.7,0.7,0.9,  
    0.9,0.7,0.9,0.7,0.9,0.7,0.7,0.7,0.9,   
    0.9,0.7,0.9,0.7,0.9,0.7,0.9,0.7,0.7,0.7 
  ]

//Other variables.
var length = 25
var note, duration, velocity
var turn = index = 0

//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "Linear", "VCenter,FillXY" )

//Add a 'Play' button.
btn = app.AddButton( lay, "Play", 0.3 )
btn.SetOnTouch( btn_OnTouch )
//Add layout to app.
app.AddLayout( lay )
}

//Called when user touches our 'Play' button.
function btn_OnTouch()
{
    //Create music component.
    music = app.CreateMusic()
    
    //Create synthesizer object.
    synth = music.CreateSynth("Synth")
    
    //Create a transport in repeat mode.
    music.RepeatTransport( repeat, "4n" )
    music.StartTransport()
}

//Play each note.
function repeat( time ) 
{
    turn     = index++%length // if notes end back first.
    note     = HeyHoNotes[turn]
    duration = HeyHoDurations[turn]
    velocity = HeyHoVelocity[turn]
    synth.PlayStopTone(note, duration, time, velocity)
}





Dave

unread,
Nov 20, 2021, 4:19:18 PM11/20/21
to DroidScript

Steve Garman

unread,
Nov 20, 2021, 4:28:30 PM11/20/21
to DroidScript
Is there a Music plugin among that lot that I've missed?

Alan Hendry

unread,
Nov 21, 2021, 12:47:00 PM11/21/21
to DroidScript
HI,

The full demo gives me error  
... Music.inc not found
then 
app.CreateMusic is not a function 

Piano gives me the second error

https://symdstools.github.io/Docs/docs/Docs.htm has a link for Music that gives a 404 

Documentation within the app (and browser IDE) shows only
Introduction, Reference, Resources, Game Engine, Javascript, Plugins

Regards, ah

Alan Hendry

unread,
Nov 26, 2021, 8:05:53 AM11/26/21
to DroidScript
HI,

I found a combination that lets me play a note while a key is touched
Set Up - CreateSynth("Signal"); SetVCA(0,0,1,0); SetVolume(1,1)
To play -  SetFrequency(440); Start
To Stop - Stop

I just downloaded the Piano to Android 11 and it seems to run OK (problem was on Oreo).

Regards, ah

Dave

unread,
Nov 26, 2021, 2:36:21 PM11/26/21
to DroidScript
Your problem on Oreo might be because your WebView needs updating from Google Play (maybe its too old to support WebAudio)

Alan Hendry

unread,
Nov 26, 2021, 5:23:08 PM11/26/21
to DroidScript
HI,
On this phone it shows Android System WebView (Beta) as installed and last updated 18 Nov 2021 97.0.4692.20
Play Store doesn't give me the option to uninstall.
I''ll try leaving Beta.
According to the web Chrome includes webview and some/many phones use that.
Regards, ah

Steve Garman

unread,
Nov 26, 2021, 6:44:23 PM11/26/21
to DroidScript
If you leave the beta it mayl let you uninstall System WebView as long as you have Chrome installed but I would recommend leaving it installed unless you really need the space

It's not doing any harm (though the beta can occasionally) but if you decide to change your browser in the future and remove Chrome , DroidScript and lots of other apps will just stop working without the System WebView

Steve Garman

unread,
Nov 26, 2021, 6:51:59 PM11/26/21
to DroidScript
And to be fair the last time I saw a System WebView beta causing a problem in DroidScript was 2015

Alan Hendry

unread,
Nov 27, 2021, 10:06:57 AM11/27/21
to DroidScript
HI,
I tried to leave WebView beta at least 12 hours ago, it still says
   Removing you from the Beta ... It can take a few minutes to remove you from the beta programme
and
   Removing your account from the beta test program.
   You may wish to uninstall the beta test version and install the public version.
Still I don't see a button to uninstall.
The date on the beta is less than a fortnight ago, so it could be buggy.
Regards, ah

Alan Hendry

unread,
Dec 5, 2021, 6:34:28 AM12/5/21
to DroidScript
HI,
The text's on the white notes of the Piano (A, B, etc) are very faint.
Text colour defaults have probably changed since it was written.
cfg.Holo or Dark or Light may be enough (or set text color).
Similarly on the other keyboards in the Music Demo.
May also apply to any old examples, apps, samples, etc.
Regards, ah

Alan Hendry

unread,
Dec 7, 2021, 9:07:02 AM12/7/21
to DroidScript
HI,
Eventually it must've taken me off Beta for Android System WebView
Now on 96.0.4664.45 updated on 15 Nov 2021 released on 10 Mar 2015
running music demo still gives me 
Error: /data/user/0/com.smartphoneremote.androidscriptfree/app_Plugins/music/Music.js
not found!
(Note: Assets are case sensitive)
But it seems to run OK.
DS 2.09 Oreo (8.1.0)
Regards, ah

Steve Garman

unread,
Dec 7, 2021, 2:40:06 PM12/7/21
to DroidScript
I asked a question about the Music plugin and Dave replied to me to say that the Music plugin is no longer required because it has been added as an internal plugin

I have only just realized he replied by email

I suggest you comment out the line 
  //app.LoadPlugin( "Music" )

Alan Hendry

unread,
Dec 8, 2021, 9:22:05 AM12/8/21
to DroidScript
Thanks,
That fixed the Music Demo (it had already been removed from standalone Piano)
Regards, ah
Reply all
Reply to author
Forward
0 new messages