Showing posts with label audio. Show all posts
Showing posts with label audio. Show all posts

Wednesday, April 20, 2016

Make the witch speak!

By Vasudev Ram



This was fun and popular, so I'm blogging it again (I rarely do this, first time in fact):

Make the TTS witch speak:

1. Here is How.

2. Tremble with fear and awe.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new products and services.

My Python posts     Subscribe to my blog by email

My ActiveState recipes


Thursday, October 22, 2015

Play a list of WAV files with PyAudio

By Vasudev Ram




While looking at various Python libraries, I remembered PyAudio, which I had blogged about here earlier:

PyAudio and PortAudio - like ODBC for sound

This time I thought of using it to play a list of WAV files. So I wrote a small Python program for that, adapting one of the PyAudio examples. Here it is, in the file pyaudio_play_wav.py:
'''
Module to play WAV files using PyAudio.
Author: Vasudev Ram - http://jugad2.blogspot.com
Adapted from the example at:
https://people.csail.mit.edu/hubert/pyaudio/#docs
PyAudio Example: Play a wave file.
'''

import pyaudio
import wave
import sys
import os.path
import time

CHUNK_SIZE = 1024

def play_wav(wav_filename, chunk_size=CHUNK_SIZE):
    '''
    Play (on the attached system sound device) the WAV file
    named wav_filename.
    '''

    try:
        print 'Trying to play file ' + wav_filename
        wf = wave.open(wav_filename, 'rb')
    except IOError as ioe:
        sys.stderr.write('IOError on file ' + wav_filename + '\n' + \
        str(ioe) + '. Skipping.\n')
        return
    except EOFError as eofe:
        sys.stderr.write('EOFError on file ' + wav_filename + '\n' + \
        str(eofe) + '. Skipping.\n')
        return

    # Instantiate PyAudio.
    p = pyaudio.PyAudio()

    # Open stream.
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
        channels=wf.getnchannels(),
        rate=wf.getframerate(),
                    output=True)

    data = wf.readframes(chunk_size)
    while len(data) > 0:
        stream.write(data)
        data = wf.readframes(chunk_size)

    # Stop stream.
    stream.stop_stream()
    stream.close()

    # Close PyAudio.
    p.terminate()

def usage():
    prog_name = os.path.basename(sys.argv[0])
    print "Usage: {} filename.wav".format(prog_name)
    print "or: {} -f wav_file_list.txt".format(prog_name)

def main():
    lsa = len(sys.argv)
    if lsa < 2:
        usage()
        sys.exit(1)
    elif lsa == 2:
        play_wav(sys.argv[1])
    else:
        if sys.argv[1] != '-f':
            usage()
            sys.exit(1)
        with open(sys.argv[2]) as wav_list_fil:
            for wav_filename in wav_list_fil:
                # Remove trailing newline.
                if wav_filename[-1] == '\n':
                    wav_filename = wav_filename[:-1]
                play_wav(wav_filename)
                time.sleep(3)

if __name__ == '__main__':
    main()
Then I ran it as follows:

$ python pyaudio_play_wav.py chimes.wav

$ python pyaudio_play_wav.py chord.wav

$ python pyaudio_play_wav.py -f wav_file_list.txt

where wav_file_list.txt contained these two lines:

chimes.wav
chord.wav
Worked okay and played the specified WAV files. I also ran it a few times with test cases that should trigger errors - the error cases that the current code handles. This worked okay too. You can use one or more WAV files to try out the program. Other than modules in the Python stdlib, the only dependency is PyAudio, which you can install with pip. - Enjoy.

- Vasudev Ram - Online Python training and programming

Signup to hear about new products and services I create.

Posts about Python  Posts about xtopdf

My ActiveState recipes

Monday, January 19, 2015

Music video: Dire Straits - Walk of Life

By Vasudev Ram



An old favorite, listened to it again today. The video is good too.

Dire Straits

Dire Straits - Walk of Life:



- Vasudev Ram - Dancing Bison Enterprises

Saturday, March 1, 2014

Speech synthesis in Python with pyttsx

By Vasudev Ram

[ Click on the link below to hear a synthesized voice:
]

pyttsx is a library for cross-platform speech synthesis (a.k.a. text-to-speech) in Python. I saw it via this article on Dr. Dobbs Journal:

Voice Throwing with Python and Android.

I have an interest in speech synthesis and have tried out some tools for it in the past.

So I downloaded and installed pyttsx, and then tried it out a bit. Had to tweak the speech rate a bit, since the default "voice" was talking a bit too fast. Also, although going by its API, pyttsx seems to support more than one voice (if your underlying text-to-speech API supports that), the program only played the words in one voice (that of a woman), even though I ran it in a loop for all the voices. It could be that my OS doesn't support more than one voice, or maybe I need to use some other pyttsx option.

Here is the test program using pyttsx, test_pyttsx.py:
import pyttsx
engine = pyttsx.init()
engine.setProperty('rate', 70)

voices = engine.getProperty('voices')
for voice in voices:
    print "Using voice:", repr(voice)
    engine.setProperty('voice', voice.id)
    engine.say("Hi there, how's you ?")
    engine.say("A B C D E F G H I J K L M")
    engine.say("N O P Q R S T U V W X Y Z")
    engine.say("0 1 2 3 4 5 6 7 8 9")
    engine.say("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")
    engine.say("Violet Indigo Blue Green Yellow Orange Red")
    engine.say("Apple Banana Cherry Date Guava")
engine.runAndWait()

You can run it with the command:
python test_pyttsx.py



- Vasudev Ram - Dancing Bison Enterprises

Contact Page



Wednesday, August 28, 2013

Micawber gets rich content from URLs


By Vasudev Ram



micawber is a Python library that enables you to "fetch rich content from URLs" of videos, music, etc. I guess micawber is named after the character Micawber in the novel "David Copperfield" by Charles Dickens.

coleifer/micawber on GitHub

Read the micawber docs

I wrote a small test program to try out micawber, by modifying one of the basic examples of its use. It fetches some of the data about 3 YouTube videos: Andres Segovia playing 'Asturias', Guido van Rossum's talk - 'In Python We Trust', and Luisa Fernandez singing 'Granada'.

Here is the program:
# test_micawber.py

import micawber

# load up rules for some default providers, such as youtube and flickr
providers = micawber.bootstrap_basic()

# Fetch the data for 3 video URLs
for url in [ 'http://www.youtube.com/watch?v=9efHwnFAkuA',
    'http://www.youtube.com/watch?v=mWB3oh1GPdo',
    'http://www.youtube.com/watch?v=UYGc9joeAD0' ]:
    
    retval = providers.request(url)
    for key in ['type', 'title', 'url', 'author_name', 
    'author_url', 'provider_name', 'provider_url', 'height',
    'width', 'thumbnail_url']:
        print key, ":", retval[key]

    print

# EOF
You can run it like this:
python test_micawber.py
And here is its output:
type : video
title : Andres Segovia - Asturias
url : http://www.youtube.com/watch?v=9efHwnFAkuA
author_name : ByTheSPiRiTs
author_url : http://www.youtube.com/user/ByTheSPiRiTs
provider_name : YouTube
provider_url : http://www.youtube.com/
height : 344
width : 459
thumbnail_url : http://i1.ytimg.com/vi/9efHwnFAkuA/hqdefault.jpg

type : video
title : In Python We Trust
url : http://www.youtube.com/watch?v=mWB3oh1GPdo
author_name : OreillyMedia
author_url : http://www.youtube.com/user/OreillyMedia
provider_name : YouTube
provider_url : http://www.youtube.com/
height : 270
width : 480
thumbnail_url : http://i1.ytimg.com/vi/mWB3oh1GPdo/hqdefault.jpg

type : video
title : Luisa Fernandez -  Granada
url : http://www.youtube.com/watch?v=UYGc9joeAD0
author_name : Dany Massart
author_url : http://www.youtube.com/user/DJCOOLDANY
provider_name : YouTube
provider_url : http://www.youtube.com/
height : 344
width : 459
thumbnail_url : http://i1.ytimg.com/vi/UYGc9joeAD0/hqdefault.jpg

- Vasudev Ram - Dancing Bison Enterprises

Contact me

Sunday, April 28, 2013

PySynth, a pure Python music synthesizer


By Vasudev Ram

PySynth is a music synthesizer library written in Python. (*)

Excerpt from the site:

[ There are three variants: PySynth A is faster, only needs Python itself, and sounds more like a cross between a flute and organ. PySynth B is more complex in sound and needs NumPy. It's supposed to be a little closer to a piano. (No competition for Pianoteq of course, but a reasonable fit for keyboard music.) Finally, PySynth S is more comparable to a guitar, banjo, or harpsichord, depending on note length and pitch.

The current release of the synthesizer is monophonic, i.e. it can only play one note at a time. (Although successive notes can overlap in PySynth B and S, but not A.) However, two output files can be mixed together as in the case of the stereo files below. ]

(*) Interestingly, the Changes section of the above linked PySynth page seems to indicate that PySynth uses both Pyglet and PyAudio, both of which I had blogged about some time ago:

Playing an MP3 with pyglet and Python is easy

PyAudio and PortAudio - like ODBC for sound

Here is PySynth on Github

PySynth supports ABC notation and can generate WAV audio files.

Here is an example tune:

D D C C B A G

and here is a PySynth program to play that tune as a WAV file:
# pysynth-ddccbag.py

import pysynth

test = ( ('d', 4), ('d', 4), ('c', 4), ('c', 4), ('b', 4), ('a', 4), ('g', 3) )
pysynth.make_wav(test, fn = "ddccbag.wav")
For some reason the last note seems to get partially cut off on my PC. Not sure whether that is a bug or a feature :-) or something to do with my hardware. Maybe the latter, since I'm using my laptop's built-in speakers.

Run that program as:
python pysynth-ddccbag.py 

Then play the generated WAV file ddccbag.wav in a music player, such as VLC or some other one.

Here are a few better examples of sound synthesis by PySynth - the links are to MP4 files, which you can download and play without needing Python or PySynth:

The Sailor’s Hornpipe — PySynth S

Bach: Jesu, Joy of Man’s Desiring — PySynth S (treble) and B (bass)

Also check my recent post: Play the piano on your computer with Python.

If you want to try it out, note that the program has a couple of bugs, related to the frequencies of notes, since I am not musically trained; I just wrote it for fun and as an experiment. Though some of the post comments gave some background and suggested corrections, they did not seem to be sure, and corrected themselves, so I have not implemented any of those suggestions yet.


Enjoy.

- Vasudev Ram - Dancing Bison Enterprises

Wednesday, October 19, 2011

Text To Speech for Chrome Extensions, and Talking Alarm Clock

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

Saw this recently on the Google Code blog

Tried it out some, it's quite cool and useful, IMO:

New Text-to-Speech API for Chrome extensions

http://googlecode.blogspot.com/2011/10/new-text-to-speech-api-for-chrome.html

As the title of the post indicates, this is about a new Text To Speech (TTS) API for Google Chrome extensions and apps.

Though the blog post title only mentions the API, the post also has links to TTS (i.e. talking) apps for Chrome. You can install them from the Chrome Web Store. There are also links to a couple of other "voices" that can be used for all such Chrome TTS (Text To Speech) apps or extensions. Even without installing the voices, you may be able to run TTS apps using the native voice support of your operating system

I tried out the TTS Demo and the Talking Alarm Clock - both were good.

The Talking Alarm Clock in particular can be useful. Apart from being useful, the audio alarm that goes off when the alarm time is reached, can be played in different ways - e.g., as a traditional ringing alarm clock, a digital alarm clock, a cuckoo clock, a grandfather clock, a "morning in the forest" tune, etc. Pretty cool ...

I had developed a simple utility something roughly like it in Delphi a while earlier

Posted via email

- Vasudev Ram @ Dancing Bison

Thursday, October 13, 2011

PyAudio and PortAudio - like ODBC for sound

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

PyAudio and PortAudio are I/O libraries for sound (i.e. audio).

I'm stretching the analogy a bit here, but they made me think:

"... like ODBC for sound". (*)

PyAudio is a Python interface to PortAudio.

PyAudio:

http://people.csail.mit.edu/hubert/pyaudio/

Excerpt:

[ PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms. ]

PortAudio:

http://www.portaudio.com/

PortAudio apps:

http://www.portaudio.com/apps.html

I installed PyAudio for Windows. Installation was trivial. It also automatically installed the PortAudio DLL (actually, the .pyd file).

I then tried a PyAudio Python program from the docs to play a small .WAV file. It worked.

(*) That's because PyAudio and PortAudio support both:

a) different platforms (Windows, Linux, Mac, UNIX)

b) different "Host APIs" on the same platform, where the different Host API's have, obviously, different API's, but PortAudio (and hence PyAudio) hide those differences behind a uniform interface (to some extent).


UPDATE: If you interested in checking out other Python multimedia libraries, you may also like to read this earlier post of mine about pyglet:

Playing an MP3 with pyglet and Python is easy

pyglet has some of the same benefits as PyAudio - cross-platform, easy to use, etc.

Posted via email

- Vasudev Ram @ Dancing Bison

Thursday, July 9, 2009

Playing an MP3 with pyglet and Python is easy

By Vasudev Ram

pyglet is a cross-platform windowing and multimedia library for Python.

Playing an MP3 with pyglet and Python is as easy as this:

import pyglet

music = pyglet.resource.media('your_file.mp3')
music.play()

pyglet.app.run()

Some of the features of pyglet (from the pyglet site):

  • No external dependencies or installation requirements. For most application and game requirements, pyglet needs nothing else besides Python, simplifying distribution and installation.

  • Take advantage of multiple windows and multi-monitor desktops. pyglet allows you to use as many windows as you need, and is fully aware of multi-monitor setups for use with fullscreen games.

  • Load images, sound, music and video in almost any format. pyglet can optionally use AVbin to play back audio formats such as MP3, OGG/Vorbis and WMA, and video formats such as DivX, MPEG-2, H.264, WMV and Xvid.


Another good thing about pyglet is that it's a small download; also, installation went fast and without a hitch. Immediately after installing it (on Windows, with the bundled AVBin), I was able to run the above code. No configuration was required. AVbin is a thin wrapper around FFmpeg, which is a cross-platform solution to record, convert and stream audio and video.

I've not yet checked whether it's possible to pause and continue the played audio, though, either programmatically or via some graphical controls of pyglet.

UPDATE: Yes, GUI controls are possible. See the code for this audio and video player with simple GUI controls, in the link titled "examples/media_player.py" near the end of this page.

Currently listening to an MP3 of a tech interview that's being played by the above pyglet program.



- Vasudev Ram - Dancing Bison Enterprises.