A Scripting Language for Web, Linux and Windows

A Scripting Language for Web, Linux and Windows

Example: Play sound

Demonstrates how to play sound on Windows with sound module.

<?php <?v1

dl ("sound"); // Load sound module, only on Windows

// Play WAV file
print ("Play WAV file.");
play_sound ("sound.wav");

// Play System sound
print ("Play Windows system start sound asynchronous.");
play_sound ("SystemStart", SND_ALIAS|SND_ASYNC);

// Play MIDI
// Note: Win7+ has no default MIDI device! Please install MIDI device manually.
// For example: https://coolsoft.altervista.org/en/virtualmidisynth
deviceId = 0;
voice = 1;
volume = 127;

freqList = array (
    "c1" => 264,
    "d1" => 297,
    "e1" => 330,
    "f1" => 352,
    "g1" => 396,
    "a1" => 440,
    "h1" => 495,
    "c2" => 528
);
print ("Play MIDI notes.");

hMidi = midi_create (deviceId);
if (!hMidi) {
  print ("No MIDI device found.");
  // Check Windows version >= Win7
  if (version(2)>=6) {
    print ("Note: Your Windows version has no default MIDI device! Please install MIDI device manually.\r\nFor example: https://coolsoft.altervista.org/en/virtualmidisynth");
  }
}
else {
  foreach (freqList as note => freq) {
    print ("Play Note: ", note, ", Voice: ", voice);
    midi_voice (hMidi, voice);
    midi_play (hMidi, midi_freq2note (freq), volume); 
    sleep (500); // Play for 500 ms
    if (note!="c2")
       midi_play (hMidi, freq, volume, true); // Stop/Mute the note, otherwise it will play forever
    voice++;
  }
  sleep (3000); // Lets playout the last note
  midi_close (hMidi);
}

print ("Stop asynchronous playing of play_sound() function.");
sleep (1000);
play_sound (null, 0);

?>

back to Home