Plugins / ezrobot / Output Audio from EZ-B

Output Audio from EZ-B

Example with source code of how to play audio out of the EZ-B when making a plugin in C#

Register   Download Plugin

This plugin is version 1, was last modified on 6/7/2017, and is 13,833 Bytes. The author is ezrobot.

Requirements

This plugin requires EZ-Builder for Microsoft Windows PC.

  Get EZ-Builder for Windows

Plugin Details

This plugin was created for educational information to accompany the plugin tutorial. This plugin does not provide any usefulness outside of that. If you wish to play audio out of the ezb, use the soundboard, not this plugin.

This plugin is an example with source code about how to play audio out of the EZ-B. The EZ-B will output audio as a stream or byte array. View the source code of this example to see how it was done. If you are making a plugin, be sure to follow the plugin tutorial here: http://www.ez-robot.com/Tutorials/UserTutorials/146/1

Source Code:
OutputAudioFromEZ-BSource.zip

*Dependency: when creating a plugin and adding resources per the tutorial, additional to adding EZ-Builder.exe and EZ-B.DLL, this plugin requires NAudio.DLL library to be added as a project reference. Remember to UNSELECT copy files.


The source code of this plugin provides the following examples:

1) Load audio from MP3 or WAV file

Code:


// MP3
NAudio.Wave.Mp3FileReader mp3 = new NAudio.Wave.Mp3FileReader(openFileDialog1.FileName);

// WAV
NAudio.Wave.WaveStream wav = new NAudio.Wave.WaveFileReader(openFileDialog1.FileName);



2) Convert audio file to uncompressed PCM data to supported EZ-B sample rate and sample size

Code:


NAudio.Wave.WaveFormatConversionStream pcm = new NAudio.Wave.WaveFormatConversionStream(new NAudio.Wave.WaveFormat(EZ_B.EZBv4Sound.AUDIO_SAMPLE_BITRATE, 8, 1), mp3);



3) Compress PCM data with gzip to be stored in project STORAGE

Code:


using (MemoryStream ms = new MemoryStream()) {

using (GZipStream gz = new GZipStream(ms, CompressionMode.Compress))
pcm.CopyTo(gz);

_cf.STORAGE[ConfigTitles.COMPRESSED_AUDIO_DATA] = ms.ToArray();
}



4) Play audio data from compressed project STORAGE

Code:


using (MemoryStream ms = new MemoryStream(compressedAudioData))
using (GZipStream gz = new GZipStream(ms, CompressionMode.Decompress))
EZBManager.EZBs[0].SoundV4.PlayData(gz);



5) Supports ControlCommand() for Play and Stop of audio to be used in external scripts

Code:


public override object[] GetSupportedControlCommands() {

List items = new List();

items.Add(ControlCommands.StartPlayingAudio);
items.Add(ControlCommands.StopPlayingAudio);

return items.ToArray();
}

public override void SendCommand(string windowCommand, params string[] values) {

if (windowCommand.Equals(ControlCommands.StartPlayingAudio, StringComparison.InvariantCultureIgnoreCase))
playStoredAudio();
else if (windowCommand.Equals(ControlCommands.StopPlayingAudio, StringComparison.InvariantCultureIgnoreCase))
stopPlaying();
else
base.SendCommand(windowCommand, values);
}



6) Changes the status of the button when audio is playing globally from anywhere in EZ-Builder on EZ-B #0

Code:


public FormMain() {

InitializeComponent();

EZBManager.EZBs[0].SoundV4.OnStartPlaying += SoundV4_OnStartPlaying;
EZBManager.EZBs[0].SoundV4.OnStopPlaying += SoundV4_OnStopPlaying;
}

private void FormMain_FormClosing(object sender, FormClosingEventArgs e) {

EZBManager.EZBs[0].SoundV4.OnStartPlaying -= SoundV4_OnStartPlaying;
EZBManager.EZBs[0].SoundV4.OnStopPlaying -= SoundV4_OnStopPlaying;
}

private void SoundV4_OnStopPlaying() {

Invokers.SetText(btnPlayAudio, "Play");
}

private void SoundV4_OnStartPlaying() {

Invokers.SetText(btnPlayAudio, "Stop");
}




Output Text to Speech
You can output text to speech easily as well, using the following code example...

Code:


using (MemoryStream s = EZBManager.EZBs[0].SpeechSynth.SayToStream("I am speaking out of the EZ-B))
EZBManager.EZBs[0].SoundV4.PlayData(s);