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

76
LINES

< > BotCompany Repo | #1019274 // Live Audio FFT [Dyn Module, shows last second]

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

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

!7

cm LiveFFT > DynImageSurface {
  volatile bool enabled = true;
  switchable bool normalize = false;
  switchable double windowSize = 1; // seconds
  
  switchable double preMult = 3.5;
  switchable int brightness = -300;
  switchable int contrast = 187;
  
  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(
          Seconds := dm_doubleSlider windowSize(0, 2),
          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 {
          MultiShortArrayInputStream_resettable stream = new(salvaged);
          stream.bigEndian = true;
          clip = spectro_clipFromMonoInputStream(stream);
          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 #1019263

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, snaazhdonpnp, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1019274
Snippet name: Live Audio FFT [Dyn Module, shows last second]
Eternal ID of this version: #1019274/37
Text MD5: 94dbbc97f1a1a70cbdb861a14ee0f437
Transpilation MD5: faffaa07a84cb96e2d8f1d011b258d59
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 06:51:44
Source code size: 2467 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 573 / 10197
Version history: 36 change(s)
Referenced in: #1025084 - dm_requireLiveAudioFFT
#1031900 - Live Audio FFT with more options