0

I have generated sequence of frequency sound from text file using :

import mmap
import math
import pyaudio

fh = open('/home/jay/Documents/try.txt', 'rb')

m = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
ba = bytearray(m)

#sudo apt-get install python-pyaudio
PyAudio = pyaudio.PyAudio

#See http://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 16000 #number of frames per second/frameset.      

for freq in ba:
#See http://www.phy.mtu.edu/~suits/notefreqs.html
   FREQUENCY = 300 + freq #Hz, waves per second, 261.63=C4-note.
   LENGTH = 1 #seconds to play sound

   NUMBEROFFRAMES = int(BITRATE * LENGTH)
   RESTFRAMES = NUMBEROFFRAMES % BITRATE
   WAVEDATA = ''    

   for x in xrange(NUMBEROFFRAMES):
    WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))    

   #fill remainder of frameset with silence
   for x in xrange(RESTFRAMES): 
    WAVEDATA = WAVEDATA+chr(128)

   p = PyAudio()
   stream = p.open(format = p.get_format_from_width(1), 
                   channels = 1, 
                   rate = BITRATE, 
                   output = True)
   stream.write(WAVEDATA)
   stream.stop_stream()
   stream.close()
   p.terminate()

(try.txt can be any text file you want)

But its having some noise in between frequency sound how can i remove it and save sequence of all frequncy played in .wave or .mp3 file? Sorry i am still learning so if i am not clear in asking. Thanks, Jay

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Jay Patel
  • 127
  • 1
  • 1
  • 11

1 Answers1

0

Biggest problem here is that you open/close audio stream at each frequency.

Instead, keep the stream open and you'll get a lot less "clicks". That doesn't mean you'll get no clicks, probably because processing time is sometimes too long and the stream is interrupted. The best way would be to multithread generation and write to be more reactive, but that's another story...

I also added .wav save capacity. My output filenames. Works very well.

Fixed code (runs with Python 3)

import mmap
import math
import pyaudio,wave
import array


fh = open('K:\out.txt', 'rb')

m = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
ba = bytearray(m)

#ba = [300,400,500,400,200]
#sudo apt-get install python-pyaudio
PyAudio = pyaudio.PyAudio

#See http://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 16000 #number of frames per second/frameset.

p = PyAudio()
stream = p.open(format = p.get_format_from_width(1),
               channels = 1,
               rate = BITRATE,
               output = True)

wf=wave.open("K:\wavout.wav","wb")
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt8))  # byte = 8 bits else trashed
wf.setframerate(BITRATE)


for freq in ba:
#See http://www.phy.mtu.edu/~suits/notefreqs.html
   FREQUENCY = 300 + freq #Hz, waves per second, 261.63=C4-note.
   print('freq '+str(FREQUENCY))
   LENGTH = 1 #seconds to play sound

   NUMBEROFFRAMES = int(BITRATE * LENGTH)
   RESTFRAMES = NUMBEROFFRAMES % BITRATE
   WAVEDATA = list()


   for x in range(NUMBEROFFRAMES):
    v = int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128)
    WAVEDATA.append(v)

   #fill remainder of frameset with silence
   WAVEDATA+=[128]*RESTFRAMES

   b = array.array('B', WAVEDATA).tostring()

   wf.writeframes(b)
   stream.write(b)

#print data
wf.close()
stream.stop_stream()
stream.close()
p.terminate()
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • thanks its working .. have some clicks because of the issue you mentioned.. but it works like charm for me :) Thanks a ton .. – Jay Patel Sep 12 '16 at 17:03