Clone wiki

ScriptExtensions / Music Box

Description

This Script extension adapts code that Helena Kosova created a number of years ago on the TestComplete forums for playing Jingle Bells using the Beep method on WIN32API. I've made a number of changes to her original code.

  • Expanded the range of tones to include everything from A0 up through B8 including all the halfsteps in between using the 12 root of 2 rule and A 440 as a base tone
  • Encapsulated the JingleBells song array into a separate variable to be retrieved as a read-only property of the runtime object
  • Made some minor changes to the Notes and Lyrics object constructors to allow using the constructors as methods of the runtime object
  • Altered the PlaySong method to account for the constructor changes
  • Altered the PlaySong method to display (Rest) when a rest is played

Implementation

To use this object, all you need to do is create an array of objects, either notes or lyrics. For example, the following creates a song that is the C major scale starting at middle C with each note lasting 2 seconds:

var CMajorScale = [new MusicBox.Lyrics('C Major Scale'),
                   new MusicBox.Notes('C4', 2000), 
                   new MusicBox.Notes('D4', 2000), 
                   new MusicBox.Notes('E4', 2000), 
                   new MusicBox.Notes('F4', 2000), 
                   new MusicBox.Notes('G4', 2000), 
                   new MusicBox.Notes('A5', 2000), 
                   new MusicBox.Notes('B5', 2000), 
                   new MusicBox.Notes('C5', 2000)]

To play any such song, call it within the MusicBox Play Song method:

function foo() {
var CMajorScale = [new MusicBox.Lyrics('C Major Scale'),
                   new MusicBox.Notes('C4', 2000), 
                   new MusicBox.Notes('D4', 2000), 
                   new MusicBox.Notes('E4', 2000), 
                   new MusicBox.Notes('F4', 2000), 
                   new MusicBox.Notes('G4', 2000), 
                   new MusicBox.Notes('A5', 2000), 
                   new MusicBox.Notes('B5', 2000), 
                   new MusicBox.Notes('C5', 2000)];

    MusicBox.PlaySong(CMajorScale);
}

To play Helen's original JingleBells, just do the following:

function foo() {
    MusicBox.PlaySong(MusicBox.JingleBells);
}

To play a Russian Holiday folk tone, do the following:

function playLittleTree() {
    MusicBox.PlaySong(MusicBox.LittleTree);
}

To play a traditional holiday tune, do the following:

function playSilentNight() {
    MusicBox.PlaySong(MusicBox.SilentNight);
}

Download Link

Click here to download the TCX file

Updated