JES program simple question

This is the place for queries that don't fit in any of the other categories.

JES program simple question

Postby Carolien » Tue Jun 25, 2013 10:43 am

Hello everybody,

I have written a program in JES (Jython Environment for Students) and it works!
But now i want to add some more code and it goes wrong.

What the program does is changing the sequence of words in a simple sentence (Sound file): Is the battery not empty (in Dutch: 'ist de batterij niet leeg')
If you run my program 'createNewBattery', it changes this soundfile into the sentence: the battery is not empty (in Dutch: 'de batterij ist niet leeg').

For those who understand dutch, I change the filename is to ist, because 'is' is a keyword (sleutelwoord) in JES.

This is the program:
Code: Select all
def createNewBattery():
  file= pickAFile()
  battery= makeSound(file)
  ist = clip(battery,0,17303)
  deBatterij = clip(battery,17304,59345)
  niet = clip(battery,59346,73359)
  leeg = clip(battery,73360,91552)
  len = getLength(ist) + getLength(deBatterij) + getLength(niet) + getLength(leeg)
  newBattery = makeEmptySound(len)
  copy(ist,newBattery,getLength(deBatterij))
  copy(deBatterij,newBattery,0)
  copy(niet,newBattery,getLength(ist) + getLength(deBatterij))
  copy(leeg,newBattery,getLength(ist) + getLength(deBatterij) + getLength(niet))
  explore(newBattery)
  return newBattery 
 
def clip(source,start,end):
  target = makeEmptySound(end - start)
  targetIndex = 0
  for sourceIndex in range(start,end):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex + 1
  return target
 
   
def copy(source,target,start):
  targetIndex = start
  for sourceIndex in range(0,getLength(source)):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex + 1



This works!!!!!! So far so good.
Now i want to add some more code that reverses the word empty (in Dutch is empty = 'leeg' and if you reverse it, it becomes 'geel'. That means yellow in it English)
The code in my book for reverse is :

Code: Select all
def reverse(source):
  target = makeEmptySound(getLength(source))
  sourceIndex = getLength(source) - 1
  for targetIndex in range(0,getLength(target)):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    sourceIndex = sourceIndex - 1
  return target


But when i try to put this code into my working program, it look like this:

Code: Select all
def createNewBattery():
  file= pickAFile()
  battery= makeSound(file)
 
  ist = clip(battery,0,17303)
  deBatterij = clip(battery,17304,59345)
  niet = clip(battery,59346,73359)
  leeg = clip(battery,73360,91552)
 
 
  #stretchIst = stretch(ist)
  #shortenDeBatterij = shorten(deBatterij)
  normalizeNiet = normalize(niet)
  #reverseLeeg = reverse(leeg)
 
  len = getLength(ist) + getLength(deBatterij) + getLength(niet) + getLength(leeg)
  newBattery = makeEmptySound(len)
 
  copy(ist,newBattery,getLength(deBatterij))
  copy(deBatterij,newBattery,0)
  copy(normalizeNiet,newBattery,getLength(ist) + getLength(deBatterij))
  copy(leeg,newBattery,getLength(ist) + getLength(deBatterij) + getLength(normalizeNiet))
 
  explore(newBattery)
  return newBattery 
 
################ 
 
def clip(source,start,end):
  target = makeEmptySound(end - start)
  targetIndex = 0
  for sourceIndex in range(start,end):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex + 1
  return target
 
#def stretch(ist):

 
#def shorten(deBatterij):

 
def normalize(niet):
  largest = 0
  for s in getSamples(niet):
    largest = max(largest,getSampleValue(s))
  multiplier = 32767.0 / largest
  print "largest sample value in original sound was", largest
  print "multiplier is", multiplier
 
  for s in getSamples(niet):
    louder = multiplier * getSampleValue(s)
    setSampleValue(s,louder)
   

 
#def reverse(leeg): 
   

   
def copy(source,target,start):
  targetIndex = start
  for sourceIndex in range(0,getLength(source)):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex + 1


Then I get a lot of errors, for example line 64 (for sourceIndex in range(0,getLength(source)):)
So the program doesn't work anymore.

I have no idea how i can fix this, i've tried many times.
So please help me, if i can Reverse the word empty (leeg) then I think i know how Stretch, Shorten and Normalize other words to.
This is for school and i have to finished it this tuesday evening

thanks a lot, Carolien
Last edited by micseydel on Tue Jun 25, 2013 11:34 am, edited 1 time in total.
Reason: Changed blue code to use code tags, locked OP.
Carolien
 
Posts: 2
Joined: Tue Jun 25, 2013 10:09 am

Re: JES program simple question

Postby micseydel » Tue Jun 25, 2013 11:38 am

Hello, and welcome to the forum! It would appear that you have not read the new users topic, since you did not use code tags. Please do so. You're also welcome to introduce yourself here although that's not a requirement at all.

I tried to take a whack at the problem you've posted, but you should really read and take to heart the intro post and try to rephrase your problem in a more straightforward way.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 939
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: JES program simple question

Postby Carolien » Tue Jun 25, 2013 2:06 pm

Thanks micseydel for your reply!

I'll try to explain my problem.
I have written a program in JES (Jython Environment for Students) that takes a sound as input.

I recorded that sourcefile myself. It is a PCM .WAV soundfile in which you hear the Dutch sentence: "Is de batterij niet leeg". That means in English "Is the battery not empty".

My program clips and copies the words in this sentence and produces a targetfile that reads: ".. de batterij..is.. niet leeg". That means in English ".. the battery..is.. not empty".

It is a combination of two functions: 1. clip a word and 2. copy that word to a new position in the targetfile.
It works!! so far so good.

Now I want to add some more code, that should result in:
- shorten the words: "de batterij"
- stretch the word: "is"
- normalize the word: "niet" (normalize means: play as loud as possible)
- reverse the word: "leeg" -> "geel" which means "yellow" in English
This should result in: " [size=50]de batterij[/size] iiss niet geel"

I know how to shorten, stretch, normalize and reverse a soundfile, but only as a single step function for a single sourcefile. Now I want to add e.g. the function "normalise a soundfile" to the existing functions "clip" and "copy".
This is what I try to add in my program (as I have described in my first post).

Code: Select all
def createNewBattery():
  file= pickAFile()
  battery= makeSound(file)
 
  ist = clip(battery,0,17303)
  deBatterij = clip(battery,17304,59345)
  niet = clip(battery,59346,73359)
  leeg = clip(battery,73360,91552)
 
 
  #stretchIst = stretch(ist)
  #shortenDeBatterij = shorten(deBatterij)
  normalizeNiet = normalize(niet)
  #reverseLeeg = reverse(leeg)
 
  len = getLength(ist) + getLength(deBatterij) + getLength(niet) + getLength(leeg)
  newBattery = makeEmptySound(len)
 
  copy(ist,newBattery,getLength(deBatterij))
  copy(deBatterij,newBattery,0)
  copy(normalizeNiet,newBattery,getLength(ist) + getLength(deBatterij))
  copy(leeg,newBattery,getLength(ist) + getLength(deBatterij) + getLength(normalizeNiet))
 
  explore(newBattery)
  return newBattery 
 
################ 
 
def clip(source,start,end):
  target = makeEmptySound(end - start)
  targetIndex = 0
  for sourceIndex in range(start,end):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex + 1
  return target
 
#def stretch(ist):

 
#def shorten(deBatterij):

 
def normalize(niet):
  largest = 0
  for s in getSamples(niet):
    largest = max(largest,getSampleValue(s))
  multiplier = 32767.0 / largest
  print "largest sample value in original sound was", largest
  print "multiplier is", multiplier
 
  for s in getSamples(niet):
    louder = multiplier * getSampleValue(s)
    setSampleValue(s,louder)
   

 
#def reverse(leeg): 
   

   
def copy(source,target,start):
  targetIndex = start
  for sourceIndex in range(0,getLength(source)):
    sourceValue = getSampleValueAt(source,sourceIndex)
    setSampleValueAt(target,targetIndex,sourceValue)
    targetIndex = targetIndex + 1



When I run this program I get a "traceback" (in JES it is a yellow text in a black window):

======= Loading Progam =======
>>> createNewBattery()
largest sample value in original sound was 4895
multiplier is 6.6939734422880495
getLength(sound): Input is not a sound
The error was:
Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
in file C:\Program Files (x86)\JES 4.3\Sources\JESThread.py, on line 62, in function run
in file <input>, on line 1, in function ?
in file D:\#Carolien\A4B\Informatica\P4 geluid\PO geluid\Jes po\createNewBattery_mono_4xclip_SSNR_4xcopy, on line 21, in function createNewBattery
in file D:\#Carolien\A4B\Informatica\P4 geluid\PO geluid\Jes po\createNewBattery_mono_4xclip_SSNR_4xcopy, on line 64, in function copy
in file C:\Program Files (x86)\JES 4.3\Sources\media.py, on line 355, in function getLength
ValueError:
Please check line 64 of D:\#Carolien\A4B\Informatica\P4 geluid\PO geluid\Jes po\createNewBattery_mono_4xclip_SSNR_4xcopy
>>>



Which I understand as a successful run of the function "normalize" but an error in line 64 which is in fact an error in the function "copy" that ran successful before i added the "normalize" function.

What is going wrong here?
I studied the book "Introduction to Computing and Programming in Python, a multimedia approach" 3rd ed by Mark Guzdial and Barbara Ericson for the last couple of days, and that's why I ask these questions.

When I compare my program to the way a document is made (e.g. .doc or .odt)
1. I start a new document
2. I add characters and edit them (function 1) compare to: clip a word from the soundfile
3. I store this in a directory (save.. or save as..filename_version1)

4. When I want to edit this document, I recall it from the directory (open.. or open with..filename_version1)
5. I add characters and edit the document (function 2) compare to: normalise the soundfile normalise means play as loud as possible
6. I store this in a directory (save.. or save as.. filename_version 2)

7. When I want to edit this document, I recall it from the directory (open.. or open with..filename_version2)
8. I add characters and edit the document (function 3) compare to:copy the normalised soundfile to a new position in the targetfile
9. I store this in a directory (save.. or save as.. filename_version3)
etc.

How does Python / Jython / JES stores and recalls a sourcefile and a targetfile?
Where is the output of a function stored?
How can I recall an output from a function, to use it as an input for the next function?
How do i run several functions in succession? e.g. clip - normalize - copy, like i try to program

Carolien.
Carolien
 
Posts: 2
Joined: Tue Jun 25, 2013 10:09 am

Re: JES program simple question

Postby ochichinyezaboombwa » Tue Jun 25, 2013 5:05 pm

Hi Carolien,
I have no idea what is JES and how it all works, but Python clearly says what your problem is:
getLength(sound): Input is not a sound

So... look at your last change. What did you do? added a new functions normalize, right?
Code: Select all
normalizeNiet = normalize(niet)

Now, ask yourself a question: what is the result of this call? what does normalizeNiet equal to?

It is None. Because you never return anything from your function.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: tnknepp and 2 guests