171 lines
4.4 KiB
Java
171 lines
4.4 KiB
Java
package AndroidRelated;
|
|
|
|
import java.io.IOException;
|
|
|
|
import com.sun.jna.Memory;
|
|
import com.sun.jna.Pointer;
|
|
import com.un4seen.bass.BASS;
|
|
import com.un4seen.bass.BASS.bassconstant;
|
|
|
|
import anywheresoftware.b4a.BA;
|
|
import anywheresoftware.b4a.objects.streams.File;
|
|
|
|
@BA.ShortName("AudioFileInformationV2")
|
|
public class AudioFileInformationV2 {
|
|
private final String filename;
|
|
private int handle=0;
|
|
private int size=-1;
|
|
private double seconds=-1;
|
|
private boolean found = false;
|
|
private boolean valid=false;
|
|
private final int _samplingrate;
|
|
private final int _initflag = bassconstant.BASS_DEVICE_16BITS | bassconstant.BASS_DEVICE_MONO;
|
|
private final int _loadflag = bassconstant.BASS_STREAM_DECODE | bassconstant.BASS_SAMPLE_MONO;
|
|
private final int KB_threshold = 1024;
|
|
private final int MB_threshold = 1024 * 1024;
|
|
private final int GB_threshold = 1024 * 1024 * 1024;
|
|
private final int minute_threshold = 60;
|
|
private final int hour_threshold = 60 * 60;
|
|
private BASS bass;
|
|
|
|
|
|
public AudioFileInformationV2() {
|
|
filename ="";
|
|
_samplingrate=44100;
|
|
}
|
|
|
|
public AudioFileInformationV2(BASS _bass,String path, int samplingrate) {
|
|
bass = _bass;
|
|
filename = path;
|
|
this._samplingrate = samplingrate;
|
|
|
|
try {
|
|
found = File.Exists("", path);
|
|
} catch (IOException e) {
|
|
BA.Log("Unable to check existanceo of "+path+", Msg : "+e.getMessage());
|
|
return;
|
|
}
|
|
|
|
|
|
if (bass instanceof BASS) {
|
|
if (bass.BASS_GetVersion()!=0) {
|
|
bass.BASS_Init(0, _samplingrate, _initflag);
|
|
handle = bass.BASS_StreamCreateFile(false, filename, 0, 0, _loadflag);
|
|
if (handle!=0) {
|
|
size = (int)bass.BASS_ChannelGetLength(handle, bassconstant.BASS_POS_BYTE);
|
|
if (size>-1) {
|
|
seconds = bass.BASS_ChannelBytes2Seconds(handle, size);
|
|
if (seconds>-1) {
|
|
valid = true;
|
|
}
|
|
}
|
|
bass.BASS_StreamFree(handle);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/***
|
|
* Check if file is found
|
|
* @return true if found
|
|
*/
|
|
public boolean isfound() {
|
|
return found;
|
|
}
|
|
|
|
/**
|
|
* Check if file is valid
|
|
* @return true if valid
|
|
*/
|
|
public boolean isvalid() {
|
|
return valid;
|
|
}
|
|
|
|
/**
|
|
* Size in bytes
|
|
* @return value in bytes
|
|
*/
|
|
public int size_in_bytes() {
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* Return size in String
|
|
* if invalid, return N/A
|
|
* if < Kilobytes, return [X] B
|
|
* if < Megabytes, return [X] KB
|
|
* if < Gigabytes, return [X] MB
|
|
* else return [X] GB
|
|
* @return value in string
|
|
*/
|
|
public String size_in_String() {
|
|
if (size<0) {
|
|
return "N/A";
|
|
} else if (size<KB_threshold) {
|
|
return String.format("%d B", size);
|
|
} else if (size < MB_threshold) {
|
|
return String.format("%.1f KB" , (float)size / KB_threshold);
|
|
} else if (size < GB_threshold) {
|
|
return String.format("%.1f MB" , (float)size / MB_threshold);
|
|
} else {
|
|
return String.format("%.1f GB" , (float)size / GB_threshold);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return length in seconds
|
|
* if invalid, return less than 0
|
|
* @return value in seconds
|
|
*/
|
|
public double length_in_seconds() {
|
|
return seconds;
|
|
}
|
|
|
|
/**
|
|
* Return length in string
|
|
* if invalid, return N/A
|
|
* if < 60 second, return [X]
|
|
* if < 3600 seconds, return [X] M [Y] S
|
|
* else return [X] H [Y] M [Z] S
|
|
* @return String
|
|
*/
|
|
public String length_in_String() {
|
|
if (seconds<0) {
|
|
return "N/A";
|
|
} else if (seconds < minute_threshold) {
|
|
return String.format("%d", seconds);
|
|
} else if (seconds < hour_threshold) {
|
|
int _minute = (int)(seconds / minute_threshold);
|
|
int _second = (int)(seconds % minute_threshold);
|
|
return String.format("%d M %d S", _minute, _second);
|
|
} else {
|
|
int _hour = (int) (seconds / hour_threshold);
|
|
int remainder = (int)(seconds - (_hour * hour_threshold));
|
|
int _minute = (int) (remainder / minute_threshold);
|
|
int _second = (int)(remainder % minute_threshold);
|
|
return String.format("%d H %d M %d S", _hour, _minute, _second);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return array of bytes for PCM data
|
|
* @return null if invalid
|
|
*/
|
|
public byte[] GetBytes() {
|
|
if (valid) {
|
|
if (size>0) {
|
|
handle = bass.BASS_StreamCreateFile(false, filename, 0, 0, _loadflag);
|
|
if (handle!=0) {
|
|
Pointer pp = new Memory(size);
|
|
int readsize = bass.BASS_ChannelGetData(handle, pp, size);
|
|
if (readsize>0) {
|
|
return pp.getByteArray(0, readsize);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|