Merge pull request #60 from PAMGuard/main

Correct sorting of input sound files
This commit is contained in:
Douglas Gillespie 2024-01-26 10:02:00 +00:00 committed by GitHub
commit dacfab07f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import javax.sound.sampled.UnsupportedAudioFileException;
import org.codehaus.plexus.util.FileUtils;
import Acquisition.offlineFuncs.AquisitionLoadPoint;
import PamDetection.RawDataUnit;
import PamguardMVC.PamConstants;
//import PamUtils.CPUMonitor;
import PamguardMVC.PamDataBlock;
import PamguardMVC.dataOffline.OfflineDataLoadInfo;
@ -47,6 +48,8 @@ public class WavAudioFile implements PamAudioFileLoader {
* Get the file extensions associated with loading these data.
*/
protected ArrayList<String> fileExtensions;
private double[] channelBackground = new double[PamConstants.MAX_CHANNELS];
public WavAudioFile() {
fileExtensions = new ArrayList<String>(Arrays.asList(new String[]{".wav", ".aif", ".aiff"}));
@ -190,6 +193,9 @@ public class WavAudioFile implements PamAudioFileLoader {
newDataUnit = new RawDataUnit(ms, 1 << ichan, totalSamples, newSamples);
newDataUnit.setFileSamples(totalSamples + skipped / frameSize); //set the number samples into the wav file.
removeDCComponent(doubleData[ichan], ichan, audioFormat);
newDataUnit.setRawData(doubleData[ichan], true);
//System.out.println("New wav data: " + PamCalendar.formatDateTime(newDataUnit.getTimeMilliseconds()));
@ -211,6 +217,26 @@ public class WavAudioFile implements PamAudioFileLoader {
}
private void removeDCComponent(double[] ds, int channel, AudioFormat audioFormat) {
/*
* do a simple background subtraction with about a 1s time constant.
* If the background is currently zero initialise it to the mean data value.
*/
double alpha = 1./audioFormat.getSampleRate();
double alpha_1 = 1.-alpha;
double bg = channelBackground[channel];
if (bg == 0.) {
for (int i = 0; i < ds.length; i++) {
bg += ds[i];
}
bg /= ds.length;
}
for (int i = 0; i < ds.length; i++) {
ds[i] -= bg;
bg = bg*alpha_1 + ds[i]*alpha;
}
}
/**
* Open a sound file.
* @param soundFile

View File

@ -228,6 +228,7 @@ public abstract class FileListWorker<T extends File> implements PamWorkWrapper<F
@Override
public final void taskFinished(FileListData<T> result) {
oldFileList = result;
sortFiles(result);
fileListUser.newFileList(result);
}