Thursday, December 26, 2013

sqlite command quick search

1. create database
sqlite3 db1
2. create table
create table tb1(id varchar(10), name varchar(10));
3. insert data into table
insert into tb1 values('1','dude1');
4. select data
select * from tb1;
5. view structure
select * from sqlite_master;
6. show help
.help
7. list databases
.databases
8. list tables
.tables
9. change mode
.mode list/column/csv/html/tcl..
10. change seperator
.separator ","
11. change width
.width
12. show headers
.header on/off
13. backup database
echo '.dump' | sqlite3 db1 | gzip -c > db1.dump.gz
14. rebuild database from backup
zcat db1.dump.gz | sqlite3 db2
15. delete table
delete table tb1;
16. exit sqlite
.exit
17. describe table structure
.schema
      or
PRAGMA table_info(posts); 

Thursday, July 11, 2013

linux command reminder

1.Save the output of terminal to a file.
$ my_program ... | tee my_program.log

Monday, July 8, 2013

Record from mic by Python

There are many module you can use to do this.


1. PyAudio

import pyaudio
import wave

CHUNK = 1024 
FORMAT = pyaudio.paInt16 #paInt8
CHANNELS = 2 
RATE = 44100 #sample rate
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

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

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data) # 2 bytes(16 bits) per channel

print("* done recording")

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

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()

2. PyMedia

import time, sys
import pymedia.audio.sound as sound
import pymedia.audio.acodec as acodec

def voiceRecorder( secs, name ):
  f= open( name, 'wb' )
  # Minimum set of parameters we need to create Encoder

  cparams= { 'id': acodec.getCodecId( 'mp3' ),
             'bitrate': 128000,
             'sample_rate': 44100,
             'channels': 2 } 
  ac= acodec.Encoder( cparams )
  snd= sound.Input( 44100, 2, sound.AFMT_S16_LE )
  snd.start()
  
  # Loop until recorded position greater than the limit specified

  while snd.getPosition()<= secs:
    s= snd.getData()
    if s and len( s ):
      for fr in ac.encode( s ):
        # We definitely should use mux first, but for

        # simplicity reasons this way it'll work also

        f.write( fr )
    else:
      time.sleep( .003 )
  
  # Stop listening the incoming sound from the microphone or line in

  snd.stop()

# -------------------------------------------------------------------
# Record stereo sound from the line in or microphone 
# and save it as mp3 file
# Specify length and output file name
# http://pymedia.org/

if __name__ == "__main__":
  if len( sys.argv )!= 3:
    print 'Usage: voice_recorder  '
  else:
    voiceRecorder( int( sys.argv[ 1 ] ), sys.argv[ 2 ]  )

3. alsaaudio

import sys
import time
import getopt
import alsaaudio

def usage():
    sys.stderr.write('usage: recordtest.py [-c ] \n')
    print 'haha4'
    sys.exit(2)

if __name__ == '__main__':
    
    print 'haha1'
    
    card = 'default'

    opts, args = getopt.getopt(sys.argv[1:], 'c:')
    for o, a in opts:
        if o == '-c':
            card = a

        if not args:
            usage()
    f = open(args[0], 'wb')
    # Open the device in nonblocking capture mode. The last argument could
    # just as well have been zero for blocking mode. Then we could have
    # left out the sleep call in the bottom of the loop
    
    inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card)
    
    # Set attributes: Mono, 44100 Hz, 16 bit little endian samples
    
    inp.setchannels(1)
    inp.setrate(44100)
    inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
    
    # The period size controls the internal number of frames per period.
    # The significance of this parameter is documented in the ALSA api.
    # For our purposes, it is suficcient to know that reads from the device
    # will return this many frames. Each frame being 2 bytes long.
    # This means that the reads below will return either 320 bytes of data
    # or 0 bytes of data. The latter is possible because we are in nonblocking
    # mode.
    
    inp.setperiodsize(48)

    print 'haha2'
    loops = 100
    while loops > 0:
        print 'haha3'
        loops -= 1
        # Read data from device
        l, data = inp.read()
        print len(data)
        print l
        if l:
            f.write(data)
            time.sleep(.001)

How to get the maximum and minimum sampling rate for your audio cards in Linux

Source from :  voxForge

1. Sound Card or Integrated Audio

    $arecord --list-devices

try an sample rate very large or very small and you can get the message from your error message.

    $ arecord -f dat -r 60000 -D hw:0,0 -d 5 test.wav

2. USB Microphone or USB audio pod

   $  arecord -f S16_LE -r 60000 -D hw:1,0 -d 5 testS16_LE.wav

the same way mentioned above.

Wednesday, July 3, 2013

Install pyaudio on Fedora

$ yum install pyaudio

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~