0% found this document useful (0 votes)
48 views6 pages

Sound HW PDF

This document provides instructions for homework assignment 5 on sound manipulation. It includes background on representing music as arrays of audio samples in WAV files. It outlines 6 parts to the assignment: 1) Implementing a toString method and changing playback speed. 2) Changing volume. 3) Playing music backwards. 4) Adding echo. 5) Demonstrating the methods. 6) Submitting the code. It also includes a challenge to implement a fade out method.

Uploaded by

Latoya Woods
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
48 views6 pages

Sound HW PDF

This document provides instructions for homework assignment 5 on sound manipulation. It includes background on representing music as arrays of audio samples in WAV files. It outlines 6 parts to the assignment: 1) Implementing a toString method and changing playback speed. 2) Changing volume. 3) Playing music backwards. 4) Adding echo. 5) Demonstrating the methods. 6) Submitting the code. It also includes a challenge to implement a fade out method.

Uploaded by

Latoya Woods
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

CSCI 1300: Intro to Programming HW05: Sound Manipulation

Page 1 of 6
Sound Manipulation
SUBMISSION: Due on June at 23
rd
at 5pm on Moodle

Background
In class weve talked about how arrays can be collections of numbers, strings, characters, or any
other data type. Now its time to think about what an array can be used to represent. We could
have an array represent a list of names, or grades, or things like stock prices over time. Another
application of arrays in music. Music, at its heart, is a collection of notes. Below you can see a
series of notes as musicians see them when theyre learning music:

When working digitally, music files can stored as a series or array of audio samples. WAV files
consist of a block of information (things like sample rate, number of samples, frequency, etc)
and an array of audio samples, and theyll be what we work with for this homework
assignment. We can manipulate the sample rate or the array of samples to change how the
song is played and what it sounds like. Ready to start?

Part 0: Getting Started
The first things you need to do for this assignment have to do with setup. You need to make
sure the starter files can be compiled properly, that they run, and that music plays. Your first
step is to download the player.cxx file from Moodle, and fill in the comment block at the top
with your information. You will also need the kmun.wav file. (Youll be able to use your own
music later, but first we want to make sure everything works, and we know that kmun.wav
works on all of the lab computers.)

Next, you need to build the files. Because this program uses outside libraries, this means youll
need to change the build commands. There are several ways to do this, Im going to talk about
two methods:

1. Change the build command in Geany to:
g++ -Wall "%f" -lsndfile -lportaudio -o "%e"

CSCI 1300: Intro to Programming HW05: Sound Manipulation
Page 2 of 6
2. Type the command directly into the console. For this method, navigate to your working
directory in the terminal, and type g++ -Wall player.cxx -lsndfile -
lportaudio -o player.

Either method is fine, the important part is the -lsndfile -lportaudio section, which tells our
compiler where to find our external libraries. (This is called linking a file or library.)

Once you have a file that compiles, try running it. You should hear the first 10 seconds of a
swing dancing song. If not, ask for help! Its very important to get this part running before you
continue the assignment.

Common Problems:
Problem: Ubuntu has no sound at all in the Virtual Machine.
Solution: Shut down the VM and change the sound setting to ICH AC97.


Problem: You see strange error messages printing when you run the file:
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.surround71
CSCI 1300: Intro to Programming HW05: Sound Manipulation
Page 3 of 6
ALSA lib setup.c:548:(add_elem) Cannot obtain info for CTL elem
(MIXER,'IEC958 Playback Default',0,0,0): No such file or directory
ALSA lib setup.c:548:(add_elem) Cannot obtain info for CTL elem
(MIXER,'IEC958 Playback Default',0,0,0): No such file or directory
ALSA lib setup.c:548:(add_elem) Cannot obtain info for CTL elem
(MIXER,'IEC958 Playback Default',0,0,0): No such file or directory
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.modem
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.modem
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.phoneline
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM
cards.pcm.phoneline
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)

Solution: These are internal messages for one of the sound libraries. You dont need to worry
about these errors unless something else isnt working.

Do This:
Download the player.cxx and kmun.wav files from Moodle and add your name to the
comment block at the top of player.cxx
Use the new commands to build the file.
Run player and make sure it works.

Part 1: toString and Changing Speed
Now that you have a working starter file, its time to start manipulating sounds! The first thing
were actually going to do is write a toString() method for our Song class so we can get
information about what song is playing. The simple version of this method just returns the
name of the song file.

BONUS VERSION OF toString()
If you want to be fancy, the toString() method should print the name of the song
file, followed by the songs sample rate, and the current size of the playbackData
array. You can find the name of the song file and the playbackData array size stored
as class variables. To retrieve the current sample rate, you need to understand a little bit
about how the Song class stores data about the song it represents. Each song has a
CSCI 1300: Intro to Programming HW05: Sound Manipulation
Page 4 of 6
variable called myInfo, of type SF_INFO. This is a struct created by the sound file
library we use, and it represents the data at the front of our WAV file. Info stored in the
SF_INFO struct includes:
frames (the number of samples)
samplerate
channels
format
sections
seekable

To access a member of the struct, e.g. frames, we use this format: myInfo.frames.
Use this information to retrieve the samplerate information. The reason this is a
bonus version of the method is that we havent covered using StringStream yet, and
youll need that to complete the bonus parts of the function.

Speeding Up the Song
The next step is write a method to speed up or slow down the song. Since the songs sample
rate determines how quickly the song plays, all we have to do for the change_speed method
is adjust the sample rate. Making the sample rate smaller will slow the song down, making it
larger will speed the song up. You can access the sample rate with the code
myInfo.samplerate. Information about changing the sample rate is included above.

Note: due to a bug not related to our libraries, the maximum sample rate is 48,484. Add a check
to your code to make sure your final sample rate does not exceed this number.

Do This:
(Optional) use the site media.io to convert one of your own music files to the WAV format
for use while testing player.cxx.
Complete toString()
Complete change_speed
Test both methods to make sure they work as you expect.

Part 2: Play it louder!
Now we want to change the volume of our song. The magnitude of each value in our
playbackData array determines how loud the song will be, so multiplying each value in that
array will change its volume. Complete the change_volume function so that passing in a
value like 1.5 makes our song louder, and passing in a value like 0.5 makes the song softer.

CSCI 1300: Intro to Programming HW05: Sound Manipulation
Page 5 of 6
Do This:
Complete change_volume.

Part 3: Play it backwards!
Rumors have floated through the music industry for years that playing certain songs backwards
reveals hidden messages from the band. The most famous of these rumors was started by
Beatles fans who were convinced Paul McCartney was dead, and claimed that playing the last
few seconds of the Beatles song Im So Tired backwards confirmed the rumor because it
sounded like Paul is dead man, miss him miss him miss him.

Were going to make a method that plays any song backwards. This task is fairly simple on the
surface: simply reverse the contents of playbackData in place, so that the last element of
the array becomes the first, and vice versa. The trick is figuring out how to do the switch. Try
working it out on paper if you get stuck!

Do This:
Complete reverse.

Part 4: Echo echo echo
Now were going to try adding something a bit like an echo. Our echo function takes an
unsigned long long int called echoFactor, which tells us how much offset our echo will have.
Echo should start at the echoFactor spot in playbackData and change that element so
that it equals:
playbackData

2
+
playbackData
echoFactor
2


Do This:
Complete the echo function.

Part 5: Demonstration
Nows the time to show off some of your methods. Add calls to each of the functions youve
written to the main function, and play the new version of the song.

Do This:
Add calls to each of the function youve written if they arent already in use.

CSCI 1300: Intro to Programming HW05: Sound Manipulation
Page 6 of 6

Submission
Submit your version of player.cxx to Moodle.

Challenge
Write a method called fade_out that starts the song at normal volume and gradually fades
the song to silence (this is more difficult than it sounds!). Add an option to your menu so that
the user can select F to fade the song.

You might also like