Thursday, June 20, 2013

Spotify-Hello World

Play Button Generator:

Thursday, June 13, 2013

Install PyQt on Mac OSX Lion 10.8.4

I have been searching for ways to successfully install PyQt4 on my macbook pro with Lion 10.8.4. Following are the steps:
source from http://blog.csdn.net/watsy/article/details/8857252, thanks to watsy.
1. Download and install Qt: http://qt-project.org/downloads What I downloaded is Qt 5.0.2 for Mac(404MB), Decompress and follow the steps, quite easy.
2. Download SIP: http://www.riverbankcomputing.co.uk/software/sip/download
I download the development snapshots sip-4.14.7-snapshot-74e1df1d9940.tar.gz
Decompress it. Commands in Terminal:
cd sip-4.14.7-snapshot-74e1df1d9940
python configure.py -d /Library/Python/2.7/site-packages --arch=x86_64
(change the arch to i386 if your mbp is 32 bits)
make
sudo make install
3.Download PyQt4: http://www.riverbankcomputing.co.uk/software/pyqt/download
I downloaded PyQt-mac-gpl-4.10.2-snapshot-11b3001947c9.tar.gz
cd PyQt-mac-gpl-snapshot-4.10.2-ffcf323516fc
python configure-ng.py -q /Users/qqli/Qt5.0.2/5.0.2/clang_64/bin/
make -d /Library/Python/2.7/site-packages/ --sip /System/Library/Frameworks/Python.framework/Versions/2.7/bin/sip
make
sudo make install
After all the steps, you can try from PyQt4 import QtGui if there is no error message, Congratulation. Have fun with PyQt :)

Wednesday, June 12, 2013

Real time voice chat example in python using pyaudio

PyAudio home page:

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

There is download links and install instructions for windows/OSX/linux. Following is the code for the audio transmission between server and client in one direction. First the client records the audio from the mic and store in a buffer and then transmit by TCP socket. The server receives the data and play out by speaker.
For Client:

import socket
import pyaudio
import wave

#record
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 40

HOST = ''    # The remote host
PORT = 50007              # The same port as used by the server

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("*recording")

frames = []

for i in range(0, int(RATE/CHUNK*RECORD_SECONDS)):
 data  = stream.read(CHUNK)
 frames.append(data)
 s.sendall(data)

print("*done recording")

stream.stop_stream()
stream.close()
p.terminate()
s.close()

print("*closed")

For Server:


# Echo server program
import socket
import pyaudio
import wave
import time

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 4
WAVE_OUTPUT_FILENAME = "server_output.wav"
WIDTH = 2
frames = []

p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                output=True,
                frames_per_buffer=CHUNK)


HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
data = conn.recv(1024)

i=1
while data != '':
    stream.write(data)
    data = conn.recv(1024)
    i=i+1
    print i
    frames.append(data)

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

stream.stop_stream()
stream.close()
p.terminate()
conn.close()

In the server part, I store the received audio into an wav file just for record. If you want to do voice chat, you can add another process and deal with the echo cancellation. Also for better audio quality and noise reduction, you may add threshold at the client. Also for better performance under worse network situation, you may need to try compress the audio before transmission.

Friday, June 7, 2013

Shall we begin

First blog on Blogger. Duo Duo Guan Zhao~