Merge pull request #339 from DSheirer/336-audio-recording-temp-files

336 - Audio recording temporary file name
This commit is contained in:
Denny Sheirer
2018-03-17 14:41:14 +00:00
committed by GitHub
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 * Primary ingest point for audio packets from all decoding channels
*
* @param audioPacket to process * @param audioPacket to process
*/ */
@Override @Override
@@ -115,7 +116,7 @@ public class RecorderManager implements Listener<AudioPacket>
while(!audioPackets.isEmpty()) while(!audioPackets.isEmpty())
{ {
for(AudioPacket audioPacket: audioPackets) for(AudioPacket audioPacket : audioPackets)
{ {
String identifier = audioPacket.getMetadata().getUniqueIdentifier(); String identifier = audioPacket.getMetadata().getUniqueIdentifier();

View File

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

View File

@@ -45,7 +45,7 @@ public class WaveWriter implements AutoCloseable
public static final String DATA_CHUNK_ID = "data"; 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; public static final long MAX_WAVE_SIZE = 2l * (long)Integer.MAX_VALUE;
private AudioFormat mAudioFormat; private AudioFormat mAudioFormat;
@@ -112,7 +112,7 @@ public class WaveWriter implements AutoCloseable
while(Files.exists(mFile)) 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++; version++;
} }
@@ -134,6 +134,35 @@ public class WaveWriter implements AutoCloseable
mFileChannel.force(true); mFileChannel.force(true);
mFileChannel.close(); mFileChannel.close();
mFileChannel = null; 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 @Override
@@ -379,7 +408,7 @@ public class WaveWriter implements AutoCloseable
if(mFileRolloverCounter == 2) if(mFileRolloverCounter == 2)
{ {
filename = filename.replace(".wav", "_2.wav"); filename = filename.replace(".tmp", "_2.tmp");
} }
else else
{ {