Class AudioReader

java.lang.Object
com.digimarc.dms.readers.BaseReader
com.digimarc.dms.readers.audio.AudioReader

public class AudioReader extends BaseReader

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
      }
 }
 
  • Method Details

    • Builder

      @NonNull public static AudioReader.Builder Builder(SdkSession session)
      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 internal PayloadCache.
      Overrides:
      clearCache in class BaseReader
    • 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.
    • createDetectors

      protected void createDetectors() throws ReaderException
      Overrides:
      createDetectors in class BaseReader
      Throws:
      ReaderException
    • 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

      @Nullable public ReaderResult processAudioSamples(@NonNull ByteBuffer data) throws Exception
      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 indicator ByteBuffer.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 class BaseReader