Hello World
Your first project
A “Hello world” program is the simplest program you can write in any language. It’s a way of confirming that you can write something small, compile and run it. Usually these programs print the words “Hello, world!”, but the equivalent in audio development is a 440Hz sine wave - or concert A.
This article walks you through this process in both C++ and Pure Data.
Table of contents
Which language should I use?
We developed Bela to support the kind of programming people are already doing. You can use many languages to program Bela, including C++, Pure Data, SuperCollider and Csound. The examples in this Get Started Guide are in C++ and Pure Data. If you are new to audio development, we recommend doing these tutorials in both languages, and going with the one that makes you comfortable.
C++ has the advantage of having the most examples that you can look at, modify and use in your own projects. There’s also a lot of support for C++ online, and it’s a very useful language if you want to learn programming, or improve your existing programming skills - see our C++ language article to learn more about C++ and Bela.
Pure Data has the advantage of being a visual, block-based programming language that doesn’t require writing any text code. Additionally, there is a huge Pure Data community online, and lots of Pure Data users in the Bela community. Some people find Pure Data’s visual, block-based approach easier to understand than written lines of code. Pure Data patches have to be assembled on your computer in the Pure Data environment and uploaded to your Bela. For a link to download Pure Data and more information about the Bela-Pure Data workflow, see our Pure Data language article.
We have a free online course on C++ Real-Time Audio Programming with Bela. Check out our YouTube channel to watch it!
Pure Data (Pd) also has lots of users in the Bela community, and a huge user community online. It’s a visual programming language, meaning that programming means not typing text but connecting Pd “objects” together, and some people find this easier to understand than written lines of code. We also have lots of on-board examples in Pd.
Pd programs, or patches, have to be edited on your computer in the Pd environment and uploaded to your Bela. Download Puredata here to install and try it out (choose the Puredata “vanilla” version).
Bela also supports other popular audio languages including SuperCollider and Csound. Ultimately, we encourage you to get started with the language you feel most comfortable with.
C++
From the system menu on the right, open
Examples and choose the folder Fundamentals
. Open the example project called sinetone
. Click the Run button to build and run it, and you will hear a 440Hz sine wave.
In this example, the sine wave is calculated in real time in render.cpp
inside the function called render()
. The example keeps track of the phase of the oscillator as a global variable called gPhase
so we remember where we left off at the end of each call to render()
.
For more details on writing C++ code on Bela, see this page.
PureData
From the system menu on the right, open
Examples and choose the folder PureData
. Open the example project called language-overview
. Click the Run button to build and run it, and you will hear a 440Hz sine wave.
In this example, the sine wave is generated by the Pd object [osc~ 440]
and its amplitude is scaled down by the object [*~ 0.1]
. The object [dac~ 1 2]
represents the two audio output channels of the Bela board.
For more details on writing PureData code on Bela, see this page.
SuperCollider
From the system menu on the right, open
Examples and choose the folder SuperCollider
. Open the example project called 0-hello-sc
. Click the Run button to build and run it. Instead of playing a sine wave, this example plays a recorded sound file and modulates its pitch.
In this example, the Buffer.read()
command loads the sound file from the flash memory storage. The block of code containing BufRd.ar()
plays the sound file, and the SinOsc.ar(0.08)
command generates a low-frequency sine wave which changes the location within the buffer that plays.
For more details on writing SuperCollider code on Bela, see this page.
Csound
From the system menu on the right, open
Examples and choose the folder Csound
. Open the example project called 0-hello-world
. Click the Run button to build and run it, and you will hear a 440Hz sine wave.
In this example, the line instr 123
defines an instrument as part of the Csound orchestra. Inside of this instrument, the opcode oscils
generates a sine wave, and the opcode out
sends it to the audio output. In the lower part of the file, you can find the Csound score. The line i 123 0 100
causes instrument 123 to play for 100 seconds before stopping.
For more details on writing Csound code on Bela, see this page.
Creating your own project
Moving beyond the examples, you can create your own project in any of these four languages. The steps are the same in each case:
- Open the system menu on the right.
- Choose the top tab, the Project Explorer .
- At the top of this tab, click "Create a new project".
- In the popup, select the language you want to use.
- Give your project a name, and click Continue.