2016-09-22 145 views
1

现在,我正在进行一个音频信号处理,并且我想要实时显示我手机录制的音频声波。如何将short []转换为double []?

这是我的音频格式为“ENCODING_PCM_16BIT”的问题。那么如何将16位短数据更改为双格式呢?

这是我的代码,但它的工作不正确。谁能帮我解决这个问题吗?

try { 

     AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, Sample_rate, Channel, Encording, 
       Buffersize); 

     DataOutputStream dos = new DataOutputStream(
       new BufferedOutputStream(new FileOutputStream(MainActivity.file))); 

     short[] buffer = new short[Buffersize/2]; //870 double/ 2 = 435 double 
     System.out.println("The buffer size is " + Buffersize); 
     timer1(); 
     audioRecord.startRecording(); // Start record 
     while (MainActivity.isrecord) { 
      int bufferReadResult = audioRecord.read(buffer, 0, buffer.length); 
      System.out.println("The buffer size is " + bufferReadResult); 
      for (int i = 0; i < bufferReadResult/2; i++) { 
       dos.writeShort(buffer[i]); 
       **tempraw[i] = (double)buffer[i];** 
      } 
      phase = DataProcess(tempraw); 
     } 
     audioRecord.stop(); // record stop 
     audioRecord.release(); 
     audioRecord = null; 
     dos.close(); // Output stream close 
     dos = null; 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我希望把这些数据在 “短[]缓冲区”,以 “双[] tempraw”

谢谢!

当我看了一些来自互联网的代码后,我做了这个。我认为它的工作,只是很慢,2ms的一个双

private static double shorttodouble(short[] a, int index) { 
    // TODO Auto-generated method stub 
    long l; 
    l = a[index + 0]; 
    l &= 0xffff; 
    l |= ((long) a[index + 1] << 16); 
    l &= 0xffffffffl; 
    l |= ((long) a[index + 2] << 32); 
    l &= 0xffffffffffffl; 
    l |= ((long) a[index + 3] << 48); 
    l &= 0xffffffffffffffffl; 
    l |= ((long) a[index + 4] << 64); 
    return (double)l; 
} 
+0

'...但它工作不正常...'究竟不能正常工作? –

+0

我有一个简短的[],并有short.length数字。对于每四个短号码,他们将转换为一个双值。但是,如果我使用我的代码中使用的方式,只需将其中一个短号码更改为双重格式即可。这是不正确的。我知道,因为我初始化短阵列就像那样“short [] buffer = new short [Buffersize/2]; double [] tempraw = new double [Buffersize/2/4]”。日蚀告诉我,“outofboundsexception” – MarvinC

+0

你必须编辑你的问题,并描述所有这一切。首先,向我们展示算法描述和实现。然后发布异常详情:消息,堆栈跟踪,原点。顺便尝试谷歌异常信息,可能你可以自己搞清楚。 –

回答

1

试试这个:---

short[] buffer = new short[size]; 
    double[] transformed = new double[buffer.length]; 
    for (int j=0;j<buffer.length;j++) { 
    transformed[j] = (double)buffer[j]; 
    } 
1

就像int数组双数组,你需要一个循环做这项工作:

short[] shorts = {1,2,3,4,5}; 
    double[] doubles = new double[shorts.length]; 

    for (int i = 0; i < shorts.length; i ++) { 
     doubles[i] = shorts[i]; 
     System.out.println(doubles[i]); 
    } 

效率低下而有效的。

+0

我正在检查这个(http://blog.csdn.net/cshichao/article/details/9813973?utm_source=tuicool&utm_medium=referral),并试图自己编写一个简单的[]加倍 – MarvinC