<img src="https://certify.alexametrics.com/atrk.gif?account=bIEZv1FYxz20cv" style="display:none" height="1" width="1" alt="">
GUIDES

Make a DJ Bot

SECTIONS

Make a DJ Bot

Project Complexity: ★★★☆☆

Want to liven up your virtual Space with some MP3s played by a robot? Within 30 minutes, you can make that happen. The DJ Bot you'll have created by the end of this guide will have the human users of your application dancing all night long.

In this guide, you will learn how to:

  1. Sign up to use the High Fidelity Spatial Audio API
  2. Create a High Fidelity Spatial Audio App and a Space to use with our DJ Bot
  3. Set up your development environment for writing the DJ Bot
  4. Write some DJ Bot code!
1. Sign Up to use the High Fidelity Spatial Audio API

If you already have a High Fidelity Developer Account, you can jump straight to the next step.

Before your app can make use of the Spatial Audio API, you'll need a High Fidelity Developer Account. Sign up for an account or log in here.

2. Create an App and Space for the DJ Bot

If you already have an App and Space for your DJ Bot, you can jump straight to the next step.

To authenticate our DJ Bot with your Space, you'll first need to create an App and a Space. To do that:

  1. Log in to your High Fidelity Developer Account.
  2. Under "New App Name", enter an app name.
    • Example: "My Test App"
  3. Click the "Create New App" button.
  4. On the page that appears, under "New Space Name", enter a space name.
    • Example: "My Test Space"
  5. Click "Create New Space".

Do not share the data you see on this page. It allows users to make authorized connections to your High Fidelity Spatial Audio Server.

3. Set up your Local Development Environment

To complete this guide, you will need to install the following software on your computer:

Next, let's create a skeleton version of a NodeJS project we'll use for development:

Create a new folder on your local disk that you'll use for development, such as C:\code\hifi-dj-bot\. Then, install the JavaScript (NodeJS) version of the High Fidelity Spatial Audio Client Library by performing the following actions:

  1. Create a new folder on your local disk that you'll use for development, such as C:\code\hifi-dj-bot\.
  2. Using PowerShell or Terminal, cd into that development directory.
  3. Enter npm init to initialize a new NodeJS project, inputting data as prompted. The data you input isn't important for the purposes of this guide.
  4. Install the JavaScript (NodeJS) version of the High Fidelity Spatial Audio Client Library by typing npm install hifi-spatial-audio
      This will also automatically install the dependencies of the HiFi Client Library onto your system.

To make our lives easier later, we're now going to install all of the NodeJS dependencies used for this DJ Bot project. We'll make use of these dependencies in the next steps.

  1. Install audio-decode by typing npm i audio-decode and pressing enter.
    • When you install audio-decode, you may see some warnings or errors in your console window. These warnings and errors are benign, and you can safely continue.
    • This dependency lets the DJ Bot decode MP3 and WAV files.
  2. Install audio-format by typing npm i audio-format and pressing enter.
    • This dependency helps us create the necessary MediaStream object that we'll send up to the High Fidelity Spatial Audio Server.
  3. Install pcm-convert by typing npm i pcm-convert and pressing enter.
    • This dependency helps us create the necessary MediaStream object that we'll send up to the High Fidelity Spatial Audio Server.
  4. Install yargs by typing npm i yargs and pressing enter.
    • This dependency lets us more easily customize our DJ Bot on the command line when we run it.
  5. Install jose by typing npm i jose and pressing enter.
    • This dependency lets us securely generate a JWT used to connect to our Space.
  6. Install wrtc by typing npm i wrtc and pressing enter.
    • This dependency lets us create the necessary MediaStream object that we'll send up to the High Fidelity Spatial Audio Server.
    • If you see errors scroll by in your console after typing npm i wrtc and also see SKIPPING OPTIONAL DEPENDENCY, do not fret! The wrtc package has an optional dependency that may not build on your system. These errors are benign, and you may ignore them.

Inside your development directory, create a new file called index.js. This is where we'll write our code.

4. Write Some Code!

Now that you have your development environment set up, it's time to write some code!

Here's the entire contents of a nearly-completed index.js DJ Bot. Copy this code into your index.js file.

We say "nearly-completed" above because you have to fill in your APP_ID, SPACE_ID, APP_SECRET, and USER_ID in the code. You gained access to this information in the "Create an App and Space" step above. For reference, you can find these pieces of information in the High Fidelity Audio API Developer Console.

After filling in the above information, in the PowerShell or Terminal window you've been using, type node index.js --audio <path to local audio file> and press enter to execute the code. You can get a list of all command-line options by typing node index.js -h and pressing enter.

If the DJ Bot is running successfully, you won't hear anything, but you will see DJ Bot connected. Let's DANCE! in your PowerShell or Terminal window. If you specified the SPACE_ID that corresponds to the JWT you created for your Simple Web app, you will hear audio after you start your DJ Bot then open your Simple Web app and connect to that space.

Conclusion

In this guide, you learned how to create a groovy DJ Bot for use in your High Fidelity Spatial Audio Space. Your DJ Bot takes an MP3 file from your local disk and broadcasts the audio from that file into your Space! 🎶

You learned that you can use the "Simple Web App" guide to build a Web app from which you can listen to your DJ Bot. Click here to go to that guide.

For detailed technical information about the High Fidelity Spatial Audio Client API, visit the API documentation.

If you have any questions or comments, please reach out to support@highfidelity.com.