Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

86
LINES

< > BotCompany Repo | #1031900 // Live Audio FFT with more options

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1456K of libraries. Click here for Pure Java version (11937L/65K).

!7

cm LiveFFT > DynImageSurface {
  volatile bool enabled = true;
  switchable bool normalize = false;
  switchable double windowSize = 1; // seconds
  switchable int overlap = 2;
  switchable int frameBits = 10; // frameSize 1024
  
  switchable double preMult = 3.5;
  switchable int brightness = -300;
  switchable int contrast = 187;

  switchable int downSampling = 1;
  
  transient Q q;
  transient L<short[]> buffer = synchroList();
  transient volatile int bufferSize;
  transient int neededSamples;
  transient Clip clip;
  transient short[] fromData;
  transient IF1<Double> colorizer = x -> (brightness+contrast*log1p(abs(preMult*x)))/255;
  
  visualize {
    if (!hasImage()) setImage(
      whiteImage(audio_estimatedFrequencyImageWidth(windowSize), audio_frequencyImageHeight()));
    ret centerAndSouthWithMargins(super.visualizeWithDoubleBuffering(),
      jvstackWithSpacing(
        makeForm(
          "Window Size" := dm_doubleSlider windowSize(0, 2),
          "Frame Bits" := dm_intSlider frameBits(1, 12),
          Downsampling := dm_intSlider downSampling(1, 8),
          Overlap := dm_intSlider overlap(1, 16),
          PreMult := dm_doubleSlider preMult(0, 50),
          Brightness := dm_intSlider brightness(-300, 200),
          Contrast := dm_intSlider contrast(0, 300)),
        jrightalignedline(dm_fieldCheckBox enabled())
      ));
  }
  
  void onNewWindowSize {
    neededSamples = iround(windowSize*44100);
    buffer.clear();
    bufferSize = 0;
  }

  start {
    dm_watchFieldAndNow windowSize(r onNewWindowSize);

    q = dm_startQ();
    dm_addAudioListener(voidfunc(short[] _data) {
      if (!enabled) ret;
      short[] data = _data;
      data = dm_audioInputIsStereo()
        ? mixStereoShortArrayToMono(data)
        : cloneShortArray(data);
      if (normalize) data = normalizeShortArray(data);
      buffer.add(data);
      bufferSize += l(data);
      
      if (bufferSize >= neededSamples) {
        bufferSize -= neededSamples;
        final L<short[]> salvaged = cloneList(buffer);
        buffer.clear();
        if (bufferSize > 0) {
          buffer.add(lastNShorts(bufferSize, last(salvaged)));
          replaceLastElement(salvaged, dropLastNShorts(bufferSize, last(salvaged)));
        }
        
        q.add(r {
          short[] data = concatShortArrays(salvaged);
          data = linearlyShortenArray(data, iround(l(data)/downSampling));
          ShortArrayInputStream stream = new(data);
          stream.bigEndian = true;
          clip = spectro_clipFromMonoInputStream(stream, 1 << frameBits, overlap);
          vmBus_send newAudioFrequencyClip(clip);
          BWImage img = clipToFrequencyImage(clip, colorizer);
          vmBus_send newAudioFrequencyImage(img);
          vmBus_send newAudioFrequencyImageFromData(img, _data);
          setImage(img);
          fromData = _data;
        });
      }
    });
  }  
}

Author comment

Began life as a copy of #1019274

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031900
Snippet name: Live Audio FFT with more options
Eternal ID of this version: #1031900/7
Text MD5: 08846e0c3cce09ec4e13d1cd34c61ea6
Transpilation MD5: bf8430f65d35daec0842356dcd86ce4e
Author: stefan
Category: javax / audio recognition
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-24 07:01:23
Source code size: 2984 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 169 / 752
Version history: 6 change(s)
Referenced in: [show references]