[Speex-dev] SpeexEncoder requires 320 samples to process a
Frame, not 160
Jean-Marc Valin
jean-marc.valin at usherbrooke.ca
Tue Jul 18 22:11:33 PDT 2006
I think you'd have more luck posting to the jspeex mailing list.
Jean-Marc
On Tue, 2006-07-18 at 22:01 -0700, Thabang Kevin Khanye wrote:
> Hi guys
>
> I have tried compiling this attached code, I made all the buffers 320,
> there is no trace of a 160 buffer, but I get a "
> SpeexEncoder requires 320 samples to process a Frame, not 160" error.
> Maybe there's something I'm missing, here's my code:
>
> import java.io.IOException;
> import java.io.FileOutputStream;
> import java.io.File;
> import javax.sound.sampled.AudioFormat;
> import javax.sound.sampled.AudioSystem;
> import javax.sound.sampled.DataLine;
> import javax.sound.sampled.TargetDataLine;
> import javax.sound.sampled.AudioFormat.Encoding;
>
> import java.io.*;
> import org.xiph.speex.*;
>
>
> public class RecordTest
> {
>
> /**
> * @param args
> * @throws Exception
> */
> private static int nbBytes = 0;
> private static int obBytes = 0;
> private static int myNum = 0;
>
> public static void main(String[] args) throws Exception
> {
> AudioFormat format;
> TargetDataLine line;
> byte[] buffer;
> format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,8000.0f, 16,
> 1, 2, 8000.0f, false);
> DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
>
> // format is an AudioFormat object
> FileOutputStream fos = new FileOutputStream(new File("Testing.raw"));
>
> if (!AudioSystem.isLineSupported(info))
> throw new Exception("no line supported");
> line = (TargetDataLine)AudioSystem.getLine(info);
> line.open(format);
> buffer = new byte[320];
> byte[] mybuffer = new byte[4000000];
>
> line.start();
> System.out.println("Frame size: "+format.getFrameSize());
> int numBytesRead;
> System.out.println("The buffer length is "+buffer.length+"\nMy packet
> buffers are: "+nbBytes+"\n");
> System.out.println("My byffer bytes are: "+obBytes+"\nNumber of
> packets: "+myNum+"\nSize: "+myNum*nbBytes+"\nRecording started");
> while (System.in.available() == 0)
> {
> numBytesRead = line.read(buffer, 0, 320);
> System.out.println("Num bytes read = "+numBytesRead+"\n");
> buffer = encode(buffer);
> System.arraycopy(buffer, 0, mybuffer, myNum*nbBytes, nbBytes);
> myNum++;
> }
> line.stop();
> System.out.println("Packet Size = "+nbBytes);
> while (System.in.available() > 0)
> {
> System.in.read();
> }
> System.out.println("Recording stopped\n");
> System.out.println("The packet buffer bytes : "+nbBytes+"\nMy buffer
> length is : "+buffer.length);
> System.out.println("My complete decoded buffer : "+mybuffer.length
> +"\nNumber of packets: "+myNum+"\nSize: "+myNum*nbBytes
> +"\nDecoding...\n\n");
>
> mybuffer = decode(mybuffer);
> fos.write(mybuffer,0,myNum*320);
> fos.close();
> System.out.println("The buffer length is "+buffer.length+"\nMy packet
> buffers are: "+nbBytes+"\n");
> System.out.println("My byffer bytes are: "+obBytes+"\nNumber of
> packets: "+myNum+"\nSize: "+myNum*320+"\nDone.");
> System.exit(0);
> }
>
>
> /**
> * Encodes a wave file to speex.
> */
> public static byte[] encode(byte[] cmbuffer)
> {
> byte[] temp = new byte[320];
> int complexity = 3;
> int bitrate =-1;
> float vbr_quality = -1;
> boolean vbr = false;
> boolean vad = false;
> boolean dtx = false;
>
> // construct a new decoder
> SpeexEncoder speexEncoder = new SpeexEncoder();
> speexEncoder.init(1, 8, 8000, 1);
>
> if (complexity > 0)
> {
> speexEncoder.getEncoder().setComplexity(complexity);
> }
> if (bitrate > 0)
> {
> speexEncoder.getEncoder().setBitRate(bitrate);
> }
> if (vbr)
> {
> speexEncoder.getEncoder().setVbr(vbr);
> if (vbr_quality > 0)
> {
> speexEncoder.getEncoder().setVbrQuality(vbr_quality);
> }
> }
> if (vad)
> {
> speexEncoder.getEncoder().setVad(vad);
> }
> if (dtx)
> {
> speexEncoder.getEncoder().setDtx(dtx);
> }
>
> int pcmPacketSize = 320;
> speexEncoder.processData(cmbuffer, 0, pcmPacketSize);
> nbBytes = speexEncoder.getProcessedData(temp, 0);
> return temp;
> }
>
> public static byte[] decode(byte[] decbuffer)
> {
> SpeexDecoder speexDecoder = new SpeexDecoder();
> int packetCount = 0;
> byte[] payload = new byte[65536];
> int bodybytes = nbBytes;
> int decsize = 0;
> int Totsize = 0;
> byte[] completebuffer = new byte[4000000];
>
> Totsize = myNum * bodybytes;
>
> speexDecoder.init(1, 8000, 1, true);
>
> while((packetCount*nbBytes) <= Totsize)
> {
>
> System.arraycopy(decbuffer, packetCount*bodybytes, payload, 0,
> bodybytes);
>
> try
> {
> speexDecoder.processData(payload,0, bodybytes);
> byte[] decoded = new byte[speexDecoder.getProcessedDataByteSize()];
> decsize = speexDecoder.getProcessedData(decoded, 0);
> System.arraycopy(decoded, 0, completebuffer, packetCount*bodybytes,
> decsize);
> packetCount++;
> System.out.println("Decoding packet size: "+decsize+"\n");
> }
> catch(StreamCorruptedException s)
> {
> System.out.println("Error ---> "+s.getMessage());
> }
> }
>
> return completebuffer;
> }
> }
>
> Any help will greatly be appreciated.
>
> ______________________________________________________________________
> Do you Yahoo!?
> Everyone is raving about the all-new Yahoo! Mail Beta.
> _______________________________________________
> Speex-dev mailing list
> Speex-dev at xiph.org
> http://lists.xiph.org/mailman/listinfo/speex-dev
More information about the Speex-dev
mailing list