Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Spring 2004 Homework 3 Questions

Questions? Comments? Issues?


Help! JES keeps telling me that the files I'm using in my collage from the mediasources file are not valid files. I was able to use the mediasources sounds yesterday when I tested the first part of my program, but now I can't even do that. Is this a problem with JES? should I re-install it in my computer? Thanks!

Does two seperate sounds mean two different wav files or could I use Elliot-hello.wav? The Elliot-hello.wav file says two words: hello and world. From the example (Mark Guzdial-below the hw3 directions)it seems like I could use the Elliot-hello wav file. Lauren Patton

I've fixed the directions – yes, it does mean two different WAV files. Mark Guzdial

Examples that Mark did in breakouts today


These might be useful to someone as examples. You'll need this file in your mediasources (right-click on it, then Save As for Windows; Control-click then Save As for Mac): clap3.wav

def clapping():
  target = makeEmptySound(10)
  clap = makeSound(getMediaPath("clap3.wav"))
  targetIndex = 1
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  play(target)
  return target


def clapping():
  target = makeEmptySound(10)
  clap = makeSound(getMediaPath("clap3.wav"))
  targetIndex = 1
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  targetIndex = targetIndex - 30000
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  targetIndex = targetIndex - 30000
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  play(target)
  return target


def clapping():
  target = makeEmptySound(10)
  clap = makeSound(getMediaPath("clap3.wav"))
  targetIndex = 1
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  targetIndex = targetIndex - 30000
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value+getSampleValueAt(target,targetIndex))
    targetIndex = targetIndex + 1
  targetIndex = targetIndex - 30000
  for source in range(1,getLength(clap)):
    value = getSampleValueAt(clap,source)
    setSampleValueAt(target,targetIndex,value+getSampleValueAt(target,targetIndex))
    targetIndex = targetIndex + 1
  play(target)
  return target


My sound comes out very quiet on my speakers, and I can't figure out why. I've got the collage working though. Interestingly, I had to set all ofthe "targetIndex = targetIndex + 1" lines to "+2" for the sounds to play normally (they sounded like chipmunks the other way). Any ideas as to what caused eiter e of these strange happenings? - Ross



I'm having the same volume and chipmunk problem.

For volume – you know how to increase the volume. Increase the amplitude of the wave. The chipmunk problem might be using sounds that have sampling rates other than 22.050Khz, which is what makeEmptySound and the silent .wav files produces. By taking every other sample, you're decreasing the frequency (taking care of the sampling rate differences) so that it works. Mark Guzdial



Remind me what it meant when if you are running a program you go beyond the number of samples in the file??
It means that the sounds that you are using are longer in length of samples than the length of the silence you are copying them into.

I manipulated one sound twice and added another in, and when I run the three sections separately, it works. How do I get the three def's to run one after another? Katherine Wrobleski
If you write separate programs to play your sounds, you have to choose one in which the others will be called (ie. if you play "Hello World in defplayHelloWorld():, it needs to be used somewhere within HW3(), like you did with the picture alterations in the HW2().) However, it would be better to play all of your sounds in one function, as with the examples above.

It's fine to have three separate functions to handle each of your sounds, as the above states, but it does have to be called from def hw3():. You may have to add some inputs that you're not having to use when having three separate sounds. For example, you might create the target sound in hw3(), then pass it into the first function to be modified, then pass it into the second function (perhaps with the current targetIndex, so that the next sound gets spliced into the end), and so on. Mark Guzdial


My program consists of two sounds merely copied into a larger file and then one that is normalized. The normalized one clips though. Is that okay, since it still meets the requirements?

Sounds like it. (Uh, no pun intended...:-) Mark Guzdial


is it ok if when i run my program, all of my sounds run simultaneously, or should I add a pause, and make then play one after the other. And if so, how do I do that?
You should have only ONE play() in your entire program, that just plays the target sound at the end. If you're just playing the different sounds, you're not meeting the requirements of the program. You have to copy all the sounds into the target sound. Mark Guzdial

Do I also need to attach my collage file? If so, under any specific title? Also, I am using the line writeSoundTo(target,r"C:\JES\JES\MediaSources\hw3.wav") but when I go into the file to find "hw3.wav" it does not exist. Any guidance?
That's a good idea – I'll change the homework assignment to say that. Your writeSoundTo will only work if you have target in the COMMAND AREA. This is all just like the picture collage – you want your hw3 function to RETURN the target, then you can type in the Command Area returnedsound = hw3(), and then writeSoundTo(returnedsound,"myaudiocollage.wav"). Mark Guzdial

yes, I'm wondering the same thing. I've got a sentence that plays forwards, then is reversed...I just want to put a slight pause between the two sentences. How is that done?
Put in some zeroes – see the discussion on p. 173 in the book. Mark Guzdial

Also, I've got 2 sounds. The first one runs forwards, then backwards, then the second sound is supposed to play...in that order. But when I run it, the first sound plays, then the first sound backwards. But when that backwards sound plays, the second sound file plays with it at the same time. Is this acceptable? If not: any ideas on how to keep them in a sequential order?Rizwan Ladha
That sounds like you have multiple play functions in your function. Generate ONE SOUND that has all of this in it – not PLAY ONE SOUND, RETURN ONE SOUND OBJECT. Mark Guzdial

I have a problem...my code is supposed to play a file, play the secon file, and then play the first file backwards...BUT it plays the first file, and then plays the first file backwards completely skipping the second file. Can someone tell me why??? THANKS! Susan Kommeth
code:
def collage():
  target=makeEmptySound(20)
  sound1=makeSound(getMediaPath("Elliot-hello.wav"))
  sound2=makeSound(getMediaPath("bassoon-c4.wav"))
  targetIndex=1
  for source in range(1,getLength(sound1)):
    value = getSampleValueAt(sound1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(sound2)):
    value = getSampleValueAt(sound2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(sound1)+1):
    value = getSampleValueAt(sound1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex - 1
  play(target)
  return target

Look carefully at your last loop, Susan. You're walking through the source forwards, while SUBTRACTING from the targetIndex. The result is that you overwrite the second sound with the third sound. If you moved the targetIndex to targetIndex+getLength(sound1) BEFORE that third loop, then you'll be filling in the right part of the target. As it is: At the end of the second loop, targetIndex is at the end of the second sound. As you subtract targetIndex while inserting the third sound, you overwrite the second sound. Mark Guzdial

why doesn't this play anything??? I know the sound exists and will play in Jes. I'm trying to increase and decrease the sound.

def hw3():
  target = makeEmptySound(15)
  pinball = makeSound(getMediaPath("sound181.wav"))
  targetIndex = 1
  for source in range (1, getLength(pinball)/2):
    value = getSampleValueAt(pinball, sampleIndex)
    setSampleValueAt(pinball, sampleIndex, value * 1.115)
    targetIndex = targetIndex + 1 
  for source in range (getLength(pinball)/2, getLength(pinball)+1):
    value = getSampleValueAt(pinball, sampleIndex)
    setSampleValueAt(pinball, sampleIndex, value * 0.2)
  play(target)
  return(target)


Karin Bowman



Check out these simple changes I made. You were copying the source samples back into the source. Also, 1.115 isn't really enough of a change to hear, and .2 is a little soft. I tweaked the numbers only so I could make sure it was working. Check over these and see if you understand what I did. Post any questions you have.
def hw3():
  target = makeEmptySound(15)
  pinball = makeSound(getMediaPath("gettysburg10.wav"))
  targetIndex = 1
  for sampleIndex in range (1, getLength(pinball)/2):
    value = getSampleValueAt(pinball, sampleIndex)
    setSampleValueAt(target, targetIndex, value * 3)
    targetIndex = targetIndex + 1 
  for sampleIndex in range (getLength(pinball)/2, getLength(pinball)+1):
    value = getSampleValueAt(pinball, sampleIndex)
    setSampleValueAt(target, sampleIndex, value * .5)
  play(target)
  return(target)
Greg Leo


How do I create a sound canvas for sound that is longer than the 3 second canvas that is already given to us with JES? I have 2 sounds that I am blending together, but they are about 8 seconds each. When I overlap them I want to have a sound that totals about 5-6 seconds, but am unsure of what code to write to create a sound canvas. Any tips? Is it the same as creating a canvas for images?...or do I need not use any canvas at all?
Catherine Billiris


makeEmptySound(20) will make you an empty (silent) sound of 20 seconds long. So, do something like canvas = makeEmptySound(15) to make you a longer sound (15 seconds long here). Mark Guzdial

Does it matter if two sounds play together? I had the same problem as Susan Kommeth some sentences ago - the sound that played backwards was simultaneous with the sound itself. But instead of tinkering with targetIndex, I added a pause that's as long as the sound. So now the sound that plays backwards, plays with the blank sound. It sounds ok, but wasn't sure if you were checking only for the final result, or also for the code that's used to get it.
Thanks.
Anu Koppikar

The final result must have THREE distinct sounds in it, and ONE of those sounds must be a modification of ANOTHER sound in the resultant sound. We'll listen to your sound and expect to hear the three sounds, and then check over your code. (Suggestion: Add the value of the sound you're copying in to the sample already there, and you can blend the sounds.) Mark Guzdial


I thought I had everything all figured out, and when I run my program it doesn't come up with any errors, but when I told it to play the finished sound, it said that a local or global name couldn't be found. Any ideas what I'm doing wrong? Kristin Noell

def hw3():
  #Establish sound files
  file = getMediaPath("canvas.wav")
  dest = makeSound(file)
  file1 = getMediaPath("gg_boom.wav")
  boom = makeSound(file1)
  file2 = getMediaPath("sh_madman.wav")
  madman = makeSound(file2)
  file3 = getMediaPath("bj_mommy.wav")

  #"Mommy mommy mommy" with volume changes
  #Add quiet volume "mommy"
  quietMommy = makeSound(file3)
  for sample in getSamples(quietMommy):
    value = getSample(sample)
    setSample(sample, value*0.5)
  return quietMommy
  source = quietMommy
  destSample = 1
  for srcSample in range(4257, 8133):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
  return dest
    
  #Add regular volume "mommy"
  regularMommy = makeSound(file3)
  source = regularMommy
  for srcSample in range(4257, 8133):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
  return dest
    
  #Add loud volume "mommy"
  loudMommy = makeSound(file3)
  source = loudMommy
  for sample in getSamples(loudMommy):
    value = getSample(sample)
    setSample(sample, value*2)
  return loudMommy
  for srcSample in range(4257, 8133):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
  return dest

  #Add the boom sound
  source = boom
  for srcSample in range(1, 27560):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
  return dest

  #Add the madman sound
  source = madman
  for srcSample in range(1, 57990):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
  return dest

  #Save and play
  writeSoundTo(dest, "JohnnyDepp.wav")
  finalFile = getMediaPath("JohnnyDepp.wav")
  finalSound = makeSound(finalFile)

What line, Kristin? Mark Guzdial


I see one real problem in your code, Kristin. Why do you have all those return statements? As soon as return executes, your function ENDS!. Nothing below that will execute! Why are you returning every few lines? You need only one return, one at the end of your function of your final destination sound dest. Mark Guzdial

I've tried to increase the volume for my first sound and this is what I have typed in:
def increaseVolume(sound1):
for sample in getSamples(sound1):
value = getSample(sample)
setSample(sample,value 2)
When I executed the function, it told me that I had an error and that I had to define the function. Please let me know what I did wrong. Thanks! =)

I'm trying to create an echo for my first sound. I used the function given on page 182 of the book. When I tried to execute it, it told me that it was unable to identify "p" and I had to define it first. I'm not sure what I'm supposed to do. Please let me know what to do.

The code ran fine for me. Make sure the for loop is:
for p in range(...
That's where p is defined. Otherwise, I don't see why you got that error. If the problem persists, just post that part of your code here. Houman Khalili

I am having trouble with my program, when i try to run it it gives me:
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.

her is my program please help:(i want to play one sound then a second and the the second sound squeaky like in breakout.

def makeclapping():
  hello = makeSound(getMediaPath("Elliot-hello.wav"))
  test = makeSound(getMediaPath("thisisatest.wav"))
  targetIndex = 1
  for index in range(1,getLength(test)):
    value = getSampleValueAt(test,index)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex+30000
  for index in range(1,getLength(hello)):
    value = getSampleValueAt(hello,index)
    setSampleValueAt(target,targetIndex,value+getSampleValueAt(target,targetIndex))
    targetIndex = targetIndex + 30000
  for index in range(1,getLength(hello),2):
    value = getSampleValueAt(hello,index)
    setSampleValueAt(target,targetIndex,value+getSampleValueAt(target,targetIndex))
    targetIndex = targetIndex + 1
  play(target)
  return(target)
Paula Verden
Paula, please do tell US WHICH line the error was on. That is useful information Mark Guzdial

did you setMediaPath ? Did you load it?

You haven't defined target, that's why it's giving you that error. If you are trying to copy the two sounds onto a blank .wav file, you have to define it at the very beginning, along with the hello, and test. I think there are blank .wav file available in the mediasources directory. Houman Khalili

Oh and you can actually can use the function, target = makeEmptySound(x), x being the length (in seconds) of the target sound you want. Houman Khalili
Paula, why would you want to add 30000 to the targetIndex for each sample in the source sound? That's a little extravagant, isn't it? One source sample spaced out by over a second in the target? Mark Guzdial

Is it bad to have comments in our code (to make it easier to read), or is it considered improper?
It's considered EXCELLENT! In most CS classes, you actually lose points if you do NOT have comments! Mark Guzdial

what do i type in the command area to try and play my code? thanks...
If you named the function with def hw3(): as the assignment says, perhaps hw3() would be a good thing to try? Mark Guzdial


Is it ok if I made an empty sound of 15 seconds but I only used about 6 seconds? Karin Bowman
Sure, no problem – I think your collage sounds really cool, Karin! Mark Guzdial

alright, so i made the code for some sounds I want and then when I try to run it, it works but it is really really quiet. all i did was add a sound to an empty file and its barely a whisper. and i turned up the volume on my computer so thats not it. What could it be? Sabrina Hassanali
Try multiplying the sample values by 2 or 3 – that'll increase the amplitude and thus the volume. Mark Guzdial

I figured out the problem I thought i put 3000 not 30000 thank professor Guzdiial
Paula Verden
Paula, you still probably don't want to add 3000 inside the loop. You probably want to only add one to the targetIndex inside the loop, but you might add/subtract more BETWEEN loops if you want to change where a splice starts. Mark Guzdial


I hate to keep posting so much code... I tried what Professor Guzdial suggested by removing all of the returns but the last one, but it's still telling me a local or global name cannot be found. Help! Kristin Noell

def hw3():
  #Establish sound files
  file = getMediaPath("canvas.wav")
  dest = makeSound(file)
  file1 = getMediaPath("gg_boom.wav")
  boom = makeSound(file1)
  file2 = getMediaPath("sh_madman.wav")
  madman = makeSound(file2)
  file3 = getMediaPath("bj_mommy.wav")

  #"Mommy mommy mommy" with volume changes
  #Add quiet volume "mommy"
  quietMommy = makeSound(file3)
  for sample in getSamples(quietMommy):
    value = getSample(sample)
    setSample(sample, value*0.5)
  source = quietMommy
  destSample = 1
  for srcSample in range(4257, 8133):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
    
  #Add regular volume "mommy"
  regularMommy = makeSound(file3)
  source = regularMommy
  for srcSample in range(4257, 8133):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1
    
  #Add loud volume "mommy"
  loudMommy = makeSound(file3)
  source = loudMommy
  for sample in getSamples(loudMommy):
    value = getSample(sample)
    setSample(sample, value*2)
  for srcSample in range(4257, 8133):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1

  #Add the boom sound
  source = boom
  for srcSample in range(1, 27560):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1

  #Add the madman sound
  source = madman
  for srcSample in range(1, 57990):
    sampleValue = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, sampleValue)
    destSample = destSample + 1

  return dest

  #Save and play
  writeSoundTo(dest, "JohnnyDepp.wav")
  finalFile = getMediaPath("JohnnyDepp.wav")
  finalSound = makeSound(finalFile)


Your return statement has to be the very LAST thing in the program, nothing else that is written after it will work. When you try to writePictureTo, you will get an error, because you have already returned dest–and nothing after returning will execute because your program is over! Summer McWilliams


Kristin, are you sure that clicked "Load"? I just ran your program (with different file names and some different indices), and it ran for me. As Summer said, the writeSoundTo doesn't execute, because it's after the return. You should NOT put writeSoundTo in your program, anyway. Do it in the Command Area. Mark Guzdial


COOL COLLAGES!

The collages that are showing up are really interesting! But they're mostly using the same sounds! (Albeit in some creative ways!) You do have a speech folder on your CD with lots of words in .wav files. You can also record sounds with MediaTools application or with Windows Sound Recorder. Let's hear some different sounds! Mark Guzdial


Professor Guzdial, I did click Load. I have moved the return dest to the very end and taken out the writeSoundTo(dest, "JohnnyDepp.wav"). However, when I ran the code and tried to write the sound out in the Command Area, it still tells me a local or global name cannot be found. So what could I possibly be doing? Kristin Noell

Kristin, does it specify a line number? If not, the error is in your Command Area. What commands are you typing, exactly? Mark Guzdial


It doesn't specify a line number. I typed writeSoundTo(dest, "JohnnyDepp.wav") and that error came up. Thanks for all the help! Kristin Noell

The variable dest is specified in your function (block), and is not recognized outside your function, in the command area. You have to set a variable equal to your function when running it in the command area. That's what the return statement is for, it returns the sound variable in your function, but you have to specify a variable outside. So for example you should have, mySound = hw3(), and then use the writeSoundTo function to write mySound to a .wav file. Houman Khalili

Exactly as Houman said! If you execute your function with mySound = hw3(), you need to say writeSoundTo(mySound,"mysoundsaved.wav") to save it. Mark Guzdial


You've seen how to increase the frequency of a sound by taking every other sample. One of the students in my breakout asked if it was possible to gradually speed up a sound so it starts out normal and gradually speeds up until it is at a target speed. It is possible, and it sounds pretty cool. The code itself is not very difficult. The code takes a sound and a target speed as input and returns the source file with the new sound. A simple algorithm changes the rate at which the source index is incremented based on the current source index. Thus, as the source index increase, the rate at which the source index increases speeds up. Because the sound will be shorted as a result of the increased speed, "0 value" samples are added over the old unedited sound left over from the original source. Speed Up Sound click to see the code and hear a sample. Please feel free to post comments, questions, or improvements.
Greg Leo


I can't seem to play any sounds at all even when I go to the command window and try to play it. I thought there was something wrong with my program, but it seems to be the files themselves for JES asks are you sure this is a valid sound file? It's .wav and those from the cd
What could be wrong?!
Are you running JES from a CD? JES will behave oddly if not installed on the hard disk. Are you running an old version of Java? Have you tried changing the volume to make sure that your sound hardware is working? Mark Guzdial

For hw3 I want to make something musical...playing a few notes and some chords. Will this meet the requirements or do I need to do something else with my code? Speaking of chords, we went over this in my class on Wednesday but I have missplaced my notes and have been trying make a chord with JES but have been unsucessful thus far. Any help with chords would be great
Jonathan Shirlock
Chapter 6 covers how to make chords (by adding sounds together), and it's in Monday's lecture notes. It's perfectly okay to use notes for your sounds. Mark Guzdial


I'm trying to reverse a sound. When it plays, though, it plays the original sound (in the forward direction) and then plays the reversed sound (it sounds like the reversed sound may start just before the end of the "forward sound," with a slight overlap). Here's my code, which I more or less copied from the lecture slides:
def reversePicture():
  sf=pickAFile()
  source=makeSound(sf)
  copy=makeSound(sf)

  currentsource=getLength(source)
  for i in range(1,getLength(copy)+1):
    setSampleValueAt(copy,i,getSampleValueAt(source,currentsource))
    currentsource=currentsource-1

  play(copy)

  return copy

Help is appreciated!
This looks okay to me – I don't see anything obviously wrong with it. Perhaps you're splicing it in wrong? You shouldn't have pickAFile in your program, BTW. The TA's will not want to pick a file when testing your program. Just use getMediaPath to access the right file by name. Mark Guzdial

>>> makeSound(pickAFile())
Filename C:\Documents and Settings\My Documents\School!\MediaSources\thisisatest.wav could not be read.
Are you sure it's a valid sound file?

I keep on getting this message.....
I'm not sure what's wrong!
Possibilities that I can suggest are your file is corrupted (grab the file again off the CD), you have an old version of Java that doesn't support reading WAV files, you are running JES off the CD so it's acting funny. I suggest using another computer for HW3 and/or see a TA on Monday to get help. (It's problems like these why it's better to start the HW before the weekend before it's due.) Mark Guzdial


I want to play a range of samples backwards. Is there a way to run my backwards(sound) function for just the range of samples I want?

def backwards(file):
source=file
target=file
sourceIndex=getLength(source)
for targetIndex in range(1,getLength(target)+1):
sourceValue = getSampleValueAt(source, sourceIndex)
setSampleValueAt(target,targetIndex,sourceValue)
sourceIndex = sourceIndex-1

Kelli Webb

Yes. Set the sourceIndex to the END of the sound you want to copy backwards (where you're now setting it to getLength(source). Figure out how LONG the sound is that you want to copy (in samples), and have targetIndex go from 1 to the length of the sound that you want to copy. Everything else stays the same. Mark Guzdial


I loaded my program, ran it–and JES didn't say anything was wrong with it..but after it ran there was NO sound. I think I may be doing something wrong, but I cannot seem to figure it out! Please help! Here is my Code:


Kyla LeCroy



oops, no idea, what happened there—let me try posting my code again:
def hw3():
  #getting the files i want and putting them into sound files
  world=getMediaPath("hello.wav")
  source = makeSound(world)
  beautiful=getMediaPath("beautiful.wav")
  source2=makeSound(beautiful)
  pirates=getMediaPath("pirates.wav")
  source3=makeSound(pirates)
  target=makeEmptySound(32)
  targetIndex=1#starts at first sample
  for sourceIndex in range (2806,20316):#where "hello world" stars and stops
    setSampleValueAt(target, targetIndex,  getSampleValueAt(source, sourceIndex))
    targetIndex = targetIndex + 1
def increaseVolume(beautiful):#increasing voulme of beautiful
  for sample in getSamples(beautiful):
    value = getSample(sample)
    setSample(sample,value * 2)
  for sourceIndex in range(11206, 1306504): # the area that I want from the part of "beautiful" and the sound increased
    setSampleValueAt(target, targetIndex, getSampleValueAt( source2, sourceIndex))
    targetIndex = targetIndex + 1
  for sourceIndex in range(2218,300774): # part of pirates that I want
    setSampleValueAt(target, targetIndex, getSampleValueAt( source3, sourceIndex))
    targetIndex = targetIndex + 1 
def playBackward(world): #playing hello world backwards
  source4 = makeSound(world)
  dest = makeSound(world)
  srcSample = getLength(source4)
  for destSample in range(1, getLength(dest)+1):
    srcVolume = getSampleValueAt(source4, srcSample)
    setSampleValueAt(dest, destSample, srcVolume)
    srcSample = srcSample - 1
  for sourceIndex in range(2806,20316):#playing "hello world" again only backward
    setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
    targetIndex = targetIndex + 1 
  play(target)	                                     #Let's hear and return the result
  return target


Kyla LeCroy

sorry for earlier mistake



since you defined your function outside of hw3() you need to call them within hw3() or dont' define them outside and include them by indentation within the hw3() function. Well, hope that helps!
Agreed with the above – you should NOT be defining functions within your function. Define them after hw3(), then call them from within hw3(). Mark Guzdial

For the sound issues with JES...
Filename C:\...\MediaSources\thisisatest.wav could not be read.
Are you sure it's a valid sound file?

What version of Java should one be running? I'm running Java 2 Runtime Environment 1.4.2_03
Also are there be any specific properties that our sound clips need to have, i.e bit rate, audio format, etc...other than just a .wav file?
That sounds fine. I think we can read any kind of .wav file, and certainly any of the .wav files on the disk. Mark Guzdial

Is it ok if my 3 seperate sounds play, but there is a little bit of time where the computer loads until each is played?
Jade Mcfadden
Jade, you should be COMBINING the three sounds into ONE sound, and then playing the ONE sound. You should NOT be playing three separate sounds. Mark Guzdial

I cannot get my code to play, either, but I do not have each sound defined separately like Kyla's... Any ideas?

def hw3():
  target = makeEmptySound(5)
  racecar = makeSound(getMediaPath("racecar.wav"))
  backward = makeSound(getMediaPath("backward.wav"))
  targetIndex = 1
  for source in range(1,getLength(racecar)):
    value = getSampleValueAt(racecar,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(backward)):
    value = getSampleValueAt(backward,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1  
  sourceIndex = 12741
  for targetIndex in range(1,12741):
    sourceValue = getSampleValueAt(racecar,(sourceIndex - 1))
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex - 1
  play(target)
  return target  

Your last loop makes no sense to me. If you have targetIndex in the FOR loop AND you are decrementing targetIndex within the FOR loop, (1) one of those must be wrong and (2) you're going to get really weird behavior. Mark Guzdial

I didn't paste that correctly, but it is correctly spaced... It isn't returning any errors.

Noelle Porter
Noelle, read the FAQ page on how to paste code so that we can read it. Mark Guzdial

I'm still having problems. This time I ran it on my PC and it returned the following error:I wasn't able to do what you wanted.
The error JavaSoundException has occured
JavaSoundException: That index 705601, does not exist. There are only 705600 frames in the entire sound
Please check line 19 of C:\Documents and Settings\Kyla\My Documents\hw3.py –what does this mean? Line 19 of my code is:
setSampleValueAt(target, targetIndex, getSampleValueAt( source2, sourceIndex))
And again, here is the rest of my code:
, 
def hw3():
  #getting the files i want and putting them into sound files
  world=getMediaPath("hello.wav")
  source = makeSound(world)
  beautiful=getMediaPath("beautiful.wav")
  source2=makeSound(beautiful)
  pirates=getMediaPath("pirates.wav")
  source3=makeSound(pirates)
  target=makeEmptySound(32)
  targetIndex=1#starts at first sample
  for sourceIndex in range (2806,20316):#where "hello world" stars and stops
    setSampleValueAt(target, targetIndex,  getSampleValueAt(source, sourceIndex))
    targetIndex = targetIndex + 1
  def increaseVolume(beautiful):#increasing voulme of beautiful
    for sample in getSamples(beautiful):
      value = getSample(sample)
      setSample(sample,value * 2)
  for sourceIndex in range(11206, 1306504): # the area that I want from the part of "beautiful" and the sound increased
    setSampleValueAt(target, targetIndex, getSampleValueAt( source2, sourceIndex))
    targetIndex = targetIndex + 1
  for sourceIndex in range(2218,300774): # part of pirates that I want
    setSampleValueAt(target, targetIndex, getSampleValueAt( source3, sourceIndex))
    targetIndex = targetIndex + 1 
  def playBackward(world): #playing hello world backwards
    source4 = makeSound(world)
    dest = makeSound(world)
    srcSample = getLength(source4)
    for destSample in range(1, getLength(dest)+1):
      srcVolume=getSampleValueAt(source4,srcSample)
    setSampleValueAt(dest, destSample, srcVolume)
    srcSample = srcSample - 1
  for sourceIndex in range(2806,20316):#playing "hello world" again only backward
    setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
    targetIndex = targetIndex + 1 
  play(target)	                                     #Let's hear and return the result
  return target


Can someone help me out please? Also, I did what someone suggested and put my functions within HW3, or so I think I did...please help :(

Kyla LeCroy
Kyla, this was discussed in Breakout this week. This error means that your targetIndex has gone beyond the edge of the target sound. Try using a longer empty sound and see if that helps, but you still need to move those functions out of there. Mark Guzdial


Well, you dont have to put your other functions within hw3. You can define other function then call them within hw3. For instance, just use a general input for def increaseVolume and playBackword, say def playBackward(source) and put that wherever you have beautiful and such. then in your hw3 function, all you have to type is playBackward(beautiful) inside and it should work

If we turnin twice, will our TA only check the second turnin?


yep, they will check the second turnin (your first will still be there, but the second one is the one that shows up).

When I type the code

target = makeEmptySound(5)

jes tells me a local or global name could not be found? What is going on when you type this?

blank page.

Dude, maybe your out of bounds. Check it out.

how do I check my bounds?

blank page.

Help!
when I Type:
>>> file = pickAFile()
>>> print file
C:\JES\MediaSources\aah.wav
JES gives me the pathway to aah.wav file but when I type:
>>>getMediaPath("aah.wav")
Note: There is no file at C:\JES\aah.wav
JES gives me this error.
please take a look at this part of my program, JES gives me a yellow line on the second line (aahsound):
def hw3():
aahsound = makeSound(getMediaPath("aah.wav"))
bassoonc4 = makeSound(getMediaPath("bassoon-c4.wav"))
bassoone4 = makeSound(getMediaPath("bassoon-e4.wav"))
bassoong4 = makeSound(getMediaPath("bassoon-g4.wav"))
target = makeEmptySound(6)
index = 1
# copy in "aahsound"
for scrSample in range (19096 , 40871):
setSampleValueAt(target, index, getSampleValueAt(aahsound, scrSample))
index = index + 1
play(target)
return(target)
Maryam Doroudi

Well, did you setMedia Path() first?

Ok, from what I understand,
target = makeEmptySound(x)

is a function that is native to Jes and that jes will highlight it in purple much like makeSound or anything like that. Im using the version of jes from the "alternative way to run jes" page and in this version of jes, it seems that makeEmptySound is not a function that jes can understand?
That's right, Matthew – makeEmptySound was added over the summer, and the alternative JES is old. Here's a suggestion: Find a Windows/Mac box, create the empty sound you want e.g., silence=makeEmptySound(10) then save it out writePictureTo(silence,"sec10silence.wav"). Use that as your canvas. Mark Guzdial

When I run my program, it doesn't give me any errors, but it doesn't do anything else either. It just gies me the following line: media.Sound instance at 8370296
but it doesn't actually play it! Any ideas? Meghan Quinlan
def hw3():
  target = makeEmptySound(73)
  preamble = makeSound(getMediaPath("preamble.wav"))
  gettysburg = makeSound(getMediaPath("gettysburg.wav"))
  beep = makeSound(getMediaPath("f440.wav"))
  targetIndex = 1
  playpreamble(preamble)
  playgettysburg(gettysburg)
  playbeeploud(beep)
  playpreamblebackwards(preamble)
  playgettysburgbackwards(gettysburg)
  play(target)
  return target

#play preamble
def playpreamble(preamble):
  target = makeEmptySound(73)
  targetIndex = 1
  for sampleIndex in range(1,getLength(preamble)):
    value = getSampleValueAt(preamble,sampleIndex)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
#play gettysburg
def playgettysburg(gettysburg):
  target = makeEmptySound(73)
  targetIndex = 1
  for sampleIndex in range(1,getLength(gettysburg)):
    value = getSampleValueAt(gettysburg,sampleIndex)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
#play beep
def playbeeploud(beep):
  for sample in getSamples(beep):
    value = getSample(sample)
    setSample(sample,value*2)
#play preamble backwards
def playpreamblebackwards(preamble):
  source = makeSound("preamble.wav")
  target = makeSound("preamble.wav")
  sourceIndex = getLength(source)
  for targetIndex in range(1,getLength(target)+1):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex = sourceIndex - 1
  return target
#play gettysburg backwards
def playgettysburgbackwards(gettysburg):
  source = makeSound("gettysburg.wav")
  target = makeSound("gettysburg.wav")
  sourceIndex = getLength(source)
  for targetIndex in range(1,getLength(target)+1):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex = sourceIndex - 1
  return target

Meghan, your hw3() doesn't actually return anything but a blank sound. You create a brand new target sound in each subfunction – you're not modifying the one that you create in hw3(). You're not passing it in as input. Therefore, each subfunction does what you ask it to do...on a target sound that then goes away when the function goes away. The target sound that you create in hw3() isn't actually changed, so when you return it, it's empty, and the program ends. I suggest you try this WITHOUT subfunctions, and then figure out how to pass the target sound into each subfunction to make them work properly. Mark Guzdial

Help!
can yo take a look at the last part of my program (decreaseVolume), please.

def hw3():
 aahsound = makeSound(getMediaPath("aah.wav"))
 bassoonc4 = makeSound(getMediaPath("bassoon-c4.wav"))
 bassoone4 = makeSound(getMediaPath("bassoon-e4.wav"))
 bassoong4 = makeSound(getMediaPath("bassoon-g4.wav"))
 target = makeEmptySound(6)
 index = 1
 # copy in "aahsound"
 for scrSample in range (19096 , 40871):
  setSampleValueAt(target, index, getSampleValueAt(aahsound, scrSample))
  index = index + 1
 # copy in "bassoonc4"
 for scrSample in range (6107, 20555):
  setSampleValueAt(target, index, getSampleValueAt(bassoonc4, scrSample))
  index = index + 1
 # copy in "bassoone4"
 for scrSample in range (87, 11987):
  setSampleValueAt(target, index, getSampleValueAt(bassoone4, scrSample))
  index = index + 1
 # copy in "bassoong4"
 for scrSample in range (1,20000):
  setSampleValueAt(target, index, getSampleValueAt(bassoong4, scrSample))
  index = index + 1
 # copy in decreaseVolume of the "aahsound"
def decreaseVolume(aahsound):
 for scrSample in getSamples(aahsound):
  value = getSample(scrSample)
  setSample(scrSample, value*0.5)
 for scrSample in range (19096 , 40871):
  setSampleValueAt(target, index, getSampleValueAt(aahsound, scrSample))
  index = index + 1
 play(target)
 return target

Maryam Doroudi
Same problem as Kyla, Maryam. You should not be defining decreaseVolume inside the function hw3(). Move it after hw3() – it's just like the collage code in the book. The functions go after, but call them from within hw3(). Mark Guzdial


Are there any sound length restrictions? My collage is 98 seconds long, I just wanted to make sure that was ok before I submitted it.
Jonathan Shirlock
No, we made no restrictions. An email to your TA warning him/her might be nice, though. Mark Guzdial

I wrote my code on a library computer. When I called it, it worked well, and I heard the sound. So, I know my code does what it is supposed to. However, when I tried to call the function at my dorm computer, I could not hear anything. But when I wrote the picture to mediasources, and double clicked on the file, it played. So, why can I not hear anything when I call the function in my room, while it works perfectly fine in the library? Is my copy of JES missing an essential component?
Hard to say from the description. First, make sure that sound is working at all on your dorm computer. Can you hear the beep when you change the volume from your dorm computer? Try restarting if you can't. Another possibility is that you don't have the same source sounds on the dorm computer, so it composed a different sound. Mark Guzdial

I keep getting an error message for the setSampleValueAt on the last loop: it says there's a Java sound exception? What's wrong with this... and if I fix that will this meet all of the requirements? Thanks~
def hw3():
  aah = makeSound(getMediaPath("aah.wav"))
  preamble =  makeSound(getMediaPath("preamble.wav"))
  target = makeEmptySound(10)
  canvas = makeSound(getMediaPath("sec3silence.wav"))
  targetIndex = 1
  #copy in aah
  for source in range(1,getLength(aah)):
    value = getSampleValueAt(aah,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 2
  targetIndex = targetIndex - 30000
  for source in range(1,getLength(aah)):
    value = getSampleValueAt(aah,source)
    setSampleValueAt(target,targetIndex,value+getSampleValueAt(target,targetIndex))
    targetIndex = targetIndex + 1
  targetIndex = targetIndex - 30000
  for source in range(1,getLength(aah)):
    value = getSampleValueAt(aah,source)
    setSampleValueAt(target,targetIndex,value+getSampleValueAt(target,targetIndex))
  targetTargetIndex = targetIndex + 2
  for targetIndex in range(1,getLength(preamble)):
    sample = getSampleValueAt(preamble,targetIndex)
    setSampleValueAt(target,targetIndex,sample)
    targetIndex = targetIndex+1
  play(target)
  return target

Tiffany Schoenhoff
Tiffany, this was discussed in breakout last week, and has appeared on this page several times. If you get that error, it means that you have gone beyond the end of the target sound. Try increasing its size. Mark Guzdial

I tried typing something a little different but now I'm having trouble with the area I tried to mirror; it says there's a spacing error with the sample later line...? What do I need to do?
Thanks~
PLEASE go read FAQ on how to format your code so that it's readable! You have an extra closing parenthesis on the mirror point calculation. Mark Guzdial
def hw3a()
  aah = makeSound(getMediaPath("aah.wav"))
  preamble = makeSound(getMediaPath("preamble.wav"))
  target = makeSound(getMediaPath("sec3silence.wav"))
  for source in range(1,getLength(aah)):
    value = getSampleValueAt(aah,source)
    setSampleValueAt(target,index,value)
    index = index + 2
  for source in range(1, int(0.05*getSamplingRate(target))):
    setSampleValueAt(target,index,0)
    index = index + 1
  for source in range(1,getLength(preamble,source)):
    setSampleValueAt(target,index,value)
    index = index + 1
  normalize(target)
  mirrorSound(target)
  writeSoundTo(target, "Homework")
  
def normalize():
  largest = 0
  for s in getSamples(sound):
    largest = max(largest, getSample(s) )
  multiplier = 32767.0 / largest
  for s in getSamples(sound):
    louder = multiplier * getSample(s)
    setSample(s,louder)

def mirrorSound():
  mirrorpoint = int(getLength(sound)/2.0))
  for sampleOffset in range(1, mirrorpoint-1):
    samplelater = getSampleObjectAt(sound,mirrorpoint+sampleOffset)
    samplebefore = getSampleObjectAt(sound,mirrorpoint-sampleOffset)
    value = getSample(samplebefore)
    setSample(samplelater,value)
  
play(target)
return (target)


I'm having a problem with the volume of my collage. When i try mark's clapping example, it plays at normal volume, however when i try my code, the volume comes out very very soft, at full volume it's still hard to hear. When I play the clips in Media tools however, clapping is the same volume as my clips.
This is just a short version, as simplified as i could get it, and it still has the problem.
As was discussed in breakout last week, if your sound is too soft, you can always multiply the sample values by 2 to double the amplitude and thus increase the volume by 3 dB. You could also normalize. Mark Guzdial

def playSound():
  target= makeEmptySound(10)
  lookGood = makeSound(getMediaPath("i look good in red.wav"))
  targetIndex = 1
  for source in range(3269,27478):
    value = getSampleValueAt(lookGood,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  play(target)
  return(target)

#9

This program is supposed to play the preamble, play the gettysburg adress, play the preamble backwards, and end by playing the gettysburg adress backwards. It only does some of that, and in the wrong order, and splicing sound. Can a fresh paor of eyes spot ant problems? Please and thank you! Meghan Quinlan
Your last two loops are wrong, Meghan. If you simply go forward with the sourceIndex and backward with the targetIndex WITHOUT CHANGING THE TARGETINDEX, you end up just overwriting the first two sounds that you put in. Mark Guzdial
def hw3():
  target=makeEmptySound(73)
  preamble=makeSound(getMediaPath("preamble.wav"))
  gettysburg=makeSound(getMediaPath("gettysburg.wav"))
  targetIndex = 1
#play preamble
  for source in range(1,getLength(preamble)):
    sample1= getSampleValueAt(preamble,source)
    setSampleValueAt(target,targetIndex,sample1)
    targetIndex=targetIndex + 1
#play gettysburg
  for source in range(1,getLength(gettysburg)):
    sample2= getSampleValueAt(gettysburg,source)
    setSampleValueAt(target,targetIndex,sample2)
    targetIndex = targetIndex + 1
#play preamble backwards  
  for source in range(1, getLength(preamble)):
    sample3= getSampleValueAt(preamble,source)
    setSampleValueAt(target,targetIndex+getLength(preamble),sample3)
    targetIndex=targetIndex - 1
#play gettysburg backwards
  for source in range(1, getLength(gettysburg)):
    sample4= getSampleValueAt(gettysburg,source)
    setSampleValueAt(target,targetIndex+getLength(gettysburg),sample4)
    targetIndex=targetIndex - 1
  play (target)
  return target


I've been having problems just getting the sounds to play. I keep getting a media instance problem even when I'm just trying to copy the examples from the book.... here's the message i'm getting
media.Sound instance at 32223077
John Lindsey
That's not an error, John. Try print hw3() and you'll see that you're simply seeing the sound being returned at the end of the function. Mark Guzdial


def hw3():
 target = makeEmptySound(8)
 but = makeSound(getMediaPath("but.wav"))
 sleep = makeSound(getMediaPath("sleep.wav"))
 targetIndex = 1
 for source in range(1,getLength(but)):
  value = getSampleValueAt(but,source)
  setSampleValueAt(target,targetIndex,value)
  targetIndex = targetIndex + 1
 for source in range(1,getLength(sleep)):
  value = getSampleValueAt(sleep,source)
  setSampleValueAt(target,targetIndex,value)
  targetIndex = targetIndex + 1
 play(target)
 return(target)



i'm trying to increase the volume for my first sound but it only plays both my sounds and ends there. It doesn't go on to play the increased sound. What do i do?
It's really impossible to know what's going on in your program with the given description – we'd have to see the code. You might want to review the notes from lecture Friday which described how to debug programs in which you are NOT getting any errors, but is NOT doing what you want either. It's also chapter 7 in the book. Mark Guzdial
I am having a problem with my backwards part, does anyone see what is going wrong with it? Thanks!
Kaysi Benefield
Yes, your backwards() function is fine, but it returns the reversed sound, and you don't do anything with it. You don't assign it a value, e.g., reversedsound = backwards(e1) and then use reversedsound. BTW, I'll bet that this isn't working right now – backwards() expects a filename as input, but you're giving it a sound. Mark Guzdial

def hw3():
  e1=makeSound(getMediaPath("elephant1.wav"))
  e2=makeSound(getMediaPath("elephant.wav"))
  target=makeEmptySound(10)
#copying e1 into target
  targetIndex=1
  for source in range(1,getLength(e1)+1):
    value = getSampleValueAt(e1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
#copying e2 into target
  for source in range(1,getLength(e2)+1):
    value = getSampleValueAt(e2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
#changing e1 and copying changed e1 into target
  backwards(e1)
  for source in range(1,getLength(e1)+1):
    value = getSampleValueAt(e1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  play(target)
  writeSoundTo(target, r"C:\\My Documents\\new.wav")
  return target

#def for backwards
def backwards(file):
  source=makeSound(file)
  target=makeSound(file)
  sourceIndex=getLength(source)
  for targetIndex in range(1, getLength(target)+1):
    sourceValue=getSampleValueAt(source, sourceIndex)
    setSampleValueAt(target, targetIndex, sourceValue)
    sourceIndex=sourceIndex-1
  return target


If I have 1 sound, then another sound, and then the first sound again, but shortened and lowered, does that meet the requirements?
Not sure what "lowered" means, but it sounds right. Mark Guzdial

ok so i wrote my program and it works when i use the sample sounds from mediasources but when i record my own phrases into wav files they do not make a sound at all when i run the program. why is this? the program has no flaws, just no words come from it, only the appears.

katherine colmer
Katherine, can you play the sounds you recorded at all from JES? Try them with play(makeSound(pickAFile())) from the Command Area. If you don't hear anything, then your WAV files aren't working with JES. Try re-recording using the MediaTools application. Mark Guzdial

I also cannot get the backwards part to work.
def hw3():
  target=makeEmptySound(20)
  sound1=makeSound(getMediaPath("concat.wav"))
  sound2=makeSound(getMediaPath("gettysburg10.wav"))
  #copy "Hello World..."
  targetIndex=1
  for source in range(1,getLength(sound1)):
    value=getSampleValueAt(sound1, source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex=targetIndex+1
  #copy "gettysburg10"
  for source in range(1,getLength(sound2)):
    value=getSampleValueAt(sound2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex=targetIndex+1
  #lower "Hello World" (cut off beeping sound)
  lower=makeSound(getMediaPath("concat.wav"))
  source= lower
  for sample in getSamples(lower):
    value=getSample(sample)
    setSample(sample,value*.2)
  for sourceIndex in range(1, 30481):
    sampleValue=getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target, targetIndex, sampleValue)
    targetIndex=targetIndex+1

  backwards(sound1)
 
  
 
  
  play(target)
  return(target)

 #backwards gettysburg
def backwards(sound1):
  source= sound1
  target= sound1
  sourceIndex=getLength(source)
  for targetIndex in range (1,getLength(target)+1):
    sourceValue=getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex=sourceIndex-1
  

Jonathan Longhitano

I now it says "backwards gettysburg at the bottom," but it is supposed to be backwards concat. I am trying to reverse the concat sound (sound1) Jonathan Longhitano

Similar problem as Kaysi, Jonathan. The backwards() function is supposed to reverse a sound then return the reversed sound. You've modified the backwards() function so that it won't work anymore (your version will actually mirror the sound around its middle), and you've removed the return. Since it's not returning anything, and doesn't actually reverse the input sound, it isn't working. Try going back to the original definition of backwards(), pass as input the FILENAME (not the SOUND), then USE the returned sound. Mark Guzdial


Trying to explain about backwards()


Someone just sent me this code, which has a mistake that several folks on this page are making. The backwards() function from the book has been modified to "work" (i.e., not generate errors), but doesn't actually DO anything anymore (e.g., it's not called, it doesn't return anything, its result doesn't impact the hw3() function).
def hw3():
  target=makeEmptySound(20)
  sound1=makeSound(getMediaPath("thisisatest.wav"))
  sound2=makeSound(getMediaPath("bassoon-e4.wav"))
  backsound=backwards(sound1)
  targetIndex=1
  for source in range(1,getLength(backsound)+1):
    value = getSampleValueAt(backsound,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(sound2)):
    value = getSampleValueAt(sound2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(sound1)+1):
    value = getSampleValueAt(sound1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  play(target)
  return target

def backwards(file):
  source = file
  target = file
  sourceIndex = getLength(source)
  for targetIndex in range(1,getLength(target)+1):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex = sourceIndex -1


So, what do we need to do to make this all work? It's actually not all that hard:
  1. We need to get backwards to RETURN the reversed sound in the target – otherwise, the reversed sound just goes away when the function does.
  2. We need to NAME the reversed sound that backwards() returns.
  3. We then need to use that NAMEd reversed sound to splice it in. Since the sound is already reversed for us, we can just paste it into our sound in a forwards direction.

Here's the program with those changes marked with ##COMMENTS at the end of each line:
def collage():
  target=makeEmptySound(20)
  sound1=makeSound(getMediaPath("thisisatest.wav"))
  sound2=makeSound(getMediaPath("bassoon-e4.wav"))
  targetIndex=1
  for source in range(1,getLength(sound1)):
    value = getSampleValueAt(sound1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  for source in range(1,getLength(sound2)):
    value = getSampleValueAt(sound2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  backwardsSound = backwards()  ## ADD THIS LINE TO CATCH THE RETURNED VALUE
  for source in range(1,getLength(backwardsSound)+1):  ## NOW USE backwardsSound
    value = getSampleValueAt(backwardsSound,source)  ## AND HERE
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
  play(target)
  return target

def backwards():
  source = makeSound(r"C:\JES\thisisatest.wav")
  target = makeSound(r"C:\JES\thisisatest.wav")
  sourceIndex = getLength(source)
  for targetIndex in range(1,getLength(target)+1):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex = sourceIndex -1
  return target  ## YOU NEED TO RETURN THE CREATED TARGET

Hope this helps! Mark Guzdial


I ran the normalize function on my .wav files and used the average value of that to increase the volume of my function by, is that alright? I tried using normalize in my program so that each .wav was multiplied by it's own multiplier but the sound was scratchy and had some "popping" noises it in. If i increase the volume of the target by the average it sounds great though... will this count for my example of manipulating the sound? By the way, my average multipler was around 275, this seems rather large to me, is there anything wrong with that? #9

Sure – what you're doing is "normalizing," but it sure sounds interesting! 275, based on an AVERAGE, doesn't sound that weird. That means that the average amplitude was 32767/275 = 119. Are you taking the absolute value or adding both positive and negative values together? Mark Guzdial

Ok, got it to work, except for the volume on my speakers is SUPER low. i boosted the total amplitude of the target wav file and it sounds almost right with my speakers turned all the way up. if i blow out the TA's speakers, will i be penalized? John Lindsey

for sample in getSamples(target):
  value = getSample(sample)
  setSample(sample,value*20)


:-) No, that's fine, John. Mark Guzdial


Okay, so I did exactly what professor Guzdial said above, to get the backwards to work, but now I am getting an out of bounds error, that says: JavaSoundException: JavaSoundException: That index 441001, does not exist. There are only 441000 frames in the entire sound.
The error is on the line that says #Error# PLEASE HELP!! THIS BACKWARDS JUST DOESNT WANT TO WORK!!!! Jonathan Longhitano


not sure why the code didnt show. Lets try again
def hw3():
  target=makeEmptySound(20)
  sound1=makeSound(getMediaPath("concat.wav"))
  sound2=makeSound(getMediaPath("gettysburg10.wav"))
  #copy "Hello World..."
  targetIndex=1
  for source in range(1,getLength(sound1)):
    value=getSampleValueAt(sound1, source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex=targetIndex+1
  #copy "gettysburg10"
  for source in range(1,getLength(sound2)):
    value=getSampleValueAt(sound2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex=targetIndex+1
  backwardsSound=backwards()
  for source in range(1,getLength(backwardsSound)+1):
    value=getSampleValueAt(backwardsSound,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndex=int(targetIndex + 1)
   #lower "Hello World" (cut off beeping sound)
  lower=makeSound(getMediaPath("concat.wav"))
  source= lower
  for sample in getSamples(lower):
    value=getSample(sample)
    setSample(sample,value*.2)
  for sourceIndex in range(1, 30481):
    sampleValue=getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target, targetIndex, sampleValue)
    targetIndex=targetIndex+1

  play(target)
  return(target)

 #backwards concat
def backwards():
  source= makeSound(getMediaPath("gettysburg10.wav"))
  target=makeSound(getMediaPath("gettysburg10.wav"))
  sourceIndex=getLength(source)
  for targetIndex in range (1,getLength(target)+1):
    sourceValue=getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex=sourceIndex-1
  return(target)
  

Jonathan Longhitano

okay, sorry again, the error is on line 19 Jonathan Longhitano
Jonathan, read some of the answers on this page to the exact same question. You've simply overrun the end of the targetSound. Make your target sound larger (maybe 25 seconds?) and then try again. Mark Guzdial

The other way of reversing sounds


I've had folks ask me "Is there another way to reverse sounds rather than use the backwards function?" Sure! You can do it by changing slightly the basic splicing loop.

You main splicing loop looks like this:
  targetIndex = 1  ##Only need this on the very first loop
  for sourceIndex in range(1,getLength(sourceSound)):
    value = getSampleValueAt(sourceSound,sourceIndex)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1


Now, to copy the sound in BACKWARDS, we need to make a few observations:
  1. How MANY samples do we need to move (forwards or backwards)? getLength(sourceSound) – it's the same NUMBER of samples in either direction.
  2. We don't want to do anything but INCREASE the targetIndex if we want to keep copying sounds on to the end.
  3. But we want to DECREMENT the sourceIndex. So we can't use it as the FOR loop.

Here's how we can do it:
  #Assuming targetIndex is already set by previous loop
  sourceIndex = getLength(sourceSound)
  for counter in range(1,getLength(sourceSound)):
    value = getSampleValueAt(sourceSound,sourceIndex)
    setSampleValueAt(target,targetIndex,value)
    targetIndex = targetIndex + 1
    sourceIndex = sourceIndex - 1

Hope this helps. Mark Guzdial


Another question I'm getting a lot of: Do we have to have range(1,getLength(sound)) or range(1,getLength(sound)+1)? If you want to get EVERY sample, use the latter one. Remember that range doesn't include the last number – range(1,10) is 1..9 (not 10). But in actual practice, it doesn't really matter. You'll never miss the last sample – it's 1/22100th of a second of a sound. You won't lose any points either way. Mark Guzdial

I know its a little late, but I've been working on this thing all weekend and it still doesnt work! Can anyone help me out?
def hw3():
  source = makeSound(getMediaPath("preamble10.wav"))
  source2 = makeSound(getMediaPath("thisisatest.wav"))
  target = makeSound(getMediaPath("preamble10.wav"))
  
  targetIndex = 17408
  for sourceIndex in range(40053,55511):
    setSampleValueAt(target,targetIndex,getSampleValueAt(source,sourceIndex))
    targetIndex=targetIndex+1
  targetIndex = 40053
  for sourceIndex in range(17408,30000):
    setSampleValueAt(target,targetIndex,getSampleValueAt(source,sourceIndex))
    targetIndex=targetIndex+1

  blendSounds(source2,target)


def blendSounds(source2,target):
  canvas = makeEmptySound(30)
  
  for index in range(1,55511):
    setSampleValueAt(canvas,index,getSampleValueAt(target,index))
  for index in range(1,55511):
    targetSample = getSampleValueAt(target,index+getLength(source2))
    source2Sample = getSampleValueAt(source2,index)
    newSample = 0.5 * targetSample + 0.5 * source2Sample
    setSampleValueAt(canvas,index+getLength(source2),newSample)
  for index in range(55511,55511+getLength(source2)):
    setSampleValueAt(canvas,index+getLength(source2),getSampleValueAt(source2,index))
  play(canvas)
  return(canvas)

It keeps returning the error "I wasn't able to do what you wanted.
The error JavaSoundException has occured
JavaSoundException: You are trying to access the sample at index: 64514, but there are only 64513 samples in the file!
Please check line 29 of ..."
Kevin Lennon
Please do read this page a bit. I've answered that question MANY times already. Mark Guzdial
Note: Making blendSounds() return the target is good, but if HW3() doesn't return the target sound, too, hw3() isn't returning anything, and that's wrong. Mark Guzdial

I am not sure if the last part of my code is working, i am confused about the not having def for increasing and then decreasing the sound, i don't have speakers on my computer so i cant really check this the right way. PLEASE HELP
def hw3():
  target=makeEmptySound(25)
  wav1=makeSound(getMediaPath("preamble.wav"))
  wav2=makeSound(getMediaPath("Elliot-hello.wav"))
  targetIndex=1
  for source in range(1,getLength(wav1)):
    value=getSampleValueAt(wav1,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndext=targetIndex + 1
  for source in range(1,getLength(wav2)):
    value=getSampleValueAt(wav2,source)
    setSampleValueAt(target,targetIndex,value)
    targetIndext=targetIndex + 1
  for sampleIndex in range(1,getLength(target)):
    value=getSampleValueAt(target,sampleIndex)
    setSampleValueAt(target,sampleIndex,value*2)
  for sampleIndex in range(1,getLength(target)):
    value=getSampleValueAt(target,sampleIndex)
    set sampleValueAt(target,sampleIndex,value*.8)
  play(target)
  return(target)

Why are you having your loops go for the length of the TARGET? Don't you really want the index to vary across the length of the SOURCE sound (wav1 or wav2)? And why are you getting the sample value from the TARGET and then putting it back into the TARGET? Wouldn't you get it from the SOURCE and then put it in the TARGET? Mark Guzdial


so basically
 for sampleIndex in range(1,getLength(wav1)):
    value=getSampleValueAt(wav1,sampleIndex)
    setSampleValueAt(target,sampleIndex,value*2)

these changes is what i am getting from your comments on my code
Erin: 




If you're still working on the HW and want some more sounds to work with, There's a folder of words on the Course Software (JES) page. Good luck to all those still working!

Yes, Erin, except that you can't use the same Index for both source and target. Use a separate targetIndex for the target. Mark Guzdial


And don't forget targetIndex = targetIndex+1 at the end of each of those loops! Mark Guzdial


When I play my collage, the sounds are all fast but when I play the individual sounds, they're fine. What could I be doing wrong?
When you play the individual sounds, they're planing at their own sampling rates. When you splice them into the collage, you're splicing them into a 22.050 Khz sampling rate, which means that they might be sped up or slowed down, depending on what the original sampling rates were. Mark Guzdial


how do i fix it?
take every other sample to slow down, take every sample twice to speed up. Page 183 in the book. Mark Guzdial

do i normalize the sound?
Page 164. Mark Guzdial

It's still not working and I can't figure out why. I looked through all the postings and increased the size of my sound file to 2 MINUTES and it's still giving me the error
"I wasn't able to do what you wanted.
The error JavaSoundException has occured
JavaSoundException: You are trying to access the sample at index: 64514, but there are only 64513 samples in the file!
Please check line 29..."
Can someone please help me?!

def hw3():
  source = makeSound(getMediaPath("preamble10.wav"))
  source2 = makeSound(getMediaPath("thisisatest.wav"))
  target = makeSound(getMediaPath("preamble10.wav"))
  
  targetIndex = 17408
  for sourceIndex in range(40053,55511):
    setSampleValueAt(target,targetIndex,getSampleValueAt(source,sourceIndex))
    targetIndex=targetIndex+1
  targetIndex = 40053
  for sourceIndex in range(17408,30000):
    setSampleValueAt(target,targetIndex,getSampleValueAt(source,sourceIndex))
    targetIndex=targetIndex+1

  blendedSound = blendSounds(source2,target)
  play(blendedSound)

def blendSounds(source2,target):
  canvas = makeEmptySound(120)
  
  for index in range(1,55511):
    setSampleValueAt(canvas,index,getSampleValueAt(target,index))
  for index in range(1,55511):
    targetSample = getSampleValueAt(target,index+getLength(source2))
    source2Sample = getSampleValueAt(source2,index)
    newSample = 0.5 * targetSample + 0.5 * source2Sample
    setSampleValueAt(canvas,index+getLength(source2),newSample)
  for index in range(55511,55511+getLength(source2)):
    setSampleValueAt(canvas,index+getLength(source2),getSampleValueAt(source2,index))
 
  return(canvas)

Kevin Lennon

to slow down my collage, i used the "playAtRate" function- but now when i play my hw3, it plays both the fast version and the slow version together. what's wrong?
def hw3():
  target = makeEmptySound(20)
  uhOh = makeSound(getMediaPath("uhOh.wav"))
  snakes = makeSound(getMediaPath("indianajones.wav"))
  genie = makeSound(getMediaPath("madeyalook.wav"))
  targetIndex = 1
  for source in range(1, getLength(uhOh)):
    value = getSampleValueAt(uhOh, source)
    setSampleValueAt(target, targetIndex, value)
    targetIndex = targetIndex + 1
  #targetIndex = targetIndex + 1
  for source in range(1, getLength(snakes)):
    value = getSampleValueAt(snakes, source)
    setSampleValueAt(target, targetIndex, value) #+ getSampleValueAt (target, targetIndex))
    targetIndex = targetIndex + 1
  #echo(delay)
  for source in range(1, getLength(genie)): 
    value = getSampleValueAt(genie, source)
    setSampleValueAt(target, targetIndex, value) #+ getSampleValueAt (target, targetIndex))
    targetIndex = targetIndex + 1
  normalize(target)
  playAtRate(target, 0.5)
  return target

def echo(delay):
  s1 = makeSound(getMediaPath("uhOh.wav"))
  s2 = makeSound(getMediaPath("uhOh.wav"))
  for p in range (delay + 1, getLength(s1)):
    setSampleValueAt(s1, p, getSampleValueAt(s1, p) + .6 * getSampleValueAt(s2, p - delay))
  play(s1)

def normalize(sound):
  largest = 0
  for s in getSamples(sound):
    largest = max(largest, getSample(s))
  multiplier = 32767.0 / largest
  for s in getSamples(sound):
    louder = multiplier * getSample(s)
    setSample(s, louder)

Don't use playAtRate. Instead, take every other sample from the source so that you slow it down. Mark Guzdial

Kevin, why do you have in line 29 "index + getLength(source2)"? Do you realize that when the index is getLength(source2), you're going to be trying to access twice the length of source2? Mark Guzdial


what time is HW 3 due?

5 pm. Mark Guzdial


I know that issues on volume have been addressed previously but I'm not sure that they apply, I can't tell. I wrote the code using Media Sources sounds and it worked fine. Then, I read where Mr. Guzdial wanted us to try new sounds, so, I recoreded some of my own and saved them into Media Sources and wav files, seemed right. Replaced everything in the code that needed to be replaced but when I ran it, I couldn't hear it. JES would think and then say "Media.Sound instance at ..." (8 digit number), so it seems like i'm not hearing it. So, I put in a new function to increase volume (the by range one) and have increase it by 2, 5, and 10 fold and it STILL isn't audible! I want to use my sounds but I'm about to switch back to the Media Sources and not be original! What am I doing wrong? thanks...

Try mysound = hw3() then play(mysound) in the command area. Mark Guzdial


I typed just "mysound = hw3()" into black part and increase volume by 200 and it worked and was finally loud enough...but how will my TA know to type that to get it to work?

I've gotten my program to work, and i've gotten it to run, but for some reason i can't get the writeSoundTo(sound,___) to work. I've tried everything i knew that should work from slides, book, lectures, and all the stuff i've done before; still doesn't work. Help please.

HELP! I can't turnin my file, it gives me error! it says: file not found(Access is denied)

Sounds like you're running JES off the CD. Try copying it onto the hard disk. Tell your TA, and you'll be given help for turning it in (see Grading Policy page). Mark Guzdial


I have a problem turnin them all together, it says "an unexpected error has occured!". I tried to send them 1 by 1, it worked!

1 by 1? If you turnin more than once, only the last one will be graded. If you uploaded the individual files to the TA's page, they won't be able to grade them well (they need the .zip files). Mark Guzdial




Link to this Page