Resolves #336. Updates audio recorder to use a *.tmp file extension

while the file is open/in-use and change the file extension to *.wav
when the recording is closed.
This commit is contained in:
Denny
2018-03-17 10:39:35 -04:00
parent 5dd1664844
commit 78bb5ffe43
3 changed files with 35 additions and 5 deletions

View File

@@ -93,6 +93,7 @@ public class RecorderManager implements Listener<AudioPacket>
/**
* Primary ingest point for audio packets from all decoding channels
*
* @param audioPacket to process
*/
@Override
@@ -115,7 +116,7 @@ public class RecorderManager implements Listener<AudioPacket>
while(!audioPackets.isEmpty())
{
for(AudioPacket audioPacket: audioPackets)
for(AudioPacket audioPacket : audioPackets)
{
String identifier = audioPacket.getMetadata().getUniqueIdentifier();

View File

@@ -104,7 +104,7 @@ public class AudioPacketWaveRecorder extends Module implements Listener<AudioPac
sb.append(mFilePrefix);
sb.append("_");
sb.append(TimeStamp.getLongTimeStamp("_"));
sb.append(".wav");
sb.append(".tmp");
mFile = Paths.get(sb.toString());

View File

@@ -45,7 +45,7 @@ public class WaveWriter implements AutoCloseable
public static final String DATA_CHUNK_ID = "data";
private static final Pattern FILENAME_PATTERN = Pattern.compile("(.*_)(\\d+)(\\.wav)");
private static final Pattern FILENAME_PATTERN = Pattern.compile("(.*_)(\\d+)(\\.tmp)");
public static final long MAX_WAVE_SIZE = 2l * (long)Integer.MAX_VALUE;
private AudioFormat mAudioFormat;
@@ -112,7 +112,7 @@ public class WaveWriter implements AutoCloseable
while(Files.exists(mFile))
{
mFile = Paths.get(mFile.toFile().getAbsolutePath().replace(".wav", "_" + version + ".wav"));
mFile = Paths.get(mFile.toFile().getAbsolutePath().replace(".tmp", "_" + version + ".tmp"));
version++;
}
@@ -134,6 +134,35 @@ public class WaveWriter implements AutoCloseable
mFileChannel.force(true);
mFileChannel.close();
mFileChannel = null;
rename();
}
/**
* Renames the file from *.tmp to *.wav after file has been closed.
* @throws IOException
*/
private void rename() throws IOException
{
if(mFile != null && Files.exists(mFile))
{
String renamed = mFile.getFileName().toString();
renamed = renamed.replace(".tmp", ".wav");
Path renamedPath = mFile.resolveSibling(renamed);
int suffix = 1;
while(Files.exists(renamedPath))
{
String renamedWithSuffix = mFile.getFileName().toString();
renamedWithSuffix = renamedWithSuffix.replace(".tmp", "_" + suffix + ".wav");
Path renamedWithSuffixPath = mFile.resolveSibling(renamedWithSuffix);
renamedPath = mFile.resolveSibling(renamedWithSuffixPath);
suffix++;
}
Files.move(mFile, renamedPath);
}
}
@Override
@@ -379,7 +408,7 @@ public class WaveWriter implements AutoCloseable
if(mFileRolloverCounter == 2)
{
filename = filename.replace(".wav", "_2.wav");
filename = filename.replace(".tmp", "_2.tmp");
}
else
{