Package com.digimarc.dms.readers.audio
Class AudioReader
java.lang.Object
com.digimarc.dms.readers.BaseReader
com.digimarc.dms.readers.audio.AudioReader
This class provides synchronous audio reading functionality. When a block of audio samples is passed in, the reader will process the entire block before returning. The following code demonstrates how to load an audio clip from the file system and pipe its data into the reader:
audioReader = AudioReader.Builder()
.setSymbologies(BaseReader.All_Audio_Readers)
.setChannelCount(1) // Only mono input is supported
.setSampleRate(16000) // Sample rate must be 16kHz
.build();
// Open an audio file
inputStream = getAssets().open("myAudioFile.wav");
// Process byte array in chunks
int bytesRead = 0;
byte[] buffer = new byte[4096];
while ((bytesRead = inputStream.read(buffer)) != -1) {
// Pass the data and number of samples to the reader
ReaderResult result = audioReader.processAudioSamples(buffer, bytesRead/2);
if (result != null && result.getPayloads() != null) {
// Handle result
}
}
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.digimarc.dms.readers.BaseReader
BaseReader.AudioSymbology, BaseReader.ImageDetectionType, BaseReader.ImageSymbology, BaseReader.ReaderError, BaseReader.ResultType, BaseReader.Symbology, BaseReader.UndefinedSymbology
-
Field Summary
Fields inherited from class com.digimarc.dms.readers.BaseReader
All_Audio_Readers, All_Barcode_1D_Readers, All_Barcode_Readers, All_Image_Readers
-
Method Summary
Modifier and TypeMethodDescriptionstatic AudioReader.Builder
Builder
(SdkSession session) Get Builder object for creating an AudioReader objectvoid
This method clears the internalPayloadCache
.int
Get the configured number of audio channels.int
Get the configured audio sample rate.processAudioSamples
(byte[] data, int samples) Synchronously process a buffer of audio data and return a list of read results when completed.Synchronously process a buffer of audio data and return a list of read results when completed.void
release()
stop and free all audio resourcesMethods inherited from class com.digimarc.dms.readers.BaseReader
buildSymbologyMask, getReaderOptions, getSymbologies, setAllowPerformanceScheduling, setReaderOptions, setSymbologies
-
Method Details
-
Builder
Get Builder object for creating an AudioReader object- Parameters:
session
- SdkSession object initialized with a valid license key.- Returns:
- Builder
-
clearCache
public void clearCache()Description copied from class:BaseReader
This method clears the internalPayloadCache
.- Overrides:
clearCache
in classBaseReader
-
getSampleRate
public int getSampleRate()Get the configured audio sample rate.- Returns:
- Returns the audio sample rate this reader is configured for.
-
getNumChannels
public int getNumChannels()Get the configured number of audio channels.- Returns:
- Returns the number of audio channels this reader is configured for.
-
processAudioSamples
@Nullable public ReaderResult processAudioSamples(@NonNull byte[] data, int samples) throws ReaderException Synchronously process a buffer of audio data and return a list of read results when completed.- Parameters:
data
- Byte array of audio samples.samples
- The number of samples in the array. Note that this is the number of 16-bit samples rather than bytes (i.e. # samples = 1/2 # bytes).- Returns:
ReaderResult
or null if no items are decoded.- Throws:
ReaderException
- Throws an exception if an error occurs in processing. This may occur if the samples don't match the size of the input data or if the audio configuration parameters don't match the data buffer.
-
processAudioSamples
Synchronously process a buffer of audio data and return a list of read results when completed.- Parameters:
data
- Android ByteBuffer of audio data. The reader determines how much data is available by referring to the buffer's position indicatorByteBuffer.position(int)
. The AudioHelper class does this automatically, but if you are writing your own audio capture code make sure that the position is set to the first byte beyond the end of the audio data.- Returns:
ReaderResult
or null if no items are decoded.- Throws:
ReaderException
- Throws an exception if an error occurs in processing. This may occur if the samples don't match the size of the input data or if the audio configuration parameters don't match the data buffer.Exception
-
release
public void release()stop and free all audio resources- Overrides:
release
in classBaseReader
-