Music

Make Music with A Programming Language easily with Chuck

A piece of code can create beautiful music.

Photo by Yasin Aydın: https://www.pexels.com/photo/unrecognizable-male-dj-in-headset-working-on-desktop-computer-3796788/

Never underestimate the power of a computer code. It is as elegant as soothing scenery and as cruel as Satan from hell ( the viruses ).

Today we will introduce to you a compact and cool programming language called – ChucK. ChucK is a strongly timed programming language, which means it has its own internal clock and it works along it. ChucK has a very strong friendship with time. Everything which happens in ChucK happens with the advance in time. Each line of code can be asked to wait for a particular time period or could be made to get executed at a particular point in time. Let’s write a program in ChucK to make some music. Before that here are some features of ChucK.

  • ChucK is a programming language tailored for making a sound.
  • ChucK is designed around time.
  • ChucK has concurrency i.e. running multiple Shreds together.
  • ChucK is a High-Level Language i.e. has simple text-like code which is easy to understand.
  • ChucK is open source and freely available.

What all do you need to make music with a programming language?

  1. Download ChucK from => https://chuck.stanford.edu/

2. Download the installer according to the operating system you use

3. Install it and you are done!

We are using windows OS for this tutorial so we downloaded the windows version of the installer. ChucK has its own Development Environment so don’t worry, the code will be the same for all OS’s.

Now shall we begin to make music with programming !!

So, now navigate to the directory in your file system where you installed ChucK and open the application titled ‘ miniAudicle.exe ‘.

Chuck Miniaudicle.exe

You’ll see something like this.

Chuck Interface

You can notice three windows appear. These are –

  1. Console – These windows display the error codes and other information.
  2. miniAudicle – You write all your code here. Save and Open files.
  3. Virtual MachineChucK has its own Engine. It works over this Virtual Machine instance. You’ll get to know more about it in later tutorials.

Now You Are A Little Bit Familiar With The ChucK Interface.

Now click on the miniAudicle window. Create a new file. File > New.

We’ll tell you all the functioning of the code later in this tutorial. For now, input this code in your new file.

/**
 
 1 SinOsc sine_wave => dac;
 2 
 3 440 => sine_wave.freq;
 4 0.5::second => now;
 5 
 6 880 => sine_wave.freq;
 7 0.5::second => now;
 8 
 9 660 => sine_wave.freq;
10 1::second => now;
 */

So, an important thing, ChucK works on that Virtual Machine thingy. Therefore, firstly click on the ‘ Start Virtual Machine ‘ button in the Virtual Machine window. You’ll notice a clock starts running in that window.

Chuck Virtual machine interface

Notice the ‘ running time ‘ and the ‘ shred ‘ count. Shreds are nothing but an instance of a ChucK program. The ‘ remove last ‘ button removes the latest added Shred and the ‘ clear VM ‘ button clears all the VM’s from the list.

Now from the miniAudicle window, click on the big green plus button titled ‘ Add Shred ‘. It will add the Shred of your program into the VM and you will hear three variations of a tone for a total of 2 seconds. 0.5 second for a sine wave at 440 Hertz, 0.5 for a sine wave at 880 Hertz, and 1 second for a sine wave at 660 Hertz.

This may not be pleasing to your ear, but hey! you create sound through your program.

Explaining the code for Chuck language

Line 1 is telling ChucK to create a Sine Wave Oscillator named ‘ sine_wave ‘ with the keyword ‘  SinOsc ‘. The symbol ‘ => ‘ is called ‘ ChucKing ‘. So, we are creating an instance of the Sine Wave Oscillator named ‘ sine_wave ‘ and we are ChucKing it to ‘ dac ‘. ‘ dac ‘ is nothing but the Digital to Analog Converter in your computer system. ChucK recognizes the hardware you are using and devices the sound hardware of your computer as a ‘dac ‘, in simpler words – your speakers. And a semicolon ‘ ; ‘ to end a ChucK code statement.

Line 3 is telling ChucK to assign ‘ 440 ‘ to the parameter ‘ sine_wave.freq ‘. In ChucK assigning is not really called ‘ Assigning ‘, it is called ChucKing, so later from now we will call this statement as ChucKing the value ‘ 400 ‘ to ‘ sine_wave.freq ‘. The keyword ‘ freq ‘ is a method, predefined for the Oscillators available in ChucK. It is used to set the Frequency of the Sound Wave which ChucK produces. The period is used between ‘ sine_wave ‘ and ‘ freq ‘ as a Dot Operator. It means, ‘ freq ‘ of object ‘ sine_wave ‘.

Line 4 – As told before, ChucK is a strongly timed language and has its own internal clock and also that he works with an Advance in Time. This line of code is telling ChucK to ChucK ‘ 0.5 ‘ as data type ‘ second ‘ to ‘ now ‘. ‘ now ‘ is a special variable in ChucK. It keeps track of the current time in ChucK. We’ll talk more about ‘ now ‘ in our later tutorials. For now, know only that ‘ now ‘ only stores the current time of ChucK.

Lines 6, 7, 9, and 10 are doing the same functions as Lines 3 and 4. Just some values are different. Rest semantics are the same.

So this is what ChucK really does with that code.

And this was your very first ChucK program. We will post more tutorials about ChucK teaching you how in ChucK you can create beautiful compositions and enjoy music creation while coding.

Have a great time!

aa ley ChucK, mai aa gaya!!!

This post was last modified on January 6, 2023 9:08 pm

Balvinder Singh

Founder And Editor at Tekraze.com. Loves to write about technology, gaming, business, tips and tricks. Working as a Senior Software Engineer in Infosys India. Exploring different blockchains as well.

Leave a Comment

Recent Posts

Unlocking Freedom: Remove KG Lock – Your Trusted Partner for Samsung Device Unlocking

Struggling with a locked Samsung Galaxy or Note smartphone? Remove KG Lock, a U.S.-based company,…

2 days ago

How to Use Photoshop Online Free Editor Photopea

When you open Adobe Photoshop for the first time, it's easy to click around in…

2 days ago

8 Social Media Apps for Influencers need to know

Social Media Influencer apps for mobile With the increasing number of social media platforms and…

4 days ago

Microsoft Dynamics 365 you need to know

Microsoft Dynamics 365 is a suite of Business Applications from Microsoft that covers the traditional…

5 days ago

Safe Downloading in South Korea: Alternatives to Torrents on pa2010.com

While torrents and peer-to-peer (P2P) file sharing have long been popular for data transfer, concerns…

1 week ago

Remote Satellite Systems International: Securing Your Communications in Any Situation

In today's increasingly interconnected world, reliable communication is paramount. But what happens when emergencies strike…

1 week ago