Intro Media Computation
Intro Media Computation CS 1315
Popular in Course
Popular in ComputerScienence
This 0 page Class Notes was uploaded by Alayna Veum on Monday November 2, 2015. The Class Notes belongs to CS 1315 at Georgia Institute of Technology - Main Campus taught by Staff in Fall. Since its upload, it has received 7 views. For similar materials see /class/234117/cs-1315-georgia-institute-of-technology-main-campus in ComputerScienence at Georgia Institute of Technology - Main Campus.
Reviews for Intro Media Computation
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 11/02/15
FUNCTION What goes in programming black area CALLING THE FUNCTION What goes in the command area OUTPUT What JES gives you in the command area def traceMeab traceMe53 O for i in rangea 3 print ib 6 9 12 deftraceabc trace516 O 1 4 7 C c b a for i in rangea aa print a b c d onceMore3 10 6 11 def addThisnum1 num2 12 print num1 num2 13 14 def onceMorescooby shaggy scrappy 15 fred O 16 vema quotsmartquot 10 daphne 10 smart monster 1 if scooby gt 8 print fred if shaggy ltgt 30 fori in rangescrajpy addThisdaphne monster monster monster 1 print daphne print velma def anothernum another7 7 for i in rangenum 77 s num 777 for z in rangei 7777 s s10 num 77777 print s 777777 7777777 def yetAgainnum yetAgain4 4 for i in rangenum 8 s num 16 32 for in rangei s 2 print s def testmepqr testme6991001 1001 if q gt 50 Here we go print r Here we go value 10 Here we go for i in range1p Here we go print quotHere we goquot Here we go value value 1 5 print value 1001 print r def newFunctiona b c newFunction672 6 print a 7 list1 range15 7 value 7 for X in list1 7 print b 2 value value 1 4 print 0 print value def hashbrownsnumber hashbrowns8 36 foo0 for i in rangenumber 1 foo foo i return foo deftracelta b tracelt24 5 a b 6 for X in range1a 7 b b 1 4 7 print b print a b def anotherTracea b c d anotherTrace1239 2 12 4 3 aa1 bcd c 4 d O for p in range13 dp1 print a b c d def whatMorecomp utational media yeah whatMorequotGoquot 5 3 11 Go CS Go media media2 Go CS Go phrase strcomp quot CS quot strcomp Go CS Go if yeah gt 10 Go CS Go fori in range1 utational 9 print phrase print media defjejoueskating curling hockey jejoue5 273 24 for i in rangeskating 21 curling curling hockey 18 print curling 15 print skating hockey 12 53 def hablabaleonardo donatello raphael hablaba2 3 quotlovequot quotquot 3 michelangelo 32 for i in rangedonatello 322 shredder 3 I love pizza for in rangei shredder shredder10 Ieonardo print shredder print michelangelo raphael quotpizzaquot def namingSucksnum namingSucksQ 90 X 99 81 for i in range1num2 72 X X num 63 print X def stringStuffstring stringStuffquotamigoquot a for letter in string ml print letter i thatThere enstring 9 print thatThere o 5 def istStuffist listStuffquotWherequot quotinquot quotthequot quotworldquot Where for item in list in new stritem quottquot the print new world min f 2003919 page 177 e CHAPTER 6 Advanced Sounds Synthesizing Sounds 61 BLENDING SOUNDS CREATING AN ECHO HOW SAMPLING KEYBOARDS WORK ADDFHVESYNTHESS 65 MODERN MUSIC SYNTHESIS 57 5quot boom Creating sounds digitally that didn t exist previously is lots of fun Rather than simply moving around samples or multiplying them we actually change their valuesiadd waves together The result are sounds that never existed until you made themi ln physics adding sounds involves issues of cancelling waves out and enforcing other factors In math its about matricesi In computer science its the easiest process in the worldI Lets say that you7ve got a sound source that you want to add in to the target Simply add the values at the same index numbers Thatls itI for sourceIndex in range1getLengthsource1 targetValuegetSamp1eVa1ueAt target sourceIndex sourceValuegetSamp1eVa1ueAt source sourceIndex setSampleValueAt source sourceIndex sourceValuetargetVa1ue To make some of our manipulations easier we re going to be using setMediaPath and getMediaPathi JES knows how set a media folder and then reference media les within that folder This makes it much easier to reference media lesiyou donlt have to spell out the whole path The functions well use are setMediaPath and getMediaPathi setMediaPath will put up a le pickeripick any le in your media folderi getMediaPath takes a base le name as an argument and will stick the path to the media folder in front of the base name and return a whole path to it gtgtgt setMediaPath New media folder Usersguzdia1mediasources gtgtgt print getMediaPathquotbarbarajpgquot Usersguzdialmediasourcesbarbara jpg gtgtgt print getMediaPathquotseclsilencewavquot Usersguzdialmediasourcessec1si1encewav 177 main i3 2003919 page 178 e 178 Chapter6 Advanced Sounds Common Bug It s not a le it s a string JugbecauaagetMediaPathlxmurnssonumhuugthatlooks like a path doesn t mean that a le really exists there You hawaUJknomr xa ghtbaaananulnnifyouhyi seager to use in your code But if you put in a non existent le youdlgetapath1xgta1unrexh entf egetMediaPathxy l warn you gtgtgt print getMediaPathquotblahblahblahquot Note There is no file at Usersguzdialmediasourcesblahblahblah Usersguzdialmediasourcesblahblahblah k 61 BLENDING SOUNDS In this example we take two soundsisomeone saying Aahl 7 and an bassoon in strument sound of C in the fourth octachand blend the two sounds The way we do this is copying part of the Aahl then adding 50 of each and then copying the C This is very much like mixing 50 of each at a mixing board It s also very much like the way that we blended pictures in Recipe 30 page 91 f Recipe 58 Blending two sounds def blendSounds bass makeSoundgetMediaPathquotbassoonc4wavquot aah makeSoundgetMediaPathquotaahwavquot canvas makeSoundgetMediaPathquotseCSSilencewavquot Each of these is over 40K samples for index in range12000O setSampleValueAtcanvasindexgetSampleValueAtaahindex for index in range12000O aahSample getSampleValueAtaahindex2000O bassSamplegetSampleValueAtbassindex newSample O 5aahSample 05 bassSample setSampleValueAtcanvasindex2000OnewSample for index in range2000O4000O setSampleValueAtcanvasindex2000OgetSampleValueAtbassindex playcanvas return canvas End of Recipe 58 main 39 2003919 page 179 e Section 62 Creating an Echo 179 62 CREATING AN ECHO Creating an echo effect is similar to the splicing recipe Recipe 56 page 169 that we saw in the last chapter but involves actually creating sounds that didn t exist be meVWadothatbyacUn bIaddmuywave nins VVhatvehelth ngluneisaddnug sann esironladelay nun x ofsann esinvayinto1 kasoundiloutrnuhjphed by 06 so that they re fainter f Recipe 59 Make a sound and a single echo of it def echodelay f pickAFile s1 makeSoundf s2 makeSoundf for p in rangedelay1 getLengths1 set delay to original value delayed value 6 setSampleValueAts1 p getSampleValueAt s1p 6getSampleValueAt s2 pdelay plays1 V End of Recipe 59 621 Creating Multiple Echoes Tlns rec oe actuaHQIlets youset the nurnber of echoes that you get R i can generate some amazing effects like this 1 r7 Rec1pe 60 Creatlng multlple echoes def echoesdelayechoes f pickAFile s1 makeSoundf s2 makeSoundf endCurrentSound getLengths1 newLength endCurrentSoundechoes delay get ultimate length of sound for i in range endCurrentSoundnewLength1 initialize delay samples to zero setSampleValueAts1iO main 2003919 page 180 e 180 Chapter6 Advanced Sounds echoAmplitude 1 for echoCount in range 1 echoes1 for each echo decrement amplitude to 6 of current volume echoAmplitude echoAmplitude 06 loop through the entire sound for e in range 1endCurrentSound1 increment position by one position edelayechoCount Set this sample s value to the original value plus the amplitude the original sample value setSampleValueAts1position getSampleValueAtsl position echoAmplitude getSampleValueAt s2 positiondelayechoCount plays1 Y End of Recipe 60 63 HOW SAMPLING KEYBOARDS WORK Sannjhng keyboards are keyboards that use recordings of swinds egW nanos harps trumpets to create music by playing those sound recordings in the de sired pitch Modern music and sound keyboards and synthesizers allow musicians to record sounds in their daily lives and turn them into instruments by shifting the frequency of the original sounds How do the synthesizers do it It s not really complicated The interesting part is that it allows you to use any sound you want as allinstrurnent Sampling keyboards use huge amounts of memory to record lots of differ ent instruments at different pitches When you press a key on the keyboard the recording closest in pitch to the note you pressed is selected then the recording is shifted to exactly the pitch you requested 39Ilus rst recipe vvorks by creatuig a sourui that skips every other saniple You read that rightiafter being so careful treating all the samples the same we re nowqy ngtosk haH then hlthemediasourcesdhechnyyoudl ndzlsound named c4wav This is the note C in the fourth octave of a piano played for one second It makes a good sound to experiment with though really any sound will 39work I Recipe 61 Double the frequency of a sound def doublefilename source makeSoundfilename target makeSoundfilename targetIndeX 1 main i3 2003919 page 181 e Section 63 How Sampling Keyboards Work 181 for sourceIndeX in range1 getLengthsource1 2 setSampleValueAt target targetIndeX getSampleValueAt source sourceIndeX targetIndeX targetIndeX 1 Clear out the rest of the target sound it s only half full for secondHalf in range getLength target2 getLength target setSampleValueAttargettargetIndeXO targetIndeX targetIndeX 1 playtarget return target v End of Recipe 61 Here s how I use it gtgtgt file pickAFile gtgtgt print file Usersguzdialmediasourcesc4wav gtgtgt c4 makeSoundfile gtgtgt playc4 gtgtgt c4doubleddoublefile This recipe looks like it s using the array copying sub recipe we saw earlier but notice that the range uses the third parameteriwe re incrementing by two If we increment by two we only ll half the samples in the target so the second loop just lls the rest with zeroes Tryitu Ybudlseethatthesound1ea yltix doubka i equenqw How did that happen It s not really all that complicated Think of it this way The frequency of the basic le is really the number of cycles that pass by in a certain amount of time If you skip every other sample the new sound has just asrnanylt3x es butlungthen1inln fthezunountoft ne Now let s try the other way Let s take every sample twice What happens then To do this we need to use the Python function int int returns the integer por onoftheinput gtgtgt print intO5 0 gtgtgt print int15 1 CHenistheredpethathahmsthe cquencyVWinaumngthearHuhanbdngsub rec xaagann butiy resortofreven ngit Ilkaforlooprnovesthe39targetIndeX z ong nalength oftlkasound TlmasourceIndeX m nonzlx ng numenknmedibut 1You are now trying this out as you read aren t you main 39 2003919 page 182 e 182 Chapter6 Advanced Sounds only by 05 The effect is that we ll take every sample in the source twice The sourceIndeX ud be 1111 225arulso onlnubecause39we e usuugthe int of that value we ll take samples 1 1 2 2 and so on Recipe 62 Half the frequency def halffilename source makeSoundfilename target makeSoundfilename sourceIndeX 1 for targetIndeX in range1 getLength target1 setSampleValueAt target targetIndeX getSampleValueAt source intsourceIndeX sourceIndeX sourceIndeX 05 playtarget return target Y End of Recipe 62 Think about what we re doing here Imagine that the number 05 above were zumua yl75h72orii VVoukithm work39Theforloop39wouklhaw3U3change but essentially the idea is the same in all these cases We are sampling the source data to create the target data Using a sample indea of 05 slows down the sound andlu vestheirequencyIXsann eindexlargerthanltuu3speedsiuthesound2nmi unmeasesthez equency Let s try to generalize this sampling with the below recipe Note that this one wont workijgh Y Recipe 63 Shifting the frequency of a sound BROKEN def shiftfilenamefactor source makeSoundfilename target makeSoundfilename sourceIndeX 1 for targetIndeX in range1 getLength target1 setSampleValueAt target targetIndeX getSampleValueAt source intsourceIndeX sourceIndeX sourceIndeX factor Section 63 How Sampling Keyboards Work 183 playtarget return target v39 End of Recipe 63 Here s how we could use this gtgtgt hellopickAFile gtgtgt print hello Usersguzdialmediasourceshellowav gtgtgt lowerhelloshifthelloO75 That will work really well But what if the factor for sampling is MORE than 10 gtgtgt higherhelloshifthello15 I wasn t able to do what you wanted The error javalangArrayIndeXOutOfBoundsException has occured Please check line 7 of Usersguzdialshiftbrokenpy VVhy VVha s happennu fknes hOW yOU couklseeit Prum out1dm sourceInderuggbe methesetSampleValueAtYbukiseethatthesourceIndeX becomes larger than the source sound Of course that makes sense If each time throughtheloop manurennnm matargetIndeX39byiL butxv reincn nentnugthe sourceIndeX by more than one we ll get past the end of the source sound before we reach the end of the target sound But how do we avoid it fhnes dun we wantto happenIfthe sourceIndeX GW 39gd kugerthan length of the source we want to reset the sourceIndexiprobably back to 1 The key word there is if or even if It turns out that we can can tell Python to make decisions based on a test and do something based on if something is true In our xwethetestissourceIndeX gt getLengthsourceVWecaniestonltgt m equality and even lt and gt An if statement takes a block just as def and for do The block de nes the things to do if the test in the if statement is true In thEltxweourl ockissnnpbrsourceIndeX L The below recipe generalizes this and allows you to specify how much to shift the samples by if Recipe 64 Shifting the frequency of a sound def shiftfilenamefactor source makeSoundfilename target makeSoundfilename sourceIndeX 1 main 2003919 page 183 e main 39 2003919 page 184 e 184 Chapter6 Advanced Sounds for targetIndeX in range1 getLength target1 setSampleValueAt target targetIndeX getSampleValueAt source intsourceIndeX sourceIndeX sourceIndeX factor if sourceIndeX gt getLengthsource sourceIndeX 1 playtarget return target IV End of Recipe 64 We can actually set the factor so that we get whatever frequency we want We call this factor the sampling interval For a desired frequency fo the sampling intervalshor dbe sann ing intevalsize of source soundgigg gg7ag This is how a keyboard synthesizer works It has recordings of pianos voices bells drums whatever By sampling those sounds at different sampling intervals itcand ftthesoundUthekEued equency The last recipe of this section plays a single sound at its original frequency then attvxgttunes thnxatunes nftunes and ve nu ve had UJInod y shift shghtbzto use blockingPlay Try H vd ithe o gun play and youdl hear the sounds collide as they re generated faster than the computer can play thenL f W Rec1pe 65 Playlng a sound 1n a range of frequenc1es def playASequencefile Play the sound five times increasing the frequency for factor in range16 shiftfilefactor def shiftfilenamefactor source makeSoundfilename target makeSoundfilename sourceIndeX 1 for targetIndeX in range1getLengthtarget1 setSampleValueAttargettargetIndeXgetSampleValueAtsourceintsourceIndeX sourceIndeX sourceIndeX factor if sourceIndeX gt getLengthsource sourceIndeX 1 631 Section 64 Additive Synthesis 185 blockingPlaytarget return target V End of Recipe 65 Sampling as an algorithm You should recognize a similarity between the halving recipe Recipe 62 page 182 and the recipe for scaling a picture up larger Recipe page To halve the frequency we take each sample twice by incrementing by 05 and using the int function to get the integer part of that To make the picture larger we take each pixel twice by adding 05 to our index variables and using the int function on those These are using the same algorithmithe same basic process is being used in each The details of pictures vs sounds aren t critical The point is that the same basic process is being used in each We have seen other algorithms that cross media boundaries Obviously our increasing red and increasing volume functions and the decreasing versions are essentially doing the same things The way that we blend pictures or sounds is the same We take the component color channels pixels or samples sounds and add them using percentages to determine the amount from each that we want in the nal product As long as the percentages total 100 we ll get a reasonable output that re ects the input sounds or pictures at the correct percentages Identifying algorithms like these are useful for several reasons If we under stand the algorithm in general eg when it s slow and when it s fast what it works for and what it doesn t what the limitations are then the lessons learned apply in the speci c picture or sound instances The algorithms are also useful to know for designers When you are designing a new program you can keep in mind the algorithms so that you can use them when they apply When we double or half the sound frequency we are also shrinking and dou bling the length of the sound respectively You might want a target sound whose length is emactly the length of the sound rather than have to clear out extra stuff from a longer sound You can do that with makeEmptySound makeEmptySound10 returns a new empty sound of 10 seconds in length 64 ADDITIVE SYNTHESIS 641 Additive synthesis creates sounds by adding sine waves together We saw earlier that it s really pretty easy to add sounds together With additive synthesis you can shape the waves yourselves set their frequencies and create instruments that have never existed Making sine waves Let s gure out how to produce a set of samples to generate a sound at a given frequency and amplitude main 2003919 page 185 e 186 Chapter 6 Advanced Sounds From trignometry we know that if we take the sine of the radians from 0 to 27 we ll get a circle Spread that over time and you get a sine wave In other words if you took values from 0 to 27 computed the sine of each value and graphed the computed values You d get a sine wave From your really early math courses you know that there s an in nity of numbers between 0 and 1 Computers don t handle in nity very well so we ll actually only take some values between 0 to 277 To create the below graph 1 lled 20 rows a totally arbitrary number of a spreadsheet with values from 0 and 2H about 628 I added about 0314 62820 to each preceeding row In the next column I took the sine of each value in the rst column then graphed it 15 1 05 O 3905 1 15 Now if we want to create a sound at a given frequency say 440 Hz This means that we have to t an entire cycle like the above into 1440 of a second 440 cycles per second means that each cycle ts into 1440 second or 000227 seconds I made the above picture using 20 values Call it 20 samples How many samples to I have to chop up the 440 HZ cycle into That s the same question as How many samples must go by in 000227 seconds We know the sampling rate that s the number of samples in one second Let s say that it s 22050 samples per second our default sampling rate Each sample is then 1 22050 00000453 seconds How many samples t into 000227 That s 00022700000453 or about 50 What we just did here mathematically is interval 1 frequency samplesPerCyle interval samplingRatg samplingRateinterval Now let s spell this out as Python To get a waveform at a given frequency say 440 Hz we need 440 of these waves in a single second Each one must t into the interval of 1 frequency The number of samples that needs to be produced during the interval is the sampling rate divided by the frequency or interval 1 f gtllt samplingrate Call that the samplesPerCycle At each entry of the sound samplelndex we want to 0 Get the fraction of samplelndexsa7nplesPerCycle 0 Multiply that fraction by 211 That s the number of radians we need Take the sin of samplelndeasa7nplesPerCycle gtllt 2H 0 Multiply the result by the desired amplitude and put that in the sampleIndeX To build sounds there are some silent sounds in the media sources Our sine wave generator will use one second of silence to build a sine wave of one second We ll provide an amplitude as input that will be the mamimum amplitude of the i main 2003919 page 186 main i3 2003919 page187 e Section 64 Additive Synthesis 187 sound G ncesnnagenenueslxmveen l and 1therange 2nnphtudesN lbe between a7nphtude anda7nphtude Connnon BugSetthelnedki der ry IfyouietO39uaacodethattwesgetMediaPathyoudlneed to execute setMediaPath rst f 41 Rec1pe 66 Generate a sme wave at a glven frequency and amplltude def sineWavefreqamplitude Get a blank sound mySound getMediaPath seclsilencewav buildSin makeSoundmySound Set sound constant sr getSamplingRatebuildSin sampling rate interval 10freq Make sure it s floating point samplesPerCycle interval sr samples per cycle maXCycle 2 pi for pos in range 1getLengthbuildSin1 rawSample sin pos samplesPerCycle maXCycle sampleVal int amplituderawSample setSampleValueAtbuildSinpossampleVal return buildSin V End of Recipe 66 Here we are building a sine wave of 880 HZ at an amplitude of 4000 gtgtgt f880sineWave8804000 gtgtgt playf880 642 Adding sine waves together Now let s add sine waves together Like we said at the beginning of the chapter that s pretty easy Just add the samples at the same indices together Here s a 188 Chapter6 Advanced Sounds functkulthat addSuu3souruiinto21secondampm1nd W Rec1pe 67 Add two sounds together def addSoundssound1sound2 for index in range1getLengthsound11 slSample getSampleValueAtsound1indeX s2Sample getSampleValueAtsound2indeX setSampleValueAtsound2indexslSamples2Sample r End of Recipe 67 How are we going to use this function to add together sine waves We need both of them at once Turns out that it s easy Making it Work Tip You can put more than one function in the same le It per a y okay to have nunrathanone unctku1inthe same le Just type them all in in any order Python will gureit out Nbf kaadditivepylookshkethm def sineWavefreqamplitude Get a blank sound mySound getMediaPath seclsilencewav buildSin makeSoundmySound Set sound constant sr getSamplingRatebuildSin sampling rate interval 10freq samplesPerCycle interval sr samples per cycle make sure floating point maXCycle 2 pi for pos in range 1getLengthbuildSin1 rawSample sin pos samplesPerCycle maXCycle sampleVal int amplituderawSample setSampleValueAtbuildSinpossampleVal return buildSin main 2003919 page 188 e mi is 2003919 page 189 B Secuon 54 Addmve Synthmis 139 def eddSoundsltsound1sound2gt r 1ndex m rangelgetLengthsound11z m A ltsound1rndexgt 1ndex setSampleValueAtsoundZ1ndexslSamplesZSample Let s add together 440 Hz 880 Hz twice 440 and 1320 Hz 880440 but we ll have the amplitudes increase We ll double the amplitude each time 2000 then 4000 then 8000 We ll add them all up into the name 440 At the end I generate a 440 Hz sound so that I can listen to them both and compare gtgtgt 440slneWaVe4402000 gtgtgt 880slneWaVe 8804000 1320slneWave13208000 gtgtgt addSoundsf880f440 gtgtgt addSoundsltf1320f440 gtgtgt play 440 gtgtgt Just440slneWaVe4402000 gtgtgt p1aylt3ust440gt a Common Bug Beware of adding amplitudes past 32767 Cquot When you add sounds you add their amplitudes too A maximum of 200040008000 will never be greater than 32767 but do worry about that Remember what hapn pened when the amp1itude got too high 1ast chapter 643 Checking our result How do we know if we really got what we Wanted We can test our code by using the sound too1s in the MediaTools First we save out a samp1e Wave just 400 Hz and the combined Wave gtgtgt wrlteSoundToltjust440 Usersgizd1almedlasourcesjust440an gtgtgt wrlteSoundToltf440 Usersgzd1almedlasourcesc0mb1ned440wav n up each of these in turn in the sound editor Right away you ll notice that the Wave forms look very di erent Figure 62 That tells you that w di samethmg to the sound but what The way you can really check your additive synthesis is with an Fm Generate the FFT for each signal You ll see that the 440 z 6 Now look at the combined Wave form s FFVT Figure 64 Wow It s what it s supposed to be You see three spikes there and each succeeding one is double the height of the 1ast one main i3 2003919 page190 e 190 Chapter6 Advanced Sounds 644 Square waves We don t have to just add sine waves We can also add square waves These are l mm ysqumeshapaiwawmqno ngbmmmml1and dThePTWTw lbokvmy h brenm andthe sound vd be very di erent Hcanactua y bezirnuch cher sound Try swapping this recipe in for the sine wave generator and see what you think Note the use of an if statement to swap between the positive and negative sides of the wave half way through a cycle f W Rec1pe 68 Square wave generator for glven frequency and amplltude def squareWavefreqamplitude Get a blank sound mySound getMediaPathquotsec1silencewavquot square makeSoundmySound Set music constants samplingRate getSamplingRatesquare sampling rate seconds 1 play for 1 second Build tools for this wave seconds per cycle make sure floating point interval 10 seconds freq creates floating point since interval is fl point samplesPerCycle interval samplingRate we need to switch every halfcycle samplesPerHalnycle intsamplesPerCycle 2 sampleVal amplitude s 1 i 1 for s in range 1 getLengthsquare1 if end of a halfcycle if i gt samplesPerHalnycle reverse the amplitude every halfcycle sampleVal sampleVal 1 and reinitialize the halfcycle counter i O setSampleValueAtsquaressampleVal i i 1 returnsquare 61L5 main 2003919 page 191 e Section 64 Additive Synthesis 191 W End of Recipe 68 Use it like this gtgtgt gtgtgt gtgtgt gtgtgt gtgtgt sq440squareWave4404000 playsq440 sq880squareWave8808000 sq1320squareWave132010000 writeSoundTosq440getMediaPathquotsquare440wavquot Note There is no file at Usersguzdialmediasourcessquare440wav gtgtgt addSoundssq880sq440 gtgtgt addSoundssq1320sq440 gtgtgt playsq440 gtgtgt writeSoundTosq440getMediaPathquotsquarecombined440wavquot Note There is no file at Usersguzdialmediasourcessquarecombined440wav You ll nd that the waves in the wave editor of MediaTools really do look square Figure 65 but the most amazing thing is all the additional spikes in FFT Figure 66 Square waves really do result in a much more complex sound Triangle waves Try triangle waves instead of square waves with this recipe Recipe 69 Generate triangle waves def triangleWavfreq Get a blank sound myFolder setMediaPath mySound getMediaPathquotsec1silencewavquot triangle makeSoundmySound Set music constants amplitude 6000 Loudness at 6000 could be any from 1 t032768 samplingRate 22050 seconds 1 sampling rate play for 1 second Build tools for this wave seconds per cycle make sure floating point interval 10 seconds freq creates floating point since interval is fl point samplesPerCycle interval samplingRate we need to switch every halfcycle main i3 2003919 page 192 e 192 Chapter6 Advanced Sounds samplesPerHalnycle intsamplesPerCycle 2 value to add for each subsequent sample must be integer increment intamplitude samplesPerHalnycle start at bottom and increment or decrement as needed sampleVal amplitude i 1 for s in range 1 samplingRate 1 create 1 second sound if end of a halfcycle if i gt samplesPerHalnycle reverse the increment every halfcycle increment increment 1 and reinit the halfcycle counter i O sampleVal sampleVal increment setSampleValueAttrianglessampleVal i i 1 playtriangle i End of Recipe 69 65 MODERN MUSIC SYNTHESIS Additive synthesis is how early music synthesizers work Nowadays additive syn themsimfttoolunnuulbecausethesoundsthatitgenerahxananaturalsoundun Synthesizing from recorded sounds is quite common but isn t pure synthesis in the sense of creating sounds out of nothing The most common synthesis technique today is probably FM synthesis or frequency modulation synthesis In FM synthesis an oscillator a programmed object that generates a regular series of outputs controls modulates frequencies with other other frequencies The result is a richer sound less tinny or computer sounduu Another common technique is subtractive synthesis In subtractive synthesis outandnn nowe m uamp astheinpuu and39 mnt hms anaapphaitOIBnmve unkumed equenckmrTherem tisagaun aijchersoundthough13q ca y39notas rich as FM synthesis Why would we want to create sounds or music with computers anyway VVhatE the poun vduulthere analotS guru soundsrnusk3 and nnwhjansin the world The point is that if you want to tell someone else how you got that souruiso that they cor drephcate the processu evenniochfy the sourulinsorne way perhaps making it better a program is the way to do it A program suc cinctly captures and communicates a processihow a sound or piece of music is generated 651 652 Section 65 Modern Music Synthesis 193 MP3 Nowadays the most common kind of audio le that you have on your computer is probably MP3 les or perhaps MP4 or one of its related or descendant le types MP3 les are sound and video in some cases encodings based on the MPEG 3 standard They are audio les but compressed in special ways One way in which MP3 les are compressed is called lossless compression There are techniques for storing data that use fewer bits For example we know that every sample is typically two bytes wide What if we didn t store every sample but instead stored the di erence from the last sample to this sample The difference between samples is usually much smaller than 32767 to 327687it might be 1000 That takes fewer bits to store But MP3 also uses lossy compression It actually throws away some of the sound information For example if there s a really soft sound immediately after or simultaneous with a really loud sound you won t be able to hear the soft sound A digital recording keeps all those frequencies MP3 throws away the ones you can t actually hear WAV les are kind of compressed but not as much as MP3 and they only use lossless techniques MP3 les tend to be much smaller than the same sound in a WAV format MIDI MIDI is the Musical Instrument Digital Interface It s really a set of agreements between manufacturers of computer music devices sequencers synthesizers drum machines keyboards etc for how their devices will work together Using MIDI you can control various synthesizers and drum machines from different keyboards MIDI is not used for encoding sound as much as encoding music MIDI doesn t really record what something sounds like as how it is played Literally MIDI encodes information like Press the key down on synthesized instrument X at pitch Y then later Release the key Y on instrument X MIDI les tend to be very small Instructions like Play key 42 on track 7 are only some ve bytes long This makes MIDI attractive in comparison with large sound les MIDI has been particularly popular for karaoke machines The quality of MIDI sound depends entirely on the synthesizer the device generating the synthesized instrument Most modern operating systems have pretty good synthesizers built into them We can actually use them from Python J ES has built in to it a function playNote that takes as input a MIDI note a duration how long to play the sound in milliseconds and an intensity how hard to strike the key from 07127 playNote will always use a piano sounding instrument MIDI notes correspond to keys not to frequencies C in the rst octave is 1 C is 2 C in the fourth octave is 60 D is 62 and E is 64 Here s a simple example of playing some MIDI notes from JES We can use for loops to specify loops in the music f Recipe 70 Playing MIDI notes example main 2003919 page 193 e 194 Chapter 6 Advanced Sounds def song playNote60200127 playNote62500127 playNote64800127 playNote60600127 for i in range12 playNote64120127 playNote65120127 playNote6760127 r End of Recipe 70 PROBLEMS 61 62 Using the sound tools gure out the characteristic pattern of different instru ments For example pianos tend to have a pattern the opposite of what we createdithe amplitudes decrease as we get to higher sine waves Try creating a variety of patterns and see how they sound and how they look VVhen nnmnjans39work vuth add iwasynthesthey vu o enumap enm opes around the sounds and even around each added sine wave An envelope changes the amplitude over time It might start out small then grow rapidly or slowly then hold at a certain value during the sound and then drop before the sound ends That kind of pattern is sometimes called the attachsustaindecay ASD enuelope Pianos tend to attack quickly then decay quickly Flutes tend to attack slowly and sustain as long as you want Try implementing that for the sine and square wave generators TO DIG DEEPER Good books on computer music will talk a lot about creating sounds from scratch like in this chapter One of my favorites for understandability is Computer Music Synthesis Composition and Performance by Dodge and Jerse The bible of computer music is Curtis Roads massive The Computer Music Tutorial 18 One of the most powerful tools for playing with this level of computer mu sic is CSound It s a software music synthesis system free and totally cross platform The book by Richard Boulanger 4 has everything you need for playing with CSound main 2003919 page 194 e 2003919 page 195 Section 65 Modern Music Synthesis 13995 FIGURE 61 The tOp and middle waves are added together to create the bottom Nave main jg 2003919 page 196 196 Chapter 6 Advanced Sounds FIGURE 62 The raw 440 Hz signal on top7 then the 4408801320 Hz signal on the bottom FIGURE 63 FFT of the 440 Hz sound FIGURE 64 FFT of the combined sound main 13 2003 9 19 page 197 e Section 65 M d Music Synthesis 197 inquot W m mam H L r t 4L 1 7quot L a U L J ii iiiquot FIGURE 65 The 440 HZ square wave top and additive combination of square waves bottom i i i i j i L 1 FIGURE 66 FFT7s of the 440 HZ square wave top and additive combination of square waves bottom min f 2003919 page 198 198 Chapter 6 Advanced Sounds Bibliography H to P 01 T39 00 3 H 53 H H H to H W H P H 01 H F H 00 HAROLD ABELSON GERALD JAY SUSSMAN AND JULIE SUSSMAN Structure and in tepretation of computer programs 7 2nd edition MIT Press Cambridge MA 1996 KEN ABERNETHY AND TOM ALLEN Exploring the digital domain An introduction to computing with multimedia and networking PWS Publishing Boston 1998 BETH ADELSON AND ELLIOT SOLOWAY The role of domain experience in software design IEEE Transactions on Software Engineering SE 11 1985 no 11 13517 1360 Richard Boulanger ed The csound book Perspectives in synthesis sound design signal processing and programming MIT Press Cambridge MA 2000 AMY BRUCKMAN Situated support for learning Storm s weekend with rachael Jour nal of the Learning Sciences 9 2000 no 3 3297372 JOHN T BRUER Schools for thought A science of learning in the classroom MIT Press Cambridge MA 1993 CHARLES DODGE AND THOMAS A JERSE Computer music Synthesis composition and performance SchimerzThomason Learning Inc 1997 MATTHIAs FELLEISEN ROBERT BRUCE FINDLER MATTHEW FLATT AND SHRIRAM KRISHNAMURTHI How to design programs An introduction to programming and com puting MIT Press Cambridge MA 2001 JAMES D FOLEY ANDRIEs VAN DAM AND STEVEN K FEINER Introduction to computer graphics Addison Wesley Reading MA 1993 MARTIN GREENBERGER Computers and the world of the future Transcribed record ings of lectures held at the Sloan School of Business Administration April 1961 MIT Press Cambridge MA 1962 MAR GUZDIAL Squeak Objectioriented design with multimedia applications PrenticeHall EngleWood NJ 2001 Mark Guzdial and Kim Rose eds Squeak open personal computing for multimedia PrenticeHall EngleWood NJ 2001 IDIT HAREL AND SEYMOUR PAPERT Software design as a learning environment In teractive Learning Environments 1 1990 no 1 1732 BRIAN HARVEY Computer science logo style 2e vol 1 Symbolic computing MIT Press Cambridge MA 1997 DAN INGALLS TED KAEHLER JOHN MALONEY SCOTT WALLACE AND ALAN KAY Back to the future The story of squeak a practical smalltalk written in itself OOP SLA 97 Conference Proceedings ACM Atlanta GA 1997 pp 3187326 MARGARET LIVINGSTONE Vision and art The biology of seeing Harry N Abrams Inc New York 2002 M RESNICK Turtles termites and tra ic jams Explorations in massively parallel microworlds MIT Press Cambridge MA 1997 CURTIS ROADS The computer music tutorial MIT Press Cambridge MA 1996 199 u 77 main 2003919 page 199 min f 2003919 page 200 e Index 7 29 26 7 A local or global name could not be found 23 Your code contains at least one syntax error meaning it is not legal jythoni 77 23 abstraction 35 ACM Turing Award 14 acuity 41 addLinepicturex1y1X2y2 125 addRectpictX1y1Wh 125 addRectFilledpictX1y1Whcolor 125 addTextpictxystring 125 algebra 18 algorithm 6 162 185 algorithms 7 alias 155 alpha channel 43 American Standard Code for Information Interchange ASCII 10 amplitude 134 analogtodigital conversion ADC 142 analogue 12 argument 24 35 array 17 144 copying 168 element 144 index 145 notation 165 166 arti cial intelligence 7 ASCII 10 getting the mapping 22 assignment 26 attacksustain decay ASD envelope 194 AutoCAD 126 background subtraction 118 base le name 24 binary 9 binding 26 28 bit 9 bitmap 40 bitmap graphical representations 126 blend 91 178 blending 200 min f 2003919 page 201 Index 201 pictures 91 sounds 178 block 30 54 152 183 body of function 30 Brando Marlon 155 byte 9 17 7 calculus 14 capitalization 23 changeRed 67 changeVolume 161 channel 43 chromakey 119 clipped 144 clipping 144 164 CMYK color model 42 code 16 collage 86 color comparing 48 sepia 101 color replacement 98 command area 20 commands 29 comment 169 common bug An Example Common Bug 2 Beware of adding amplitudes past 32767 189 Donlt try printing the pixels Way too big 47 End with jpg 50 Its not a le its a string 178 JES is slow to start 19 Keep sounds short 154 Making JES run faster 19 Mistyping a name 147 Patience for loops always end 56 Pythonls types can produce odd results 21 Saving a le quicklyiand how to nd it again 50 149 Seeing changes in the picture 48 Set the media folder rst 187 Windows and WAV les 154 Windows lenames and backslashes 34 comparing colors 48 min f 2003919 page 202 202 Index compressed 18 40 45 compressions 134 computational recipe 6 computer 9 computer hardware 141 computer music 7 computer science idea 2 patterns in n bits 144 An Example Idea 2 Comments are good 81 Computer science is the study of recipes 6 Computers can layer encodings 10 Moorels Law 11 Much of programming is about naming 16 Names inside of functions are different than names outside of functions 58 Nyquist Theorem 143 Programs are for people not computers 17 Programs are for people 64 The most important skill is tracing 57 We can substitute names values and functions 29 We Write programs to encapsulate and communicate process 129 constants 8 109 coordinates 40 copying 79 cropping 84 CSound 194 cycle 134 cycles per second 136 data 17 data representation 11 data structures 7 11 databases 7 debugger 20 debuggging using print statements 78 debugging 160 163 using print statements 78 debugging tip An Example Debugging Tip 2 Common typos 22 Donlt forget to LOAD 31 Getting more information on errors 147 decibels 136 decimal number 9 def 30 35 de ning functions 30 min f 2003919 page 203 Index 203 Digital 12 Digital media 12 digitalto analog conversion DAC 142 digitization 12 digitizing media sounds 141 digitizing media pictures 41 Why 12 directory 18 24 dis distance 48 drawing tool 126 Dynabook 14 editor 18 emergent properties 7 empty string 24 encoding 9 24 end conditions 110 envelopes 194 environment 9 equal temperament 136 errors A local or global name could not be found 23 Your code contains at least one syntax error meaning it is not legal jythoni 7 23 expert vs normal 147 evaluation 28 35 expert mode 147 expression 21 27 Fast Fourier Transform 140 FFT 140 le 17 le extension 24 le name 2 lters 192 Flash 126 oat 17 oating point typing 22 oating point number 14 FM synthesis 192 font how de ned 126 for 54 min f 2003919 page 204 204 Index for loop 54 151 body 55 152 in command area 55 154 Fourier transform 139 frequency 134 136 142 frequency domain 139 frequency modulation synthesis 192 function arguments 35 input values 35 input variables 35 parameters 35 When to make one 36 function body 30 functions 22 functions vsi recipes 64 fundamental 138 general 64 getColor 47 getHeight 46 getMediaPath 77 79 177 getPixel 47 getPixels 47 54 getRed 47 getSample 146 getSamples 146 getSampleValueAt 146 getWidth 46 getX 47 getY 47 GIF 126 going digital 12 Google 9 graphics 7 green 11 greyscale 70 Guido 18 hard disk 17 hardware 141 Hertz 136 hierarchical decomposition 66 hierarchy 66 HSV color model 42 HTML 11 humancomputer interface 7 min f 2003919 page 205 Index 205 if 183 190 indentation 30 Industrial Light amp Magic 9 lngalls Dan 176 input 22 input value 35 input variable 35 inputs 30 int 181 integer 17 28 typing 22 integer function 181 Intel 11 intelligence 7 intelligent systems 7 intensity 70 136 interface 7 iterate 151 Java installing 19 JES 9 18 command area 20 installing 19 loading 20 program area 20 running slowly 19 starting 19 JPEG 24 126 Jython 9 18 installing 19 Jython Environment for Students 9 18 Kaehler Ted 176 Kay Alan 176 kilobyte 126 knots 7 liberal education 14 Lisp 8 lossless compression 193 lossy compression 40 193 luminance 41 71 makeColor 47 min f 2003919 page 206 206 Index makeDarker 49 makeEmptyPicture 96 makeEmptySound 185 makeLighter 49 makePicture 46 What it does 46 makePicture 24 makeSound what it does 146 makeSound 25 making it work An Example How To Make It Work 2 Copying and pasting 34 Donlt just trust your programs 60 Donlt start by trying to write applications 65 Explore sounds 141 Get to know your Help 20 Name the names you like 33 Try every recipe 30 Use the JES Help 50 You can put more than one function in the same le 188 Maloney John 176 matrix 40 max 162 maximum nding 162 media computation 11 MediaTools 51 139 application 149 JE S MediaTools 150 picture tools 51 sound tools 139 memory 9 18 19 MIDI 193 mixing sounds 178 Monty Python 18 Moorels Law 11 Moore Gordon 11 MP3 193 MP4 193 MPEG3 193 Musical Instrument Digital Interface 193 negative image 68 nested 72 nested block 55 153 networking 7 noise 139 192 main 19 2003 9 19 page 207 ea Index 207 normalizing 162 Nyquist theorem 142 applications 143 object 146 objects 146 operating system 17 ord 22 ordinal 22 overtones 138 painting tool 126 parameter 35 pass 106 path 24 path delimiter 24 PCM 144 percentage 53 Perlis Alan 13 physics color 11 pickAColor 49 pickAFileO 23 picture makePicture 24 show 25 picture element 12 picture objects 40 picture tool 51 pitch 136 pitch interval 136 pixel 12 40 pixelation 106 pixelization 41 pixels 40 placeholder 67 play07 25 playNote 193 posterizing 103 Postscript 126 process 14 program 6 16 de ned 16 program area 20 programming language 16 programming languages 8 psychoacoustics 136 min f 2003919 page 208 208 Index pulse coded modulation PCM 144 Python 9 18 capitalization 23 de ning functions 30 quote marks 21 range 72 ranges 8 rarefactions 134 realtime 139 recipe 6 A simple blur 106 Add two sounds together 188 An example of using drawing commands 125 An Example Recipe 2 Blending two pictures 91 Blending two sounds 178 Change a soundls volume by a given factor 161 Chromakey shorter 121 Chromakey Replace all blue with the new background 120 Clear the blue component from a picture 61 Color replacement in a range 99 Color replacement Turn Barbara into a redhead 98 Convert a picture to sepiatones 102 Convert to greyscale 70 Convert to greyscale with more careful control of luminance 71 Copy elsewhere into the canvas 83 Copying picture to canvas 80 Copying picture to canvas another way 82 Create the negative of the original picture 69 Creating a collage 87 Creating multiple echoes 179 Cropping a picture onto a canvas 84 Cropping the face into the canvas differently 85 Darken the picture 68 Decrease an input soundls volume by halving the amplitude 160 Double the frequency of a sound 180 Draw lines by setting pixels 122 Draw the grey effect 127 Draw the picture in Figure 4114 128 Draw the picture in Figure 4115 128 Generate a sine wave at a given frequency and amplitude 187 Generate triangle waves 191 Half the frequency 182 lncrease an input soundls volume by doubling the amplitude 155 lncrease an input soundls volume using range 166 min f 2003919 page 209 Index 209 Increase the red component by 20 61 Increase the volume then decrease 166 Lighten the picture 68 Lighten the picture using nested loops 73 Make a sound and a single echo of it 179 Making a sunset 62 Making a sunset as three functions 64 Mirror pixels horizontally bottomtotop 75 Mirror pixels horizontally topto bottom 75 Mirror pixels in a picture along a vertical line 74 Mirror the Temple of Zeus 77 Normalize the sound to a maximum amplitude 163 Pick and play a sound 32 Pick and show a picture 31 Play a speci c sound 33 Play the given sound backwards 172 Play the sound le whose le name is input 35 Playing a sound in a range of frequencies 184 Playing MIDI notes example 193 Posterize by leve s 1 5 Posterizing a picture 104 Reduce red eye 101 Reduce the amount of red in a picture by 50 56 Rotating a picture 93 Scaling a picture down smaller 95 Scaling the picture up larger 95 Set all samples to maximum values 164 Shifting the frequency of a sound 183 Shifting the frequency of a sound BROKEN 182 Show a speci c picture 33 Show the picture le whose le name is input 35 Show the picture provided as input 36 Splice the preamble to have united people 169 Square wave generator for given frequency and amplitude 190 Subtract the background and replace it with a new one 118 recipes vsi functions 64 red 11 repaint 48 return 67 162 reusable 64 RGB model 42 Roads Curtis 176 194 sample 12 142 sample index 182 sample objects 145 sample size 164 min f 2003919 page 210 210 Index sample sizes 143 sampling 95 182 184 sampling rate 148 sampling interval 184 sampling rate 143 in additive synthesis 186 sawtooth 138 scope 58 155 171 sepiatoned 101 sequence 54 165 setColor 47 setMediaPath 177 setRed 47 setSample 146 setSampleValueAt 147 show 46 show 25 sign bit 144 signal vieW 139 sine wave 134 Siren 176 slice 140 software engineering 7 13 sonogram vieW 140 soun amplitude 134 decibels 136 decreasing volume 160 frequency 134 how to manipulate 145 increasing volume 155 intensity 136 makeSound 25 pitch 136 play07 25 splicing 167 volume 136 155 160 sound cursor 149 sound editor 139 sound object 145 sound pressure level 136 source 79 speci cation 126 spectrum vieW 139 spike 139 splicing 167 square brackets 166 min f 2003919 page 211 Index 211 square waves 190 Squeak 176 statement 30 stepping 57 Stop button 20 string 17 28 29 strings 21 strongly typed 17 subrecipe 162 168 array copying 168 sampling 1 subscript 166 subsitution 28 substitution 35 subtractive synthesis 192 Sun 19 symbols 16 syntax 30 synthesizers 180 systems tab 30 takes on 35 target 79 using print statements 78 testing methods 158 163 The Godfather 155 theory 7 time domain 139 trace 57 tracing 160 using print statements 78 transforming 79 transistor 11 transparency 43 91 TrueType 126 two s complement notation 143 type 16 types byte 17 de ned 17 oat 17 integer 17 integer array 17 string 17
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'