AlgoSound
Overview:
AlgoSound adds sound capability to Algodoo scenes. It does this by using either a vbSript or a Python program to read and play sound requests sent to text files from an Algodoo scene. Each text file represents a channel of sound, allowing multiple sounds to play concurrently. In operation, the vbSript or a Python program runs at the same time as the scene that uses AlgoSound.
Installation:
Notes:
Only one program ("runVbAlgoSound.bat" or "pyAlgoSound_04.py"), depending on the PC operating system, is needed to use AlgoSound.
After you execute "runVbAlgoSound.bat" or "pyAlgoSound_04.py", a "command" window will open. Keep that window open (minimized is OK) while running an Algodoo sound scene in order to hear sound.
Windows:
Unzip "AlgoSound.zip"
file in the Algodoo working folder (typically
"C:\Users\SomeUser\Documents\Algodoo").
"runVbAlgoSound.bat",
"vbAlgoSound_04.vbs", and "pyAlgoSound_04.py"
should end up in "C:\Users\SomeUser\Documents\Algodoo\AlgoSound"
Delete "pyAlgoSound_04.py".
Unzip "sounds.zip"
file in the Algodoo working folder (typically
"C:\Users\SomeUser\Documents\Algodoo").
All sound
files should end up in "C:\Users\SomeUser\Documents\Algodoo\sounds"
Double-click "runVbAlgoSound.bat". Disregard the warning when running runVbAlgoSound.bat the first time. This warning is expected since the program does read and write text files when running.
Run AlgoSound Test Utility to see how it works.
Other operating systems:
Unzip "AlgoSound.zip"
file in the Algodoo working folder (typically
"C:\Users\SomeUser\Documents\Algodoo").
"runVbAlgoSound.bat",
"vbAlgoSound_04.vbs", and "pyAlgoSound_04.py"
should end up in "C:\Users\SomeUser\Documents\Algodoo\AlgoSound"
Delete "runVbAlgoSound.bat" and "vbAlgoSound_04.vbs".
Unzip "sounds.zip"
file in the Algodoo working folder (typically
"C:\Users\SomeUser\Documents\Algodoo").
All sound
files should end up in "C:\Users\SomeUser\Documents\Algodoo\sounds"
Download and install Python, pygame, numpy, and gtts. See How To Download And Install Pygame On Python 3.6.0 On Windows 10 if you have any problems. I know the video is for Windows OS, but installation for other OS's is similar.
Double-click "pyAlgoSound_04.py".
Run AlgoSound Test Utility to see how it works.
See How to Install AlgoSound if you run into any problems.
Adding sound to your own scene:
Run the following code in the Algodoo console
Code:
scene.my.playSound :=
(sParams, chan)=>{
sXFile := "snd" + chan + ".txt";
system.ReadWholeFile(sXFile)
== ";" ? {
System.WriteToFile(sXFile, sParams)
} : {}
}
Create a box in Algodoo
Add the following code to the box's onClick event:
Code:
scene.my.playSound("speak,Hello world.", 1)
See "Usage" below for playing different sounds.
AlgoSound
Features and Benefits:
vbAlgoSound:
Pros:
Plays .mp3, .wav, & .mid files
Generates stereo tones
Converts text to speech
Generates and plays midi notes with choice of 128 instruments
Uses 8 channels
No added programs are needed
Cons:
Works only in Windows
pyAlgoSound:
Pros:
Plays .mp3, .wav, .OGG, & .mid files
Generates stereo tones
Converts text to speech
Generates and plays midi notes with choice of 128 instruments
Uses 8 channels
written in Python
All code is visible
Works cross platform
Cons:
Requires Python, pygame, numpy, and gtts download and installation
Only supports 1 channel (channel 0) of music (.mp3, .mid, .OGG) The other channels support .wav files.
Developer
SDK:
AlgoSound requires the following function in the Algodoo scene:
Code:
scene.my.playSound :=
(sParams, chan)=>{
sXFile
:= "snd" + chan + ".txt";
system.ReadWholeFile(sXFile) == ";" ?
{
System.WriteToFile(sXFile, sParams)
} : {}
}
Usage: scene.my.playSound("Item1,Item2,Item3,... ",channel)
If Item1 = someSoundFile(.wav,.mp3,.mid) Then
scene.my.playSound("filename,[numberOfPlays],[volume]",channel)
[] indicates optional parameter
numberOfPlays = number of play cycles; -1 = loop, 0 = stop, 1 = 1, 2 = 2..., default = 1
volume = 1 to 100, default = 100
channel = channel number; 0 or 1
Sample1: scene.my.playSound("phunTheme.mp3", 0)
Sample2: scene.my.playSound("phunTheme.mp3,1,90", 0)
If Item1 = "tone" Then
scene.my.playSound("tone,leftFrequency,rightFrequency,duration,[volume]",channel)
[] indicates optional parameter
leftFrequency = frequency out of left speaker
rightFrequency = frequency out of right speaker
duration = duration in seconds
volume = 1 to 100, default = 100
channel = channel number; 0 or 1
Sample: scene.my.playSound("tone,440,444,10,5", 1)
If Item1 = "midi" Then
scene.my.playSound("midi,midiNoteNum,duration,[volume],[instrument]",channel)
[] indicates optional parameter
midiNoteNum = midi note number
duration = duration in seconds
volume = 1 to 127, default = 127
instrument = midi instrument; 1 to 128
Sample: scene.my.playSound("midi,72,1.0,127,1", 1)
If Item1 = "speak" Then
scene.my.playSound("speak,text to be spoken,[voice],[volume]",channel)
[] indicates optional parameter
text to be spoken = some text to be spoken
voice = voice to be used; 0 = male, 1 = female, default = 0
volume = 1 to 100, default = 100
channel = channel number; 0 or 1
Sample: scene.my.playSound("speak,Hello World,0,90", 0)
How it works:
Start "runVbAlgoSound.bat" or "pyAlgoSound_04.py"
Start an Algodoo scene.
AlgoSound creates a "snd" + n + ".txt" file for each channel, where "n" is the channel number. Each file contains a single semicolon.
If Algodoo sees a single semicolon in the file, then it writes the sound parameters to the file.
If AlgoSound sees more than a semicolon in the file, then it gets the sound information, removes it from the file, and then processes the sound information.
AlgoSound creates temporary files in the AlgoSound folder. Deleting those files won't hurt anything.
Notes:
Python, pygame, and numpy download and installation is shown at How To Download And Install Pygame On Python 3.6.0 On Windows 10
If you create a scene that uses sound, then add an "AlgoSound" Tag to the scene page. This way we can search for all the scenes that use AlgoSound.
Change Log:
_01 Initial release
_02 Added text to speech
_03 Speech text now handles commas
_04 8 channels was 2. Program now has its own folder.