mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-13 09:07:27 -05:00
#2569 Support SDRSharp I/Q Baseband Recordings as Recording Tuners and as a user preference for tuner and channel baseband recordings. (#2570)
Co-authored-by: sheirerd <sheirerd@ainfosec.com>
This commit is contained in:
107
src/test/java/io/github/dsheirer/record/wave/IQWriterTest.java
Normal file
107
src/test/java/io/github/dsheirer/record/wave/IQWriterTest.java
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
package io.github.dsheirer.record.wave;
|
||||
|
||||
import io.github.dsheirer.buffer.INativeBuffer;
|
||||
import io.github.dsheirer.sample.ConversionUtils;
|
||||
import io.github.dsheirer.sample.complex.InterleavedComplexSamples;
|
||||
import io.github.dsheirer.source.wave.ComplexWaveSource;
|
||||
import io.github.dsheirer.source.wave.SDRSharpIQRecording;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class IQWriterTest
|
||||
{
|
||||
@TempDir
|
||||
Path mTempDir;
|
||||
|
||||
@Test
|
||||
void writesHeaderlessSamplesReadableByComplexWaveSource() throws Exception
|
||||
{
|
||||
float[] samples = new float[]{0.5f, -0.25f, 0.125f, -1.0f, 0.0f, 0.75f};
|
||||
String name = SDRSharpIQRecording.createFileName(LocalDateTime.now(), "test", "AIRSPY", 851_000_000L, 25_000);
|
||||
Path path = mTempDir.resolve(name);
|
||||
|
||||
try(IQWriter writer = new IQWriter(path))
|
||||
{
|
||||
writer.writeData(ConversionUtils.convertToSigned16BitSamples(new InterleavedComplexSamples(samples, 0)));
|
||||
}
|
||||
|
||||
//Headerless: 3 complex samples x 4 bytes
|
||||
assertEquals(12, Files.size(path));
|
||||
|
||||
assertTrue(ComplexWaveSource.supports(path.toFile()));
|
||||
|
||||
List<float[]> received = new ArrayList<>();
|
||||
|
||||
try(ComplexWaveSource source = new ComplexWaveSource(path.toFile()))
|
||||
{
|
||||
source.setListener((INativeBuffer buffer) -> {
|
||||
Iterator<InterleavedComplexSamples> it = buffer.iteratorInterleaved();
|
||||
while(it.hasNext())
|
||||
{
|
||||
received.add(it.next().samples());
|
||||
}
|
||||
});
|
||||
source.open();
|
||||
assertEquals(25_000d, source.getSampleRate());
|
||||
source.next(3);
|
||||
}
|
||||
|
||||
float[] readBack = received.get(0);
|
||||
assertEquals(samples.length, readBack.length);
|
||||
assertArrayEquals(samples, readBack, 0.0001f);
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesToOverwriteExistingFile() throws Exception
|
||||
{
|
||||
Path path = mTempDir.resolve("existing.iq");
|
||||
Files.write(path, new byte[]{1, 2, 3, 4});
|
||||
assertThrows(java.nio.file.FileAlreadyExistsException.class, () -> new IQWriter(path));
|
||||
assertArrayEquals(new byte[]{1, 2, 3, 4}, Files.readAllBytes(path));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesFullBuffer() throws Exception
|
||||
{
|
||||
Path path = mTempDir.resolve("full.iq");
|
||||
ByteBuffer buffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
|
||||
buffer.position(8); //Writer must rewind
|
||||
|
||||
try(IQWriter writer = new IQWriter(path))
|
||||
{
|
||||
writer.writeData(buffer);
|
||||
}
|
||||
|
||||
assertArrayEquals(new byte[]{1, 2, 3, 4, 5, 6, 7, 8}, Files.readAllBytes(path));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
package io.github.dsheirer.source.wave;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.LocalDateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class SDRSharpIQRecordingTest
|
||||
{
|
||||
private static SDRSharpIQRecording parse(String name)
|
||||
{
|
||||
return SDRSharpIQRecording.parse(new File(name));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesHertzFrequency()
|
||||
{
|
||||
SDRSharpIQRecording r = parse("20261102-071525_toronto_AIRSPY_424461724Hz_10MSps.iq");
|
||||
assertNotNull(r);
|
||||
assertEquals(LocalDateTime.of(2026, 11, 2, 7, 15, 25), r.getStartTime());
|
||||
assertEquals("toronto", r.getLabel());
|
||||
assertEquals("AIRSPY", r.getTunerType());
|
||||
assertEquals(424461724L, r.getCenterFrequency());
|
||||
assertEquals(10_000_000d, r.getSampleRate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesKilohertzAndMegahertzFrequency()
|
||||
{
|
||||
assertEquals(855_125_000L, parse("20261102-072949_toronto_AIRSPY_855125kHz_10MSps.iq").getCenterFrequency());
|
||||
assertEquals(412_000_000L, parse("20261102-071621_toronto_AIRSPY_412MHz_10MSps.iq").getCenterFrequency());
|
||||
assertEquals(860_880_000L, parse("20261102-071621_toronto_AIRSPY_860.88MHz_10MSps.iq").getCenterFrequency());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesOptionalAndUnderscoredLabels()
|
||||
{
|
||||
SDRSharpIQRecording noLabel = parse("20261102-071525_AIRSPY_424461724Hz_10MSps.iq");
|
||||
assertNotNull(noLabel);
|
||||
assertNull(noLabel.getLabel());
|
||||
assertEquals("AIRSPY", noLabel.getTunerType());
|
||||
|
||||
SDRSharpIQRecording underscored = parse("20261102-071525_toronto_north_AIRSPY_424461724Hz_2.5MSps.iq");
|
||||
assertNotNull(underscored);
|
||||
assertEquals("toronto_north", underscored.getLabel());
|
||||
assertEquals("AIRSPY", underscored.getTunerType());
|
||||
assertEquals(2_500_000d, underscored.getSampleRate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnparseableNames()
|
||||
{
|
||||
assertNull(parse("recording.iq"));
|
||||
assertNull(parse("20261102-071525_toronto_AIRSPY_424461724Hz_10MSps.wav"));
|
||||
assertNull(parse("20261302-071525_toronto_AIRSPY_424461724Hz_10MSps.iq")); //Invalid month
|
||||
}
|
||||
|
||||
@Test
|
||||
void formatsSampleRate()
|
||||
{
|
||||
assertEquals("10MSps", SDRSharpIQRecording.formatSampleRate(10_000_000));
|
||||
assertEquals("2.5MSps", SDRSharpIQRecording.formatSampleRate(2_500_000));
|
||||
assertEquals("2.048MSps", SDRSharpIQRecording.formatSampleRate(2_048_000));
|
||||
assertEquals("25kSps", SDRSharpIQRecording.formatSampleRate(25_000));
|
||||
assertEquals("12.5kSps", SDRSharpIQRecording.formatSampleRate(12_500));
|
||||
assertEquals("26.042kSps", SDRSharpIQRecording.formatSampleRate(26_041.666));
|
||||
assertEquals("800Sps", SDRSharpIQRecording.formatSampleRate(800));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createdFileNamesRoundTrip()
|
||||
{
|
||||
LocalDateTime start = LocalDateTime.of(2026, 11, 3, 14, 5, 9);
|
||||
|
||||
String name = SDRSharpIQRecording.createFileName(start, null, "AIRSPY", 851_012_500L, 10_000_000);
|
||||
assertEquals("20261103-140509_AIRSPY_851012500Hz_10MSps.iq", name);
|
||||
|
||||
String channel = SDRSharpIQRecording.createFileName(start, "Site 1_Control", "SDRTRUNK", 851_012_500L, 25_000);
|
||||
assertEquals("20261103-140509_Site 1_Control_SDRTRUNK_851012500Hz_25kSps.iq", channel);
|
||||
SDRSharpIQRecording parsed = parse(channel);
|
||||
assertNotNull(parsed);
|
||||
assertEquals(start, parsed.getStartTime());
|
||||
assertEquals("Site 1_Control", parsed.getLabel());
|
||||
assertEquals("SDRTRUNK", parsed.getTunerType());
|
||||
assertEquals(851_012_500L, parsed.getCenterFrequency());
|
||||
assertEquals(25_000d, parsed.getSampleRate());
|
||||
|
||||
//Underscores in the tuner type would break parsing - they're replaced with hyphens
|
||||
SDRSharpIQRecording hf = parse(SDRSharpIQRecording.createFileName(start, null, "AIRSPY_HF", 7_200_000L, 768_000));
|
||||
assertNotNull(hf);
|
||||
assertEquals("AIRSPY-HF", hf.getTunerType());
|
||||
assertEquals(768_000d, hf.getSampleRate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectsExtension()
|
||||
{
|
||||
assertTrue(SDRSharpIQRecording.hasIQExtension(new File("a.IQ")));
|
||||
assertFalse(SDRSharpIQRecording.hasIQExtension(new File("a.wav")));
|
||||
assertFalse(SDRSharpIQRecording.hasIQExtension(null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user