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).

1  
!7
2  
3  
cm LiveFFT > DynImageSurface {
4  
  volatile bool enabled = true;
5  
  switchable bool normalize = false;
6  
  switchable double windowSize = 1; // seconds
7  
  
8  
  switchable double preMult = 3.5;
9  
  switchable int brightness = -300;
10  
  switchable int contrast = 187;
11  
  
12  
  transient Q q;
13  
  transient L<short[]> buffer = synchroList();
14  
  transient volatile int bufferSize;
15  
  transient int neededSamples;
16  
  transient Clip clip;
17  
  transient short[] fromData;
18  
  transient IF1<Double> colorizer = x -> (brightness+contrast*log1p(abs(preMult*x)))/255;
19  
  
20  
  visualize {
21  
    if (!hasImage()) setImage(
22  
      whiteImage(audio_estimatedFrequencyImageWidth(windowSize), audio_frequencyImageHeight()));
23  
    ret centerAndSouthWithMargins(super.visualizeWithDoubleBuffering(),
24  
      jvstackWithSpacing(
25  
        makeForm(
26  
          Seconds := dm_doubleSlider windowSize(0, 2),
27  
          PreMult := dm_doubleSlider preMult(0, 50),
28  
          Brightness := dm_intSlider brightness(-300, 200),
29  
          Contrast := dm_intSlider contrast(0, 300)),
30  
        jrightalignedline(dm_fieldCheckBox enabled())
31  
      ));
32  
  }
33  
  
34  
  void onNewWindowSize {
35  
    neededSamples = iround(windowSize*44100);
36  
    buffer.clear();
37  
    bufferSize = 0;
38  
  }
39  
40  
  start {
41  
    dm_watchFieldAndNow windowSize(r onNewWindowSize);
42  
43  
    q = dm_startQ();
44  
    dm_addAudioListener(voidfunc(short[] _data) {
45  
      if (!enabled) ret;
46  
      short[] data = _data;
47  
      data = dm_audioInputIsStereo()
48  
        ? mixStereoShortArrayToMono(data)
49  
        : cloneShortArray(data);
50  
      if (normalize) data = normalizeShortArray(data);
51  
      buffer.add(data);
52  
      bufferSize += l(data);
53  
      
54  
      if (bufferSize >= neededSamples) {
55  
        bufferSize -= neededSamples;
56  
        final L<short[]> salvaged = cloneList(buffer);
57  
        buffer.clear();
58  
        if (bufferSize > 0) {
59  
          buffer.add(lastNShorts(bufferSize, last(salvaged)));
60  
          replaceLastElement(salvaged, dropLastNShorts(bufferSize, last(salvaged)));
61  
        }
62  
        q.add(r {
63  
          MultiShortArrayInputStream_resettable stream = new(salvaged);
64  
          stream.bigEndian = true;
65  
          clip = spectro_clipFromMonoInputStream(stream);
66  
          vmBus_send newAudioFrequencyClip(clip);
67  
          BWImage img = clipToFrequencyImage(clip, colorizer);
68  
          vmBus_send newAudioFrequencyImage(img);
69  
          vmBus_send newAudioFrequencyImageFromData(img, _data);
70  
          setImage(img);
71  
          fromData = _data;
72  
        });
73  
      }
74  
    });
75  
  }  
76  
}

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: 572 / 10197
Version history: 36 change(s)
Referenced in: [show references]