how framing byte packet to time length from android to python server?
<p>I'm trying to develop an application which send to pcm data to python server.</p>

<p>I used AudioRecord library to get real-time audio signal.</p>

<p>And this is the source code.</p>

<pre><code>/*------ setting audio recording ------*/

private static final int SAMPLE_RATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

private boolean isRecording = true;
private AudioRecord recorder = null;
private Thread recordingThread;
private AudioTrack player;
//byte[] TotalByteMessage;

/*------ about socket communication ------*/
public DatagramSocket socket;
private int port = 7979;
String IP = "192.168.0.4";



/*------ Recording, Playing and Sending packets method ------*/

private void startStreaming() {

    recordingThread = new Thread(new Runnable() {
        @Override
        public void run() {

            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

            try {
                /*------about socket------*/
                socket = new DatagramSocket();
                Log.d(LOG_NW, "Socket Created!");
                DatagramPacket packet;

                InetAddress destination = InetAddress.getByName(IP);
                Log.d(LOG_NW, "Address retrieved!");


               /*------setting recording &amp;&amp; playing------*/
                //get MinBufferSize for audio recording
                int Buffer_Size = AudioRecord.getMinBufferSize(SAMPLE_RATE,
                        RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
                Log.d(LOG_Audio, "Min buffer size is " + Buffer_Size);

                if (Buffer_Size == AudioRecord.ERROR || Buffer_Size == AudioRecord.ERROR_BAD_VALUE) {
                    Buffer_Size = SAMPLE_RATE * 2;
                }


                recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION,
                        SAMPLE_RATE, RECORDER_CHANNELS,
                        RECORDER_AUDIO_ENCODING, Buffer_Size);

                if (recorder.getState() != AudioRecord.STATE_INITIALIZED) {
                    Log.d(LOG_Audio, "Audio Record can't initialize!");
                    return;
                }


                player = new AudioTrack(AudioManager.STREAM_MUSIC,
                        SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                        RECORDER_AUDIO_ENCODING, Buffer_Size,
                        AudioTrack.MODE_STREAM);
                Log.d(LOG_Audio, "ready for playing music by using audiotrack");

                player.setPlaybackRate(SAMPLE_RATE);

                byte[] audioBuffer = new byte[Buffer_Size];
                Log.d(LOG_Audio, "AudioBuffer created of size " + Buffer_Size);

                recorder.startRecording();
                Log.d(LOG_Audio, "Start Recording!");

                player.play();
                Log.d(LOG_Audio, "Start Playing!");


                while (isRecording == true) {
                    //reading data from MIC into buffer
                    recorder.read(audioBuffer, 0, audioBuffer.length);
                    player.write(audioBuffer, 0, audioBuffer.length);


                    //putting buffer in the packet
                    packet = new DatagramPacket(audioBuffer, audioBuffer.length, destination, port);

                    socket.send(packet);
                    Log.d(LOG_NW, "packet sending to  " + destination + " with port : " + port);

                }

            } catch (UnknownHostException e) {
                Log.d(LOG_Audio, "UnknownHostException");
            } catch (IOException e) {
                Log.d(LOG_Audio, "IOException");
            }
        }
    }); // end of recordingThread

    recordingThread.start();
}
</code></pre>

<hr>

<p>and this is the python server code.</p>

<pre><code>import socket    
import numpy as np
import matplotlib.pyplot as plt

IP = "192.168.0.4"  
server_address = (IP, 7979)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
buffer_size = 3584
server.bind(server_address)

i = True
while(i):
    print "Listening...\n"
    packet, client = server.recvfrom(buffer_size)
    #Convert packet to numpy array
    signal = np.fromstring(packet, dtype=np.int16)    
    i=False
server.close()
</code></pre>

<p>With this python code, I receive only one packet.</p>

<p>But later, I'll make a list to receive several packets.</p>

<p>I want to make a frame which has size of 64ms (time!) for windowing and FFT(Fast Fourier Transform), but the problem is a packet is 3584 bytes.</p>

<p>So I don't know how to windowing and do fft with byte packet from android in python server.</p>

<p>How can I make a frame using time length?   </p>

<p>You have a problem with your while loop in your python code</p>

<pre><code>i = True
while(i):
    print "Listening...\n"
    packet, client = server.recvfrom(buffer_size)

    #Convert packet to numpy array
    signal = np.fromstring(packet, dtype=np.int16)    

    i=False
server.close()
</code></pre>

<p>This will enter the loop, since <code>i == True</code>, then go through one iteration, and set <code>i = False</code> at the end of the first iteration, which will cause the loop to finish.</p>

android python sockets fft