hi,
Thanks for your kindly help :-)
However, I am still confused about the RATE, frames_per_buffer and
frame, do you mind giving me some information on them?
----------
Regards,
Lingfeng Xiong
“人民身体之自由应予保障。除现行犯之逮捕由法律另定外,非经司法或警察机关
依法定程序,不得逮捕拘禁。非由法院依法定程序,不得审问处罚。非依法定程序
之逮捕、拘禁、审问、处罚,得拒绝之。”
-- 《中华民国宪法》
On 100/11/14 下午 05:56, Martin Storsjö wrote:
> Hi,
>
> On Mon, 14 Nov 2011, Lingfeng Xiong wrote:
>
>> I am trying to encode a voice stream by opencore-amr, this stream is
>> captured by PortAudio libray. However, I found whatever data I passed to
>> Encoder_Interface_Encode(475), it would always output 13 elements in
>> "output" array. So I am wondering how often should I encode my voice
>> stream?
>
> If you encode with mode 0, each AMR frame is encoded to 13 bytes, so
> that looks correct.
>
>> This is the code:
>>
>> > import pyaudio
>> > import wave
>> > import sys
>> > import ctypes
>> >
>> > chunk = 512
>
> One AMR-NB frame is 160 samples (320 for AMR-WB), so you should give it
> that many samples each time you call Encoder_Interface_Encode.
>
>> > FORMAT = pyaudio.paInt8
>
> You should pass 16 bit integer samples to the encoder, this most
> probably is the reason why it doesn't sound right at all.
>
> I'm not familiar with this python wrapper library, so you might need to
> either use 160 or 320 as chunk length, depending on whether it's
> regarded as 320 bytes or 160 16-bit samples.
>
>> > CHANNELS = 1
>> > RATE = 22800
>
> AMR-NB normally is 8000 Hz only. You can of course record the data at
> any rate you want, but then you'd have to make sure the playback is at
> the same rate.
>
>> > RECORD_SECONDS = 5
>> >
>> > p = pyaudio.PyAudio()
>> > stream = p.open(format = FORMAT,
>> > channels = CHANNELS,
>> > rate = RATE,
>> > input = True,
>> > frames_per_buffer = chunk)
>> >
>> > libamr = ctypes.cdll.LoadLibrary("libopencore-amrnb.so")
>> > amrstate = libamr.Encoder_Interface_init(0)
>> >
>> > print "* recording"
>> > outfile = open("record.amr", "wb")
>> > outfile.write("#!AMR\n")
>> > for i in range(0, RATE / chunk * RECORD_SECONDS):
>> > data = stream.read(chunk)
>> > print "data length is %d" % len(data)
>> > outbuf = ctypes.create_string_buffer(600)
>> > n = libamr.Encoder_Interface_Encode(amrstate, 0, data, outbuf, 0);
>> > outfile.write(outbuf[0:n])
>> > print "* done recording"
>> >
>> > outfile.close()
>> > stream.close()
>> > p.terminate()
>
> // Martin
|