- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码记录,播放和“解码” wav文件。当我从文件中绘制字节数组时,它看起来像这样:
录音是我说的“测试”,它应该像这样:
有谁知道为什么wav文件中字节数组的图看起来不像真实的音频数据?
这是整个 Activity 的代码:
package com.example.wesle.noisemachine;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.widget.Toast;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
public class ReceiveScreen extends AppCompatActivity {
private Button buttonStart, buttonStop, buttonDecode, buttonPlay;
private String filePath;
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
short[] audioData;
private AudioRecord recorder = null;
private int bufferSize = 0;
private Thread recordingThread = null;
private boolean isRecording = false;
Complex[] fftTempArray;
Complex[] fftArray;
int[] bufferData;
int bytesRecorded;
LineGraphSeries<DataPoint> series;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_screen);
filePath = getFilename();
final File wavfile = new File(filePath);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonDecode = (Button) findViewById(R.id.buttonDecode);
buttonStop.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
bufferSize = AudioRecord.getMinBufferSize
(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING)*3;
audioData = new short [bufferSize];
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStart.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING,
bufferSize);
int i = recorder.getState();
if (i==1)
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
@Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
buttonStop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStop.setEnabled(false);
if (null != recorder){
isRecording = false;
int i = recorder.getState();
if (i==1)
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
copyWaveFile(getTempFilename(),filePath);
deleteTempFile();
Toast.makeText(getApplicationContext(), "Recording Completed", Toast.LENGTH_LONG).show();
buttonStart.setEnabled(true);
buttonPlay.setEnabled(true);
buttonDecode.setEnabled(true);
}
});
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStart.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
Toast.makeText(getApplicationContext(), "Recording Playing", Toast.LENGTH_LONG).show();
Uri myUri1 = Uri.fromFile(wavfile);
final MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
buttonStart.setEnabled(true);
buttonDecode.setEnabled(true);
buttonPlay.setEnabled(true);
}
});
buttonDecode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStart.setEnabled(false);
buttonDecode.setEnabled(false);
buttonPlay.setEnabled(false);
GraphView thegraph = (GraphView) findViewById(R.id.thegraph);
series = new LineGraphSeries<DataPoint>();
double x,y;
x = 0;
try {
ByteArrayOutputStream outt = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(wavfile));
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
outt.write(buff, 0, read);
}
outt.flush();
byte[] audioBytes = outt.toByteArray();
//int[][] graphData = getUnscaledAmplitude(audioBytes, 1);
for(int i = 0; i < audioBytes.length;i++){
//System.out.println(audioBytes[i]);
byte curByte = audioBytes[i];
//int curByte = graphData[0][i];
y = (curByte);
series.appendData(new DataPoint(x,y), true, audioBytes.length);
x = x + 1;
//x = x + (1 / RECORDER_SAMPLERATE);
}
thegraph.addSeries(series);
} catch (IOException e) {
e.printStackTrace();
}
buttonStart.setEnabled(true);
buttonDecode.setEnabled(true);
buttonPlay.setEnabled(true);
}
});
//Code for the back button
Button backbuttonR = (Button) findViewById(R.id.backbuttonR);
backbuttonR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ReceiveScreen.this, MainActivity.class));
}
});
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
System.out.println(file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_WAV);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_WAV);
}
private String getTempFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
if (tempFile.exists())
tempFile.delete();
return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}
private void writeAudioDataToFile() {
byte data[] = new byte[bufferSize];
String filename = getTempFilename();
FileOutputStream os = null;
try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
int read = 0;
if (null != os) {
while(isRecording) {
read = recorder.read(data, 0, bufferSize);
if (read > 0){
}
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void deleteTempFile() {
File file = new File(getTempFilename());
file.delete();
}
private void copyWaveFile(String inFilename,String outFilename){
FileInputStream in = null;
FileOutputStream out = null;
long totalAudioLen = 0;
long totalDataLen = totalAudioLen + 36;
long longSampleRate = RECORDER_SAMPLERATE;
int channels = 2;
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;
byte[] data = new byte[bufferSize];
try {
in = new FileInputStream(inFilename);
out = new FileOutputStream(outFilename);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
System.out.println("File size: " + totalDataLen);
WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);
while(in.read(data) != -1) {
out.write(data);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void WriteWaveFileHeader(
FileOutputStream out, long totalAudioLen,
long totalDataLen, long longSampleRate, int channels,
long byteRate) throws IOException
{
byte[] header = new byte[44];
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (byte) channels;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
header[32] = (byte) (2 * 16 / 8); // block align
header[33] = 0;
header[34] = RECORDER_BPP; // bits per sample
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
out.write(header, 0, 44);
}
public static final class Complex {
// The number stored is x+I*y.
final private double x, y;
// I don't want to allow anyone to access these numbers so I've labeled
// them private.
/** Construct a point from real and imaginary parts. */
public Complex(double real_part, double imaginary_part) {
x=real_part;
y=imaginary_part;
}
/** Construct a real number. */
public Complex(double real_part) {
x=real_part;
y=0;
}
// A static constructor.
/** Construct a complex number from the given polar coordinates. */
public static Complex fromPolar(double r, double theta) {
return new Complex(r*Math.cos(theta), r*Math.sin(theta));
}
// Basic operations on Complex numbers.
/** Return the real part. */
public double re(){
return x;
}
/** Return the imaginary part. */
public double im(){
return y;
}
/** Return the complex conjugate */
public Complex conj() {
return new Complex(x,-y);
}
/** Return the square of the absolute value. */
public double absSquared() {
return x*x+y*y;
}
/** Return the absolute value. */
public double abs() {
// The java.lang.Math package contains many useful mathematical functions,
// including the square root function.
return Math.sqrt(absSquared());
}
// ARITHMETIC
/** Add a complex number to this one.
*
* @param z The complex number to be added.
* @return A new complex number which is the sum.
*/
public Complex add(Complex z) {
return new Complex(x+z.x, y+z.y);
}
/** Subtract a complex number from this one.
*
* @param z The complex number to be subtracted.
* @return A new complex number which is the sum.
*/
public Complex minus(Complex z) {
return new Complex(x-z.x, y-z.y);
}
/** Negate this complex number.
*
* @return The negation.
*/
public Complex neg() {
return new Complex(-x, -y);
}
/** Compute the product of two complex numbers
*
* @param z The complex number to be multiplied.
* @return A new complex number which is the product.
*/
public Complex mult(Complex z) {
return new Complex(x*z.x-y*z.y, x*z.y+z.x*y);
}
/** Divide this complex number by a real number.
*
* @param q The number to divide by.
* @return A new complex number representing the quotient.
*/
public Complex div(double q) {
return new Complex(x/q,y/q);
}
/** Return the multiplicative inverse. */
public Complex inv() {
// find the square of the absolute value of this complex number.
double abs_squared=absSquared();
return new Complex(x/abs_squared, -y/abs_squared);
}
/** Compute the quotient of two complex numbers.
*
* @param z The complex number to divide this one by.
* @return A new complex number which is the quotient.
*/
public Complex div(Complex z) {
return mult(z.inv());
}
/** Return the complex exponential of this complex number. */
public Complex exp() {
return new Complex(Math.exp(x)*Math.cos(y),Math.exp(x)*Math.sin(y));
}
// FUNCTIONS WHICH KEEP JAVA HAPPY:
/** Returns this point as a string.
* The main purpose of this function is for printing the string out,
* so we return a string in a (fairly) human readable format.
*/
// The _optional_ override directive "@Override" below just says we are
// overriding a function defined in a parent class. In this case, the
// parent is java.lang.Object. All classes in Java have the Object class
// as a superclass.
@Override
public String toString() {
// Comments:
// 1) "" represents the empty string.
// 2) If you add something to a string, it converts the thing you
// are adding to a string, and then concatentates it with the string.
// We do some voodoo to make sure the number is displayed reasonably.
if (y==0) {
return ""+x;
}
if (y>0) {
return ""+x+"+"+y+"*I";
}
// otherwise y<0.
return ""+x+"-"+(-y)+"*I";
}
/** Return true if the object is a complex number which is equal to this complex number. */
@Override
public boolean equals(Object obj) {
// Return false if the object is null
if (obj == null) {
return false;
}
// Return false if the object is not a Complex number
if (!(obj instanceof Complex)) {
return false;
}
// Now the object must be a Complex number, so we can convert it to a
// Complex number.
Complex other = (Complex) obj;
// If the x-coordinates are not equal, then return false.
if (x != other.x) {
return false;
}
// If the y-coordinates are not equal, then return false.
if (y != other.y) {
return false;
}
// Both parts are equal, so return true.
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 83 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
hash = 83 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
return hash;
}
}
public int[][] getUnscaledAmplitude(byte[] eightBitByteArray, int nbChannels)
{
int[][] toReturn = new int[nbChannels][eightBitByteArray.length / (2 * nbChannels)];
int index = 0;
for (int audioByte = 0; audioByte < eightBitByteArray.length;)
{
for (int channel = 0; channel < nbChannels; channel++)
{
// Do the byte to sample conversion.
int low = (int) eightBitByteArray[audioByte];
audioByte++;
int high = (int) eightBitByteArray[audioByte];
audioByte++;
int sample = (high << 8) + (low & 0x00ff);
toReturn[channel][index] = sample;
if (audioByte == 0) {
System.out.println("CHANNEL COUNT");
}
}
index++;
}
return toReturn;
}
}
最佳答案
如果这是16位音频,则每隔一个字节将是最低有效的数据。绘制这些字节会使它看起来非常嘈杂(如您上面所述)。
像这样想,在电话簿中,姓氏由LastName,FirstName排序。这类似于为16位音频对两个字节进行排序的方式。假设名称为:
关于java - 为什么WAV文件中的字节数组看起来不像音频波形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45061589/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!