Current location - Trademark Inquiry Complete Network - Futures platform - Urgent! ! ! ! How to make a simple tape recorder through python, record your own voice with 8k sampling and 16 bit quantization coding, and observe its value?
Urgent! ! ! ! How to make a simple tape recorder through python, record your own voice with 8k sampling and 16 bit quantization coding, and observe its value?
# I can write you a piece of code, which can be recorded to form a wav file, but to analyze the waveform of the recorded file, you can find another # tool, such as cooledit, which is also very convenient.

From where? sys? Import? Byte order

From where? Array? Import? rank

From where? struct? Import? package

Import? pyaudio

Import? wave

Threshold? =? 500

CHUNK_SIZE? =? 1024

Format? =? pyaudio.paInt 16

Rate? =? 44 100

def? is_silent(snd_data):

"Return? Really? What if? Below? That? Silence? Threshold "

Return? max(snd_data)? & lt? threshold value

def? Normalization (snd_data):

"generally? That? Volume? Get out "

Maximum? =? 16384

Times? =? Floating (max)/max (abs(i)? For what? Me? Are you online? snd_data)

r? =? Array ("h")

For what? Me? Are you online? snd_data:

r.append(int(i*times))

Return? r

def? trim(snd_data):

"pruning? That? Blank? Spots? Where is it? That? Start? And then what? End "

def? _trim(snd_data):

snd_started? =? wrong

r? =? Array ("h")

For what? Me? Are you online? snd_data:

What if? Isn't it? snd_started? And then what? ABS(I)& gt; Threshold:

snd_started? =? real

R. Add (i)

Elif? snd_started:

R. Add (i)

Return? r

#? Trim? Where to? That? left

snd_data? =? _trim(snd_data)

#? Trim? Where to? That? correct

snd_data.reverse()

snd_data? =? _trim(snd_data)

snd_data.reverse()

Return? snd_data

def? add_silence(snd_data,? Seconds):

"supplement? Silence? Where to? That? Start? And then what? End? Yes? snd_data '? Yes? Length? Seconds (Floating) "

r? =? Array ('h',? [0? For what? Me? Are you online? Xrange(int (second * rate))]

r.extend(snd_data)

r.extend([0? For what? Me? Are you online? Xrange(int (second * rate))]

Return? r

def? Record ():

"""

Record? Answer? Words? Or? Words? From where? That? Microphone? And then what?

Return? That? Data? As? Ann? Array? Yes? Signed it? Shorts.

Normalization? That? Audio? trims? Silence? From where? That?

Start? And then what? End,? And then what? Pad? With what? 0.5? Seconds Yes?

Blank? Sound? Where to? Manufacturing? Are you sure? VLC? et? Al. Is it okay? Play?

It? No? Become? Chop? Turn it off.

"""

p? =? pyaudio。 PyAudio()

Stream? =? p.open(format=FORMAT,? Channel = 1,? Rate = rate,

Input = true,? Output = true,

Frame per buffer = block size)

num_silent? =? 0

snd_started? =? wrong

r? =? Array ("h")

What time? 1:

#? Small? endian,? Signed it? short

snd_data? =? Array ('h',? stream.read(CHUNK_SIZE))

What if? byteorder? ==? Big':

snd_data.byteswap()

r.extend(snd_data)

Silence? =? is_silent(snd_data)

What if? Silence? And then what? snd_started:

num_silent? +=? 1

Elif? Isn't it? Silence? And then what? Isn't it? snd_started:

snd_started? =? real

What if? snd_started? And then what? num_silent? & gt? 30:

break

Sample _ width? =? P.get_sample_size (format)

stream.stop_stream()

stream.close()

P. terminate ()

r? =? Normalization (r)

r? =? Prune (r)

r? =? add_silence(r,0.5)

Return? Sample width? r

def? Log to file (path):

"Record? From where? That? Microphone? And then what? Output? That? And the result? Data? Where to? Path' "

Sample width? Data? =? Record ()

Data? =? Pack ('<'? +? (' h'*len(data)),? * data)

wf? =? Wave.open (path,? WB’)

wf.setnchannels( 1)

setsampwwidth(sample _ width)

Wf.setframerate (rate)

Wf.writeframes (data)

wf.close()

What if? __name__? ==? __main__ ':

Print ("Please? Talk? Answer? Words? Become? That? Microphone ")

Record to file ('demo.wav')

Print ("Done? -? The result? Written? Where to? demo.wav ")