我正在尝试使用Android将wav文件读入数组。为了验证结果,我使用Matlab读取了相同的wav文件。问题是价值观不同。在解决此问题时,我们非常感谢您的帮助。 请在下面找到Matlab和Android代码以及相关结果:
fName = 'C:\Users\me\Desktop\audioText.txt';
fid = fopen(fName,'w');
dlmwrite(fName,y_sub,'-append','delimiter','\t','newline','pc');
0.00097656 0.00045776 0.0010681 0.00073242 0.00054932 -0.00064087 0.0010376 -0.00027466 -0.00036621 -9.1553e-05 0.00015259 0.0021362 -0.00024414 -3.0518e-05 -0.00021362
String filePath;
private static DataOutputStream fout;
ByteArrayOutputStream out;
BufferedInputStream in;
filePath = "mnt/sdcard/audio.wav";
out = new ByteArrayOutputStream();
try {
in = new BufferedInputStream(new FileInputStream(filePath));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int read;
byte[] buff = new byte[2000000];
try {
while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] audioBytes = out.toByteArray();
}
82,73,70,70,92,108,40,0,07,65,86,69,102,109
谢谢,
答案 0 :(得分:0)
在Android中,您正在读取文件标题,而不是声音样本的实际值。您在Android中的值是ASCII
RIFF \ l(WAVEfm
在Matlab中我不确定你在做什么......看起来你在写,而不是在读文件。
答案 1 :(得分:0)
dir
命令在这里非常有用。它既可以显示目录的全部内容,也可以指定一个glob来返回一个文件子集,例如: dir('*.wav')
。这将返回包含name
,date
,bytes
,isdir
等文件信息的struct-array。
要开始使用,请尝试以下操作:
filelist = dir('*.wav');
for file = filelist
fprintf('Processing %s\n', file.name);
fid = fopen(file.name);
% Do something here with your file.
fclose(fid);
end
如果每个文件必须存储处理结果, 我经常使用以下模式。我通常预先分配一个数组,一个struct数组或 与文件列表大小相同的单元格数组。然后我使用整数索引进行迭代 在文件列表中,我也可以使用它来编写输出。如果信息要 存储是同构的(例如,每个文件一个标量),使用数组或结构数组。 但是,如果信息因文件而异(例如不同大小的矢量或矩阵),请使用单元格数组。
使用普通数组的示例:
filelist = dir('*.wav');
% Pre-allocate an array to store some per-file information.
result = zeros(size(filelist));
for index = 1 : length(filelist)
fprintf('Processing %s\n', filelist(index).name);
% Read the sample rate Fs and store it.
[y, Fs] = wavread(filelist(index).name);
result(index) = Fs;
end
% result(1) .. result(N) contain the sample rates of each file.
使用单元格数组的示例:
filelist = dir('*.wav');
% Pre-allocate a cell array to store some per-file information.
result = cell(size(filelist));
for index = 1 : length(filelist)
fprintf('Processing %s\n', filelist(index).name);
% Read the data of the WAV file and store it.
y = wavread(filelist(index).name);
result{index} = y;
end
% result{1} .. result{N} contain the data of the WAV files.
答案 2 :(得分:0)
我不确定究竟是什么问题,但是当我使用以下代码时,我得到了正确的读数:
File filein = new File(filePath, "audio.wav");
try
{
// Open the wav file specified as the first argument
WavFile wavFile = WavFile.openWavFile(filein);
// Display information about the wav file
wavFile.display();
// Get the number of audio channels in the wav file
int numChannels = wavFile.getNumChannels();
// Create a buffer of 100 frames
double[] buffer = new double[20000 * numChannels];
int framesRead;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
do
{
// Read frames into buffer
framesRead = wavFile.readFrames(buffer, 20000);
// Loop through frames and look for minimum and maximum value
for (int s=0 ; s<framesRead * numChannels ; s++)
{
if (buffer[s] > max) max = buffer[s];
if (buffer[s] < min) min = buffer[s];
}
}
while (framesRead != 0);
// Close the wavFile
wavFile.close();
// Output the minimum and maximum value
System.out.printf("Min: %f, Max: %f\n", min, max);
}
catch (Exception e)
{
System.err.println(e);
}