Thumbnail

Output Audio From EZ-B

by Synthiam

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

How to add the Output Audio From EZ-B robot skill

  1. Load the most recent release of ARC (Get ARC).
  2. Press the Project tab from the top menu bar in ARC.
  3. Press Add Robot Skill from the button ribbon bar in ARC.
  4. Choose the Audio category tab.
  5. Press the Output Audio From EZ-B icon to add the robot skill to your project.

Don't have a robot yet?

Follow the Getting Started Guide to build a robot and use the Output Audio From EZ-B robot skill.

How to use the Output Audio From EZ-B robot skill

This skill is an example, with source code, of how to play audio out of the EZ-B while making a custom robot skill. 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 skill, be sure to follow the skill tutorial here: https://synthiam.com/Docs/Create-Robot-Skill/Overview

If you want your robot to play audio out of an EZB that supports audio stream, have a look at the SoundBoard skill, or many others in the Audio section of skills.

This skill was created for educational information to accompany the skill tutorial. This skill does not provide any usefulness outside of that. If you wish to play audio out an EZ-B or PC, use a Soundboard skill, they are far more versatile.

Main Window

User-inserted image

1. Load Audio File Button This button loads an audio file, browse to the location on your computer and select an .MP3 or .WAV file.

2. Play Audio Button This button plays the selected audio file through the EZ-B speaker.

3. Audio File Information Field This field displays the selected audio filename, Original size (in bytes) and the Compressed size (in bytes).

How to Use the Output Audio from EZ-B Skill

  1. Add this skill to your ARC project (Project -> Add Skill -> Audio -> Output Audio from EZ-B)

  2. Click the Load Audio button to attach an .MP3 or .WAV file

  3. Click the Play Audio button to hear the Audio play

*Note: As mentioned above, this skill is a simple example for integrating into other skills.

Script Samples

The source code of this skill provides the following examples:

  1. Load audio from MP3 or WAV file

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

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

  1. Convert audio file to uncompressed PCM data to supported EZ-B sample rate and sample size

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

  1. Compress PCM data with gzip to be stored in project STORAGE

                using (MemoryStream ms = new MemoryStream()) {

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

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

  1. Play audio data from compressed project STORAGE

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

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

    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);
    }

  1. Changes the status of the button when audio is playing globally from anywhere in ARC on EZ-B #0

    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...


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

Requirements

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

Resources

Source Code: OutputAudioFromEZ-BSource.zip


ARC Pro

Upgrade to ARC Pro

ARC Pro will give you immediate updates and new features needed to unleash your robot's potential!