first commit
This commit is contained in:
156
src/jbass/HandleWavWriter.java
Normal file
156
src/jbass/HandleWavWriter.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package jbass;
|
||||
import com.sun.jna.Pointer;
|
||||
import com.un4seen.bass.BASS;
|
||||
import com.un4seen.bass.BASSenc;
|
||||
import com.un4seen.bass.BASSenc.ENCODEPROC;
|
||||
|
||||
import anywheresoftware.b4a.BA;
|
||||
|
||||
@BA.ShortName("HandleWavWriter")
|
||||
@BA.Events(values= {
|
||||
"log(msg as string)",
|
||||
"pcmbytes(bb() as byte)"
|
||||
})
|
||||
public class HandleWavWriter implements HandleWriter {
|
||||
private String event = "";
|
||||
private BA ba;
|
||||
private int PCMSize=0; // size of written PCM bytes
|
||||
private boolean inited = false;
|
||||
private String NamaFile = "";
|
||||
private boolean need_log_event = false;
|
||||
private boolean need_pcmbytes_event = false;
|
||||
|
||||
private BASS bass;
|
||||
private BASSenc bassenc;
|
||||
private ENCODEPROC theproc;
|
||||
private int encoder_handle = 0;
|
||||
private Object myobject;
|
||||
|
||||
/**
|
||||
* Initialize WavWriter for B4A and B4J
|
||||
* @param eventname : event name
|
||||
*/
|
||||
public void Initialize(BA bax, String eventname) {
|
||||
NamaFile = "";
|
||||
inited = false;
|
||||
ba = bax;
|
||||
event = eventname;
|
||||
myobject = this;
|
||||
if (ba!=null) {
|
||||
if (!event.isEmpty()) {
|
||||
need_log_event = ba.subExists(event+"_log");
|
||||
need_pcmbytes_event = ba.subExists(event+"_pcmbytes");
|
||||
}
|
||||
}
|
||||
|
||||
bass = new BASS();
|
||||
bassenc = new BASSenc();
|
||||
if (bass.BASS_GetVersion()!=0) {
|
||||
if (bassenc.BASS_Encode_GetVersion()!=0) {
|
||||
inited = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if WavWriter is initialized
|
||||
* @return true if initialized
|
||||
*/
|
||||
public boolean IsInitialized() {
|
||||
return inited;
|
||||
}
|
||||
|
||||
private ENCODEPROC create_encodeproc() {
|
||||
ENCODEPROC newproc = new ENCODEPROC() {
|
||||
|
||||
@Override
|
||||
public void ENCODEPROC(int handle, int channel, Pointer buffer, int length, Pointer user) {
|
||||
if (handle==0) return;
|
||||
if (channel==0) return;
|
||||
if (buffer==null) return;
|
||||
if (length<1) return;
|
||||
PCMSize+=length;
|
||||
raise_pcmbytes_event(buffer.getByteArray(0, length));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return newproc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Record from Other Bass Handle to file
|
||||
* File will be auto written by source handle
|
||||
* If source handle is closed, then WavWriter will also auto closed
|
||||
* @param recordfile : target WAV filename, if empty string, no file will be created
|
||||
* @param handle : other handle
|
||||
* @return true if success
|
||||
*/
|
||||
public boolean CreateFile_from_Handle(String recordfile, int handle) {
|
||||
if (inited) {
|
||||
int flag = bassenc.BASS_ENCODE_PCM | bassenc.BASS_ENCODE_AUTOFREE;
|
||||
PCMSize = 0;
|
||||
theproc = create_encodeproc();
|
||||
encoder_handle = bassenc.BASS_Encode_Start(handle, recordfile, flag, theproc, null);
|
||||
if (encoder_handle==0) {
|
||||
raise_log_event("CreateFile_From_Handle::Encode_Start failed, code="+bass.GetBassErrorString());
|
||||
return false;
|
||||
} else {
|
||||
NamaFile = recordfile;
|
||||
raise_log_event("CreateFile_From_Handle success, target="+NamaFile);
|
||||
return true;
|
||||
}
|
||||
} else return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Current Opened Filename
|
||||
* @return empty string if no file is opened
|
||||
*/
|
||||
public String getCurrentFileName() {
|
||||
return NamaFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get PCM Size, obtained from ENCPROC
|
||||
* @return value in int
|
||||
*/
|
||||
public int getSize() {
|
||||
return PCMSize;
|
||||
}
|
||||
|
||||
// auto close by handler
|
||||
// /**
|
||||
// * Close Wav Writer
|
||||
// */
|
||||
// public void Close_Wav() {
|
||||
//
|
||||
// if (encoder_handle!=0) {
|
||||
// if (!bassenc.BASS_Encode_Stop(encoder_handle)) {
|
||||
// raise_log_event("Close_Wav::Encode_Stop failed, code="+bass.GetBassErrorString());
|
||||
// }
|
||||
// encoder_handle = 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Check if WAV file is opened and ready to write
|
||||
* @return true if opened
|
||||
*/
|
||||
public boolean FileIsOpened() {
|
||||
return (encoder_handle != 0) ? true : false;
|
||||
}
|
||||
|
||||
private void raise_log_event(String msg) {
|
||||
if (need_log_event) ba.raiseEventFromDifferentThread(myobject, null, 0, event+"_log", false, new Object[] {msg});
|
||||
}
|
||||
|
||||
private void raise_pcmbytes_event(byte[] bb) {
|
||||
if (need_pcmbytes_event) ba.raiseEventFromDifferentThread(myobject, null, 0, event+"_pcmbytes", false, new Object[] {bb});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user