Edit Audio Using Python?

2007-06-20 Thread Ultrus
Hello Python Gurus,
I picked up a book the other day on Python programming. Python rocks!
I'm learning Python as I want to call upon it to handle some intensive
tasks from PHP/web server.

The top goal right now is automating audio editing using Python. Is it
possible? I was able to do this directly through php, but it was
rey slo. PHP is not designed for that sort of
thing.

So far I googled the Snack Sound Toolkit at http://www.speech.kth.se/snack/
. It seems to have potential, but is very limited, and has not been a
very active project lately from what I can tell. I'm not sure I can
use it without learning C or C++ to make it do what I want (fading
ends, appending - there but not working?, mixing). It may work once I
learn more about it.

Do you know of any other modules, scripts, or whatnot that allows for
fast .wav audio editing from a Python script (not through a visual
interface)? Would it be better to start from scratch?

Thank you much for the advise :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Edit Audio Using Python?

2007-06-20 Thread Ultrus
Ah! I found this on the official website:
http://www.python.org/doc/1.5.2p2/lib/module-audioop.html

That should keep me occupied. If you think of anything interesting
however, I would be happy to know. :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Edit Audio Using Python?

2007-06-21 Thread Ultrus
Aaron,
Thanks for sharing this!

> I think you'll find that you have to put a lot of pieces together
> to manipulate audio -- it's all there, but it's not straightforward.

The challenge of building something unique makes it more worth while.

> I did it in my "skimpygimpy" audio components, which may help
> (especially
> if you ignore the stranger parts where I went off the deep end).

Sweet program. I wish I had access to it on websites where I can never
seem to read the captcha letters.

The waveTools are VERY helpful! My biggest wall was getting samples
from audio files. Your scripts go beyond that. I'll play with the
scripts once I know a bit more on Python, and send you an update if I
expand upon it.

Thank you

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Edit Audio Using Python?

2007-06-29 Thread Ultrus
Thanks to Aaron, I was able to read and write audio data using
Python's wave module. Trying to better understand the data I'm looking
at, what does each element of the frame represent, and how do I
convert a sample ranging from -32,768 to 32,768 back to a frame set
like below?

When using a 16 bit mono wav file, reading a frame of audio produces 2
numbers like this:

import wave
file = wave.open("myWave.wav")
frame = file.readframes(1) #read first frame
file.close
#ord(frame[0]) = 0 to 256, ord(frame[1]) = 0 to 256,
#possible max of 65,536, or sample with range of -32,768 to 32,768


-- 
http://mail.python.org/mailman/listinfo/python-list


dealing with nested xml within nested xml within......

2007-07-09 Thread Ultrus
Hello all,
I don't need specific examples, but I'm trying to wrap my head around
parsing xml within xml and even further, not limiting how far someone
will nest xml. I'm already making great use of BeautifulSoup's
BeautifulStoneSoup to parse xml, but what do I do if I come across
something like this?


   This is a random response (once parsed)
   
  
 This is a random response within a random response
 

   This is a random response within a random response,
within another random response
   Like above, this is another random response.

 
  
   


Not knowing how far one will nest random responses, how would one
manage digging into xml like this? Right now I'm thinking about not
even going there. I would presently write scripts that would parse 3
or so levels deep, but no further. :P It would make an interesting
project, like an interactive adventure story.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dealing with nested xml within nested xml within......

2007-07-09 Thread Ultrus
> You'd probably write a function that called itself to parse something
> like this. Unfortunately, I am not a recursion expert. You can read up
> on it though:
>
> http://www.freenetpages.co.uk/hp/alan.gauld/tutrecur.htmhttp://pythonjournal.cognizor.com/pyj2.2/RecursionByAJChung.html

Ah! That's the termage I was looking for. Thanks much for the link. I
will research recursion.

-- 
http://mail.python.org/mailman/listinfo/python-list


replacing xml elements with other elements using lxml

2007-08-29 Thread Ultrus
Hello,
I'm attempting to generate a random story using xml as the document,
and lxml as the parser. I want the document to be simplified before
processing it further, and am very close to accomplishing my goal.
Below is what I have so far. Any ideas on how to move forward?

The goal:
read and edit xml file, replacing random elements with randomly picked
content from within

Completed:
[x] read xml
[x] access first random tag
[x] pick random content within random item
[o] need to replace  tag with picked contents

xml sample:
Here is some content.

   Here is some random content.
   Here is some more random content.

Here is some content.

Python code:
from lxml import etree
from StringIO import StringIO
import random

theXml = "Here is some content.Here is some random content.Here is some more random content.Here is some content."

f = StringIO(theXml)
tree = etree.parse(f)
r = tree.xpath('//random')

if len(r) > 0:
   randInt = random.randInt(0,(len(r[0]) - 1))
   randContents = r[0][randInt][0]
   #replace parent random tag with picked content here

now that I have the contents tag randomly chosen, how do I delete the
parent  tag, and replace it to look like this:

final xml sample (goal):
Here is some content.
Here is some random content.
Here is some content.

Any idea on how to do this? So close! Thanks for the help in
advance. :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replacing xml elements with other elements using lxml

2007-08-29 Thread Ultrus
Stefan,
I'm honored by your response.

You are correct about the bad xml. I attempted to shorten the xml for
this example as there are other tags unrelated to this issue in the
mix. Based on your feedback, I was able to make following fully
functional code using some different techniques:

from lxml import etree
from StringIO import StringIO
import random

sourceXml = "\
\
 Stefan's fortune cookie:\
 \
  \
   \
\
 You will always know love.\
\
\
 You will spend it all in one place.\
\
   \
  \
  \
   Your life comes with a lifetime warrenty.\
  \
 \
 The end.\
"

parser = etree.XMLParser(ns_clean=True, recover=True,
remove_blank_text=True, remove_comments=True)
tree = etree.parse(StringIO(sourceXml), parser)
xml = tree.getroot()

def reduceRandoms(xml):
for elem in xml:
if elem.tag == "random":
elem.getparent().replace(elem, random.choice(elem)[0])
reduceRandoms(xml)

reduceRandoms(xml)
for elem in xml:
print elem.tag, ":", elem.text




One challenge that I face now is that I can only replace a parent
element with a single element. This isn't a problem if an 
element only has 1  element, or just 1  element
(this works above). However, if  elements have more than one
child element such as a  element, followed by a 
element (like children of ), only the first element is used.

Any thoughts on how to replace+append after the replaced element, or
clear+append multiple elements to the cleared position?

Thanks again :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replacing xml elements with other elements using lxml

2007-08-29 Thread Ultrus
Ah! I figured it out. I forgot that the tree is treated like a list.
The solution was to replace the  element with the first 
child, then use Python's insert(i,x) function to insert elements after
the first one.

lxml rocks!

-- 
http://mail.python.org/mailman/listinfo/python-list