- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 android studio 项目的 asset 文件夹中有一个名为 abaa.wav
的音频文件。你可以看到图像: 然后,不知道为什么会出现错误 java.io.FileNotFoundException:
从日志中可以看到:
05-22 13:34:56.067 12052-12052/com.ringdroid D/WAVE: EXCEPTION
java.io.FileNotFoundException: abaa.wav
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:327)
at android.content.res.AssetManager.open(AssetManager.java:301)
at com.ringdroid.spectrogram.WaveTools.wavread(WaveTools.java:41)
at com.ringdroid.spectrogram.SpectrogramActivity.onCreate(SpectrogramActivity.java:67)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
at dalvik.system.NativeStart.main(Native Method)
此日志中的代码 fragment com.ringdroid.spectrogram.WaveTools.wavread(WaveTools.java:41)
是=> is2 =mCtx.getAssets().open(path) ;
此日志中的代码 fragment 位于 com.ringdroid.spectrogram.SpectrogramActivity.onCreate(SpectrogramActivity.java:67)
是 => audioBuf = WaveTools.wavread(inputPath, this) ;
这是我的类(class)WaveTools.java:
public class WaveTools {
static byte[] myData = null;
public static byte[] myData2 = null;
static int mySampleRate;
public static float [] wavread(String path, Context mCtx) {
String strThrow = "Error";
InputStream inFile = null;
byte[] tmpLong = new byte[4];
byte[] tmpInt = new byte[2];
long myChunkSize;
long mySubChunk1Size;
int myFormat;
long myChannels;
long myByteRate;
int myBlockAlign;
int myBitsPerSample;
long myDataSize = 0;
float [] buffer = null;
myData = null;
try{
InputStream is2 = null;
is2 =mCtx.getAssets().open(path);
inFile = new DataInputStream(is2);
String chunkID ="" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read();
inFile.read(tmpLong); // read the ChunkSize
myChunkSize = byteArrayToLong(tmpLong);
String format = "" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read();
if (!format.equals("WAVE")){
strThrow="File format is not .wav";
throw new IllegalStateException(strThrow);
}
//Log.d("WAVE","format = "+format);
String subChunk1ID = "" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read();
inFile.read(tmpLong); // read the SubChunk1Size
mySubChunk1Size = byteArrayToLong(tmpLong);
inFile.read(tmpInt); // read the audio format. This should be 1 for PCM
myFormat = byteArrayToInt(tmpInt);
//Log.d("WAVE","myFormat = "+myFormat);
inFile.read(tmpInt); // read the # of channels (1 or 2)
myChannels = byteArrayToInt(tmpInt);
if (myChannels > 1){
strThrow = "File format is not mono";
throw new IllegalStateException(strThrow);
}
inFile.read(tmpLong); // read the samplerate
mySampleRate = (int)byteArrayToLong(tmpLong);
//Log.d("WAVE","channels = "+myChannels);
if (mySampleRate > mySampleRate){
strThrow = "File format is not 8kHz";
throw new IllegalStateException(strThrow);
}
Log.d("WAVE","Fs = "+mySampleRate);
inFile.read(tmpLong); // read the byterate
myByteRate = byteArrayToLong(tmpLong);
inFile.read(tmpInt); // read the blockalign
myBlockAlign = byteArrayToInt(tmpInt);
inFile.read(tmpInt); // read the bitspersample
myBitsPerSample = byteArrayToInt(tmpInt);
String dataChunkID = "" + (char)inFile.read() + (char)inFile.read() + (char)inFile.read() + (char)inFile.read();
inFile.read(tmpLong); // read the size of the data
myDataSize = byteArrayToLong(tmpLong);
// read the data chunk
myData = new byte[(int)myDataSize];
myData2 = new byte[(int)myDataSize];
Short [] shortVal = new Short[(int) myDataSize/2];
ByteBuffer bb = ByteBuffer.allocateDirect(2);
int max = 0;
buffer = new float[(int) myDataSize/2];
bb.order(ByteOrder.LITTLE_ENDIAN);
int count = 0;
for (int i = 0; i<myDataSize;i+=2){
inFile.read(tmpInt);
myData[i]= tmpInt[0];
myData[i+1]= tmpInt[1];
bb.position(0);
bb.put(tmpInt[0]);
bb.put(tmpInt[1]);
buffer[count] = (float) bb.getShort(0);
shortVal[count] = bb.getShort(0);
//Log.d("Audio Read","myFormat = "+shortVal[count]);
if (shortVal[count] > max){
max = shortVal[count];
}else if (-shortVal[count] > max){
max = -shortVal[count];
}
count++;
}
int inc = 0;
ByteBuffer bb2 = ByteBuffer.allocateDirect(2);
bb2.order(ByteOrder.LITTLE_ENDIAN);
for (int i=0; i<((int) myDataSize/2) ;i++){
shortVal[i] = (short) (((int)shortVal[i]*32767)/max);
bb2.putShort(0,shortVal[i]);
myData2[inc] = bb2.get(0);
myData2[inc+1] = bb2.get(1);
inc = inc +2;
}
// close the input stream
inFile.close();
}catch(Exception e){
Log.d("WAVE", "EXCEPTION ",e);
}
return buffer;
}
public static long byteArrayToLong(byte[] b)
{
int start = 0;
int i = 0;
int len = 4;
int cnt = 0;
byte[] tmp = new byte[len];
for (i = start; i < (start + len); i++)
{
tmp[cnt] = b[i];
cnt++;
}
long accum = 0;
i = 0;
for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 )
{
accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
i++;
}
return accum;
}
public static int byteArrayToInt(byte[] b)
{
int start = 0;
int low = b[start] & 0xff;
int high = b[start+1] & 0xff;
return (int)( high << 8 | low );
}
public static byte [] getByteArray(){
return myData2;
}
public static int getFs() {
return mySampleRate;
}
}
这是我的类(class)SpectrogramActivity.java:
public class SpectrogramActivity extends Activity {
float[] buff;
float[] buff_audio;
float[] new_sig;
TextView left;
TextView right;
TextView title;
int tshift = 4; //frame shift in ms
int tlen = 32; //frame length in ms
static float [] audioBuf;
static String inputPath;
// test
float[] array_hat,res=null;
float[] fmag = null;
float[] flogmag = null;
float[] fft_cpx,tmpr,tmpi;
float[] mod_spec =null;
float[] real_mod = null;
float[] imag_mod = null;
double[] real =null;
double[] imag= null;
double[] mag =null;
double[] phase = null;
double[] logmag = null;
static float [][] framed;
static int n, seg_len,n_shift;
static float n_segs;
float [] time_array;
float [] array;
float [] wn;
double[] nmag;
static float [][] spec;
float [] array2;
static float max;
static float min;
static float smax;
static float smin;
static float mux;
static float smux;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetupUI();
// Acquire input audio file
inputPath = "abaa.wav";
try{
audioBuf = WaveTools.wavread(inputPath, this);
}catch(Exception e){
Log.d("SpecGram2","Exception= "+e);
}
/* Calculate Log Spectrogram data, ideally you
* would do this in a worker thread or an
* AsynTask so you don't consume UI resources
*/
String dummy = "test";
new calcSpec().execute(dummy);
}
/**
* Draw layout
*/
private void SetupUI() {
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,
(float) 1.0f);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,
(float) 1.0f);
LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,
(float) 0.1f);
LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,
(float) 1.0f);
LinearLayout main = new LinearLayout(this);
LinearLayout secondary = new LinearLayout(this);
ScrollView scroll = new ScrollView(this);
title = new TextView(this);
left = new TextView(this);
scroll.setLayoutParams(param4);
main.setLayoutParams(param4);
main.setOrientation(LinearLayout.VERTICAL);
secondary.setLayoutParams(param1);
secondary.setOrientation(LinearLayout.HORIZONTAL);
title.setLayoutParams(param3);
left.setLayoutParams(param2);
secondary.addView(left);
scroll.addView(secondary);
main.addView(title);
main.addView(scroll);
setContentView(R.layout.activity_spectrogram);
title.setText("FFT Spectrogram of speech example by DigiPhD");
title.setTextSize(12);
title.setTypeface(null, Typeface.BOLD);
left.setText("Calculating.....\n");
}
/**
* Calculates the spectrogram or log spectrum of the
* audio signal
* @param data
* @param nsegs
* @param nshift
* @param seglen
*/
public void specGram(float [] data, float nsegs, int nshift, int seglen){
spec = new float[seglen][(int)nsegs];
array2 = new float[seglen];
seg_len = seglen;
n_segs = nsegs;
n_shift = nshift;
time_array = new float[data.length];
time_array = data;
framed = new float [seg_len][(int)n_segs];
framed = FrameSig();
minmax(framed,seg_len,(int)n_segs);
meansig((int)n_segs);
array = new float[seg_len*2];
res=new float[seg_len];
fmag = new float[seg_len];
flogmag = new float[seg_len];
mod_spec =new float[seg_len];
real_mod = new float[seg_len];
imag_mod = new float[seg_len];
real = new double[seg_len];
imag= new double[seg_len];
mag = new double[seg_len];
phase = new double[seg_len];
logmag = new double[seg_len];
nmag = new double[seg_len];
for (int i = 0;i<seg_len*2;i++){
array[i] = 0;
}
for (int j=0;j<nsegs; j++){
FFT fft = new FFT(seg_len*2, 8000);
for (int i = 0;i<seg_len;i++){
array[i] = framed [i][j];
}
fft.forward(array);
fft_cpx=fft.getSpectrum();
tmpi = fft.getImaginaryPart();
tmpr = fft.getRealPart();
for(int i=0;i<seg_len;i++)
{
real[i] = (double) tmpr[i];
imag[i] = (double) tmpi[i];
mag[i] = Math.sqrt((real[i]*real[i]) + (imag[i]*imag[i]));
mag[i] = Math.abs(mag[i]/seg_len);
logmag[i] = 20*Math.log10(mag[i]);
phase[i]=Math.atan2(imag[i],real[i]);
/****Reconstruction****/
//real_mod[i] = (float) (mag[i] * Math.cos(phase[i]));
//imag_mod[i] = (float) (mag[i] * Math.sin(phase[i]));
spec[(seg_len-1)-i][j] = (float) logmag[i];
//Log.d("SpecGram","log= "+logmag[i]);
}
}
minmaxspec(spec,seg_len,(int)nsegs);
meanspec((int)nsegs);
//fft.inverse(real_mod,imag_mod,res);
}
/**
* Calculates the mean of the fft magnitude spectrum
* @param nsegs
*/
private void meanspec(int nsegs) {
float sum = 0;
for (int j=1; j<(int)nsegs; j++) {
for (int i = 0;i<seg_len;i++){
sum += spec[i][j];
}
}
sum = sum/(nsegs*seg_len);
mux = sum;
}
/**
* Calculates the min and max of the fft magnitude
* spectrum
* @param spec
* @param seglen
* @param nsegs
* @return
*/
public static float minmaxspec(float[][] spec, int seglen, int nsegs) {
smin = (float) 1e35;
smax = (float) -1e35;
for (int j=1; j<nsegs; j++) {
for (int i = 0;i<seglen;i++){
if (smax < spec[i][j]) {
smax = spec[i][j]; // new maximum
}else if(smin > spec[i][j]) {
smin=spec[i][j]; // new maximum
}
}
}
return smax;
}
/**
* Calculates the min and max value of the framed signal
* @param spec
* @param seglen
* @param nsegs
* @return
*/
public static float minmax(float[][] spec, int seglen, int nsegs) {
min = (float) 1e35;
max = (float) -1e35;
for (int j=1; j<nsegs; j++) {
for (int i = 0;i<seglen;i++){
if (max < spec[i][j]) {
max = spec[i][j]; // new maximum
}else if(min > spec[i][j]) {
min=spec[i][j]; // new maximum
}
}
}
return max;
}
/**
* Calculates the mean of the framed signal
* @param nsegs
*/
private void meansig(int nsegs) {
float sum = 0;
for (int j=1; j<(int)nsegs; j++) {
for (int i = 0;i<seg_len;i++){
sum += framed[i][j];
}
}
sum = sum/(nsegs*seg_len);
smux = sum;
}
/**
* Frames up input audio
* @return
*/
public float[][] FrameSig(){
float [][] temp = new float [seg_len][(int)n_segs];
float [][] frame = new float [seg_len][(int)n_segs];
float padlen = (n_segs-1)*n_shift+seg_len;
Log.d("DEBUG10","padlen = "+padlen);
Log.d("DEBUG10","len = "+array2.length);
wn = hamming(seg_len);
for (int i = 0; i < n_segs;i++){
for (int j = 0;j<seg_len;j++){
temp[j][i] = time_array[i*n_shift+j];//*wn[i];
}
}
for (int i = 0; i < n_segs;i++){ // Windowing
for (int j = 0;j<seg_len;j++){
frame[j][i] = temp[j][i]*wn[j];
}
}
return frame;
}
/**
* Calculates a hamming window to reduce
* spectral leakage
* @param len
* @return
*/
public float[] hamming(int len){
float [] win = new float [len];
for (int i = 0; i<len; i++){
win[i] = (float) (0.54-0.46*Math.cos((2*Math.PI*i)/(len-1)));
}
return win;
}
private class calcSpec extends AsyncTask<String, Integer, String> {
int fs = 0; // Sampling frequency
int nshift = 0;// Initialise frame shift
int nlen = 0;// Initialise frame length
float nsegs = 0 ; //Initialise the total number of frames
@Override
protected String doInBackground(String... params) {
fs = WaveTools.getFs();
nshift = (int) Math.floor(tshift*fs/1000); // frame shift in samples
nlen = (int) Math.floor(tlen*fs/1000); // frame length in samples
Log.d("Spectrogram", "Nilai dari nlen " + nlen + " nilai dari nshift " + nshift);
nsegs = 1+(float) (Math.ceil((audioBuf.length-(nlen))/(nshift)));
specGram(audioBuf,nsegs,nshift,nlen);
return null;
}
@Override
protected void onPostExecute(String result) {
left.setText("");
left.setTextSize(4);
for (int j = 0; j < nsegs ; j++){
for (int i = 0; i < nlen; i++) {
left.append(Integer.toString((int) spec[i][j])+" ");
}
}
}
}}
我的问题是:我的代码有什么问题?这个问题有解决办法吗?
谢谢
最佳答案
通过转到源文件夹然后右键单击来创建 Assets 文件夹
New->Folder->Asset Folder
然后粘贴到该资源文件夹中的文件look at this image
请注意, Assets 文件夹位于主文件夹下
关于java.io.FileNotFoundException : abaa. wav?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462282/
我在 VisualStudio2010 中创建了小的 Windows Forms progs,只是为了业余爱好。释放它们后,我使用 .exe 文件在其他 PC 上运行它们,而无需 进行任何安装。这些
我正在尝试使用AvroParquetWriter将Avro格式的文件转换为 Parquet 文件。我加载架构 val schema:org.apache.Schema = ... getSchema(
我正在尝试使用 Image.IO.Write() 保存图像;我基本上从 here 复制了标准代码使用 lwjgl 截取屏幕截图。我唯一做的就是使用现有目录作为保存路径来初始化文件。 当我尝试保存图像时
错误: E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/
如果这是基本的,我很抱歉,我错过了一些简单的东西。我正在尝试运行下面的代码以遍历文件夹中的文件并将所有以特定字符串开头的文件合并到数据框中。所有文件都放在一个湖中。 file_list=[] path
在我的数据库中,我的手机上的每个条目都有一个图片的 uri。为了在手机上显示它,Listview 有一个 CustomAdapter。现在,我想在 ListView 中显示图片,得到如下错误信息: 0
我的所有节点在压缩期间都抛出 FileNotFoundException。因此,没有一个压缩(自动、手动)可以完成,我的 SSTable 计数现在是单个 CF (CQL3) 的数千个。 nodetoo
我在 java 中读取文件时遇到一些问题: 我的文件是例如: 3,4 2 6 4 1 7 3 8 9 其中第一行 3 和 4 是数组 A 和 B 的长度,然后是每个数组的元素。 我做的 import
我创建了一个程序,其中保存了学生的成绩,我想将这些成绩存储在txt文件中,然后在启动程序时,导入成绩,并在程序完成后导出成绩。我将import和exportTo方法放在单独的文件中,然后在主类中调用这
我怎样才能捕获一个 com.sun.faces.context.FacesFileNotFoundException 在 Java EE 网络应用程序中? 我尝试在我的 web.xml 文件中添加以下
请帮忙,我正在尝试从此谷歌翻译 API URL 获取数据仅当值为 1 个单词时它才有效。如果值为 2,则会出现错误。 我的意思是这个值会起作用: String sourceLang = "auto";
当我尝试使用retrofit2上传图片时,出现此错误 :java.io.FileNotFoundException(No such file or directory). HashMap partMa
try { FileReader fr = new FileReader("C:\\Users\\kevin\\Desktop\\AndroidLibr\\LeagueStats\\a
我尝试使用 Java 将单个文件从源复制到目标,但收到以下错误消息。 java.io.FileNotFoundException:以下是方法 public void copy_single(Strin
类似的问题涉及 C: 上的文件。驱动器,其中对文件路径进行硬编码是可接受的答案。此应用程序是移动应用程序,对文件路径进行硬编码并不实用。 我正在尝试通过扫描仪导入一个文本文件,其中包含一个字符串列表,
我正在修改一个小应用程序以从文件中读取一些数字。到目前为止一切都运行良好,但现在我遇到了一个问题,我不知道如何有效地解决它。如果用户输入了错误的文件名(可能是无意的),JVM 将抛出 FileNotF
我有一个 Web 项目,其中使用以下代码: try { br1 = new BufferedReader(new FileReader("queryWords.txt")); } catch
我尝试使用绝对路径从文件系统读取文件,但由于“FileNotFoundException”而失败,我不知道为什么 File file=new File("E:\\Directory\\File.txt
在我当前的项目中,我遇到了未收到文件未找到异常的问题。我的驱动程序文件将要打开的路径传递给正在构建图书库的构造函数。我正在使用 JFileChooser 来获取路径。在尝试强制错误(输入不存在的文件名
这个问题已经有答案了: Java: Unresolved compilation problem (10 个回答) 已关闭 4 年前。 我已经查看了有关此问题的其他答案,并尝试了他们的建议,但没有成功
我是一名优秀的程序员,十分优秀!