Fractal Sound: An Exploration of Compression


In this assignment, students generate sound waveforms from scratch, starting with simple examples (sine waves, sawtooth waves, chords), and ending with an exploration of the strange and haunting world of fractal sounds.

In my course, I frame this assignment as an exploration of the idea of data compression. In lecture, we discuss ideas like run length encoding and Huffman coding, and I point the fact that for any stream of bits, there exists some shortest Java program that outputs those bits. This assignment demonstrates this in a visceral way.

The core idea for this assignment is that students built different implementations of the Generator interface, shown below:

public interface Generator {
  /** Returns a number between -1 and 1 */
  double next();

Implementations of Generator include SineWaveGenerator, SawToothGenerator, AcceleratingSawToothGenerator, and StrangeBitwiseGenerator.

The assignment starts by introducing them to the provided very simple API that lets them visualize and audibilize the output of their generators.

SineWaveGenerator is given to students, and SawToothGenerator and AcceleratingSawToothGenerator are puzzles for them to implement. They also use our API to play three sine waves at once, generating a major chord. After completing these challenges, we guide them directly towards implementation of the StrangeBitwiseGenerator, which makes surprisingly complex, musical sounds using bitwise operations.

We then turn students lose to tinkering with the `StrangeBitwiseGenerator`, which can be done with no specific expertise or even understanding of bitwise operations.


My version of the assignment covers the following goals, in decreasing order of importance:


Specification for this assignment: Link
Starter files (Java): Link


Summary Students create code that generates sound waveforms, including fractal sound
OOP, bitwise operations, Data compression, sound synthesis
Late CS1, CS2
Easy to moderate. This was used as a 2 hour lab.

  • Cool Application: Students get to build sound from the bit level up, allowing them a peek into the world of signal processing (though no prior knowledge is assumed).
  • Natural Use of Inheritance: Students see a nice clean example of subtype polymorphism, both in creating new subtypes (of the waveform Generator class), and using interfaces (e.g. creating multiple Generator objects and feeding them to a MultiGenerator).
  • Creative: Students can tweak the code to generate all sorts of crazy sounds. It was a really fun way to end our semester long course.

  • Doesn't Cover Core CS1/CS2 Ideas: This is not a typical CS1/CS2 assignment, and is more of a fun exploration of ancillary ideas.
  • Needs Lots of Work to Adapt: If you're not using Java, there's a fair amount of work involved in porting this to other languages due to library dependencies.
Library Dependencies
Requires some sort of graphing library (provided: xChart) and sound playing library (provided: Princeton's StdAudio)

This assignment is built for variation. The sky is the limit as far as what they can build and achieve.

Could also be adapted to be an intro assignment for bitwise operations. You can give them more complex waveforms that require the use of bitwise operations to efficiently match.