Volume - Walkthrough - CS50 Labs 2020
Key Takeaways
Walks through volume using CS50 Labs 2020
Full Transcript
in this lab your task is going to be to write a program in c that changes the volume of an audio file specifically in this lab we're going to be working with wave files a standard file format for representing audio information what is the structure of a wave file well like any file these files consist of bytes and in particular in a wav file the first 44 bytes of that file are the header for that wav file that contain information about that file that your computer or programs on your computer might need to know in order to read and understand and interpret that file after the 44 byte header are two byte samples of audio many many of these two byte samples of audio repeated back to back to back where each of these two byte samples represents the value of some audio waveform at a particular point in time so ultimately your wav file is going to consist of one 44 byte header followed by many of those two byte samples and the interesting thing about those two byte samples is that each one is really just a number and if you want to change the volume of a sample you're going to multiply that sample by a number if i have an audio sample and i want to double the volume for example i would take each of the samples of audio in the audio file that i'm trying to modify and just multiply each of those sample values by two likewise if i'm trying to cut the volume in half for a particular audio file then i'll take each of the two byte samples and i'll multiply each by 0.5 or one-half in order to cut the volume in half as well ultimately the way your program is going to work is we're going to run our program as dot slash volume followed by three command line arguments the first command line argument is an input wave file some file that already exists representing the audio file that we're here going to try to modify the next command line argument is the name of some output file some new audio file that our program is going to generate that is going to have the modified volume and finally the last command line argument to this program is going to be a floating point number representing the factor by which to change the volume of the original audio file in this case for example we're using a factor of 2.0 to mean we want to double the volume of the audio file but if instead we had tried to use 0.5 for example as the factor then we would be cutting the volume of the audio file in half so here's what you're going to need to do in this lab in order to take the input file and then generate a new output file that's the same audio but just with the volume changed you're going to want to start with the header of that wav file since the header those first 44 bytes of the file are the first thing you'll find inside the input wav file so you'll initially want to read the header from the input file and then you'll want to write that exact same header to the output file after the header recall comes each of those two byte samples many two byte samples back-to-back in the file representing the audio waveform of the file itself so you'll want to repeat for each of those two byte samples you'll want to first read the sample from the input file likely into some sort of variable and then you'll want to multiply that sample value by the volume factor if you're trying to double the volume of the sample then you'll double that value if you're trying to triple it you'll multiply it by three if you're trying to cut the volume in half you'll multiply by 0.5 for example and after you've done that multiplication you'll then write that new sample to the output file so that the updated sample with the modified volume ends up being written to the output file for each of the samples of audio in that entire file as you go about working on this lab a couple of techniques will be helpful one is taking advantage of the various different types that c gives you for dealing with data of different sizes uiint 8t for example is a type that stores unsigned integers that are eight bits or one byte large and this is useful anytime you want to represent just a generic byte of data for example if you're trying to read in a header that has 44 bytes of data you might use an array of 44 of these unsigned integers of 8-bit size as uint 8ts inside of an array to store your header meanwhile int 16t is a type that stores signed integers meaning they could be positive or zero or negative of 16 bits or two bytes each and ultimately this is a great choice of type for representing your samples because we know that each sample is two bytes large and int 16t is the perfect size for representing this kind of information next you're also going to want to deal with reading and writing files and for that these two functions will likely prove helpful f read and f right f read will read a certain number of bytes from some file into memory inside of your computer and likewise f write can write data from your computer's memory to a particular file and you'll likely want to look to the documentation for each of these functions f read and f right to get an understanding for what arguments they take in what order and how you might use these functions to read from the input file and then write your updated data to your output file now let's take a look at the distribution code that we give to you as part of this lab you'll notice that one of the first things we give you is a constant integer called header size equal to 44. you can use this variable anytime you need to reference the number of bytes inside of the header of a wav file as this constant integer called header size inside of the main function we've already done some work for you first checking the number of command line arguments to ensure that when the user is running this program they're providing an input file an output file as well as some factor by which to change the volume of the input file we then open the input file checking to make sure that the input file was able to be opened correctly we then open the output file using the w mode w for writing that file in order to make sure that we can open the file that we're going to be writing to correctly and then we compute the factor converting that factor to a floating point number like 2.0 for doubling the volume or 0.5 for cutting the volume in half here then are your to-do's the first thing you'll want to do is to copy the header from the input file to the output file recall that you know that header is always going to be 44 bytes so you'll likely want to use f-read to read 44 bytes from your file and then f right to write 44 new bytes to the output file then you'll want to go through each of the samples likely using some kind of loop to loop through the input file until you get to the end of the file and for each of those two byte samples to read it into memory update the volume and then write that updated sample to the output file as well you'll notice that we provide you with a sample input file input.wave which you can listen to as just a sample of audio and you can run your volume program on this file passing in a factor of 2.0 to double the volume or some other factor to change the volume by a different amount and then you should be able to listen to that new output file and detect that it does in fact have the same audio just with a different volume after you've done all of those steps you should then have a program that can take any wav file and change the volume by a particular factor my name is brian and this was volume
Original Description
***
This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.
***
HOW TO SUBSCRIBE
http://www.youtube.com/subscription_center?add_user=cs50tv
HOW TO TAKE CS50
edX: https://cs50.edx.org/
Harvard Extension School: https://cs50.harvard.edu/extension
Harvard Summer School: https://cs50.harvard.edu/summer
OpenCourseWare: https://cs50.harvard.edu/x
HOW TO JOIN CS50 COMMUNITIES
Discord: https://discord.gg/T8QZqRx
Ed: https://cs50.harvard.edu/x/ed
Facebook Group: https://www.facebook.com/groups/cs50/
Faceboook Page: https://www.facebook.com/cs50/
GitHub: https://github.com/cs50
Gitter: https://gitter.im/cs50/x
Instagram: https://instagram.com/cs50
LinkedIn Group: https://www.linkedin.com/groups/7437240/
LinkedIn Page: https://www.linkedin.com/school/cs50/
Reddit: https://www.reddit.com/r/cs50/
Quora: https://www.quora.com/topic/CS50
Slack: https://cs50.edx.org/slack
Snapchat: https://www.snapchat.com/add/cs50
Twitter: https://twitter.com/cs50
YouTube: http://www.youtube.com/cs50
HOW TO FOLLOW DAVID J. MALAN
Facebook: https://www.facebook.com/dmalan
GitHub: https://github.com/dmalan
Instagram: https://www.instagram.com/davidjmalan/
LinkedIn: https://www.linkedin.com/in/malan/
Quora: https://www.quora.com/profile/David-J-Malan
Twitter: https://twitter.com/davidjmalan
***
CS50 SHOP
https://cs50.harvardshop.com/
***
LICENSE
CC BY-NC-SA 4.0
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
https://creativecommons.org/licenses/by-nc-sa/4.0/
David J. Malan
https://cs.harvard.edu/malan
malan@harvard.edu
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS50 · CS50 · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Hello, World: Hadi Partovi
CS50
Content Distribution and Archival in a Digital Age
CS50
CS50 2014 - Week 1
CS50
CS50 2014 - Week 3
CS50
CS50 2014 - Week 0, continued
CS50
CS50 2014 - Week 4
CS50
Week 3, continued
CS50
Quiz 0 Review
CS50
CS50 2014 - Week 3, continued
CS50
CS50 2014 - Week 7
CS50
CS50 2014 - Week 7, continued
CS50
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
CS50 2014 - Week 9
CS50
How to Build Innovative Technologies by Abby Fichtner
CS50
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
CS50 2014 - Week 10
CS50
This is CS50 with Steve Ballmer?
CS50
Meteor: a better way to build apps by Roger Zurawicki
CS50
Data Analysis in R by Dustin Tran
CS50
Data Visualization and D3 by David Chouinard
CS50
CS50 2014 - Week 6
CS50
Build Tomorrow's Library by Jeffrey Licht
CS50
CS50 2014 - Week 9, continued
CS50
Essential Scale-Out Computing by James Cuff
CS50
iOS App Development with Swift by Dan Armendariz
CS50
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
3D Modeling and Manufacture by Ansel Duff
CS50
CS50 2014 - Week 5, continued
CS50
hello, world
CS50
CS50 2014 - Deep Thoughts - Hash Table
CS50
CS50 2014 - Deep Thoughts - Binary Tree
CS50
CS50 2014 - Deep Thoughts - Scratch
CS50
CS50 2014 - Deep Thoughts - MySQL
CS50
LaunchCode Visits CS50
CS50
CS50 Live, Episode 100
CS50
CS50 Field Trip to Google
CS50
This is CS50 AP
CS50
Week 4: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
Week 11: Monday - CS50 2011 - Harvard University
CS50
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
Week 12: Monday - CS50 2011 - Harvard University
CS50
Week 1: Friday - CS50 2011 - Harvard University
CS50
Week 3: Monday - CS50 2011 - Harvard University
CS50
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
Week 2: Monday - CS50 2011 - Harvard University
CS50
Week 9: Monday - CS50 2011 - Harvard University
CS50
Week 7: Monday - CS50 2011 - Harvard University
CS50
Week 5: Monday - CS50 2011 - Harvard University
CS50
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Monday - CS50 2011 - Harvard University
CS50
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
Week 10: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2010 - Harvard University
CS50
Related Reads
📰
📰
📰
📰
Essential AI Tools for C#/.NET Software Engineering in 2026
Medium · AI
I shipped three AI tools that run entirely in the browser — here's everything that broke
Dev.to · Dodly Forbi
Parse AI Coding JSONL Safely Before You Infer Completion
Dev.to AI
SANA-Video 2.0: Hybrid Linear Attention with Attention Residuals for Efficient Video Generation
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI