Reputation: 302
I have an audio file with the name abaa.wav
in assets folder in android studio project. You can see the image:
then, I do not know why the error appears java.io.FileNotFoundException:
you can see from the log:
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)
The code snippet From this log com.ringdroid.spectrogram.WaveTools.wavread(WaveTools.java:41)
is => is2 =mCtx.getAssets().open(path);
and the code snippet from this log at com.ringdroid.spectrogram.SpectrogramActivity.onCreate(SpectrogramActivity.java:67)
is => audioBuf = WaveTools.wavread(inputPath, this);
This is my 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;
}
}
This is my 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])+" ");
}
}
}
}}
my question is: what's wrong on my code? is there a solution for this problem?
Thanks
Upvotes: 0
Views: 294
Reputation: 199
create asset folder by go to your source folder then right click
New->Folder->Asset Folder
then paste to file in that asset folder look at this image
note that asset folder comes under main folder
Upvotes: 1