Lab: Volume

Write a program to modify the volume of an audio file.

$ ./volume input.wav output.wav 2.0

WAV Files

WAV files are a common file format for representing audio. WAV files store audio as a sequence of “samples”: numbers that represent the value of some audio signal at a particular point in time. WAV files begin with a 44-byte “header” that contains information about the file itself, including the size of the file, the number of samples per second, and the size of each sample. After the header, the WAV file contains a sequence of samples, each a single numerical value representing the audio signal at a particular point in time.

Scaling each sample value by a given factor has the effect of changing the volume of the audio. Multiplying each sample value by 2.0, for example, will have the effect of doubling the volume of the origin audio. Multiplying each sample by 0.5, meanwhile, will have the effect of cutting the volume in half.

Types

So far, we’ve seen a number of different types in C, including int, bool, char, double, float, and long. Inside a header file called stdint.h are the declarations of a number of other types that allow us to very precisely define the size (in bits) and sign (signed or unsigned) of an integer. Two types in particular will be useful to us in this lab.

Getting Started

  1. In the terminal window, run
wget https://scienceacademy.github.io/web/lab4.zip

to download a zip file of the lab code.

  1. In the terminal window, run unzip lab4.zip to unzip (decompress) the file.
  2. In the terminal window, run cd lab4 to change directories into your lab4 directory.

Implementation Details

Complete the implementation of volume.c, so that it changes the volume of the sound by a given factor.

Hints

uint8_t header[n];

replacing n with the number of bytes. You can then use header as an argument to fread() or fwrite() to read into or write from the header.

int16_t buffer;

You can then use &buffer as an argument to fread() or fwrite() to read into or write from the buffer.

How to Test Your Code

Your program should behave per the examples below.

$ ./volume input.wav output.wav 2.0

When you listen to output.wav, it should be twice as loud as input.wav!

$ ./volume input.wav output.wav 0.5

When you listen to output.wav, it should be half as loud as input.wav!

Execute the below to evaluate the correctness of your code using check50. But be sure to compile and test it yourself as well!

check50 scienceacademy/problems/2024ap/volume

How to Submit

submit50 scienceacademy/problems/2024ap/volume