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

133
LINES

< > BotCompany Repo | #1033273 // ContinuousOscillators_TestAudioRestoration_v2

JavaX fragment (include) [tags: use-pretranspiled]

Uses 11894K of libraries. Click here for Pure Java version (20559L/135K).

1  
sclass ContinuousOscillators_TestAudioRestoration_v2 {
2  
  S audioURL = "https://mouth.gazelle.rocks/beaHTML/15/5502%2Frecognize+-+61351880347bd84b28c7f42d779cde94.mp3";
3  
4  
  short[] inputSamples;
5  
  double inputSampleRate = 48000;
6  
7  
  settable bool separateFrequencies;
8  
  settable bool cumulative;
9  
  settable bool subtractive;
10  
  settable bool separateRealAndImag;
11  
  settable bool playRestored;
12  
  settable bool showImage;
13  
  settable bool printAllIntensities;
14  
  bool useInputSampleRate = true;
15  
  double silence = 0.2;
16  
  double minFreq = 0, maxFreq = 48000;
17  
  int useEveryNthOscillator = 1;
18  
  
19  
  short[] resampled;
20  
  new L<double[]> audioOutput;
21  
  File wavOut;
22  
  
23  
  new ContinuousOscillators_v2 co;
24  
25  
  void loadAudioURL {
26  
    File mp3 = loadBinaryPageOnce(audioURL);
27  
    loadMP3(mp3);
28  
  }
29  
  
30  
  void loadMP3(File mp3) {
31  
    printFileInfo(+mp3);
32  
    File wav = mp3ToWAVUnlessExists(mp3, replaceExtension(mp3, ".mp3", ".wav"));
33  
    loadWAV(wav);
34  
  }
35  
   
36  
  void loadWAV(File wav) {
37  
    //playWAV(wav);
38  
    printFileInfo(+wav);
39  
    wavOut = appendToBaseFileName(wav, ".restored");
40  
    
41  
    temp WAVDecoder decoder = new(wav);
42  
    print(sampleRate := decoder.sampleRate);
43  
    int n = decoder.totalValues();
44  
    inputSamples = decodeWAVToMonoSamples(decoder, n);
45  
    inputSampleRate = decoder.sampleRate;
46  
  }
47  
  
48  
  // assumes soundSource is using the default inputSampleRate
49  
  void setAudio(double seconds, VF1<double[]> soundSource) {
50  
    inputSamples = concatShortArrays(soundSourceToShortArrays(seconds, soundSource, 1));
51  
    print(inputSamples := sfu(takeFirst(10, inputSamples));
52  
  }
53  
  
54  
  run {
55  
    if (useInputSampleRate) co.sampleRate = inputSampleRate;
56  
    resampled = convertSampleRate_shortArray_simple(inputSampleRate, co.sampleRate, inputSamples);
57  
    print(samples := l(resampled));
58  
    
59  
    co.setAudio(new StaticAudioSample(ll(resampled), 1, co.sampleRate));
60  
61  
    if (subtractive) co.onEscalating(o -> {
62  
      print("Escalating " + o);
63  
      var data = co.subtractedAudio.toDoubleArray();
64  
      audioOutput.add(data);
65  
      print("Got samples: " + l(data));
66  
    });
67  
68  
    // START CALCULATION
69  
    co.stepTo(co.audio.bounds());
70  
    
71  
    print(coverageByFreq := co.coverageByFreq());
72  
    
73  
    restoreAudio();
74  
    double[] audio = concatDoubleArrays(audioOutput);
75  
    double[] audioNorm = normalizeDoubles(audio, 32767);
76  
    
77  
    if (wavOut != null) {
78  
      shortArrayToMonoWAVE(doubleToShortArray_iround(audioNorm), wavOut, co.sampleRate);
79  
      printFileInfo(+wavOut);
80  
      if (playRestored) playWAV(wavOut);
81  
    }
82  
    
83  
    print(audioNorm := sfu(takeFirst(10, audioNorm));
84  
    
85  
    double pixelsPerSecond = 100;
86  
    if (showImage)
87  
      dm_showPixelatedImage(co.image(co.audio.bounds(), pixelsPerSecond));
88  
  }
89  
  
90  
  void restoreAudio {
91  
    L<ContinuousOscillators_v2.Oscillator> oscillators = new L;
92  
    //double ratio = 2; // octave
93  
    int nOscillators = l(co.oscillators);
94  
    var lOsc = asList(co.oscillators);
95  
    
96  
    for i to nOscillators: {
97  
      if ((i % useEveryNthOscillator) == 0)
98  
        oscillators.add(lOsc.get(i));
99  
    }
100  
    
101  
    oscillators = filter(oscillators, o -> between(o.frequency()!, minFreq, maxFreq));
102  
    
103  
    print("Using " + nFrequencies(oscillators) + ": " + map(oscillators, o -> formatDouble(o.frequency()!, 1));
104  
    
105  
    if (separateFrequencies) {
106  
      wavOut = appendToBaseFileName(wavOut, ".separate");
107  
      double[] cum = null;
108  
      
109  
      for (o : oscillators) {
110  
        if (printAllIntensities) pnl(map(o.intensities, (k, v) ->
111  
          o.frequency() + " " + formatDouble(k, 1) + " = " + renderComplexWithAngle(div(v.first(), l(k)*32767))));
112  
        //co.toAudio_shouldPrint = (i, n) -> i < 30 || i >= n-10;
113  
        for (xx : separateRealAndImag ? ll(1, 2) : ll(0)) {
114  
          co.realOnly = (xx & 1) != 0;
115  
          co.imagOnly = (xx & 2) != 0;
116  
          double[] newAudio = co.toAudio(ifloor(co.audio.bounds()), ll(o));
117  
          /*if (subtractive) {
118  
            audioOutput.add(co.subtractedAudio.toDoubleArray());
119  
          } else*/ {
120  
            if (cumulative) cum = newAudio = doubleAdd(cum, newAudio);
121  
            audioOutput.add(newAudio); 
122  
            audioOutput.add(repDouble(iround(co.sampleRate*silence), 0));
123  
          }
124  
          
125  
          co.toAudio_shouldPrint = null;
126  
          co.realOnly = co.imagOnly = false;
127  
        }
128  
      }
129  
    }
130  
      
131  
    audioOutput.add(co.toAudio(ifloor(co.audio.bounds()), oscillators));
132  
  }
133  
}

Author comment

Began life as a copy of #1033253

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033273
Snippet name: ContinuousOscillators_TestAudioRestoration_v2
Eternal ID of this version: #1033273/16
Text MD5: 46f245a9b45cd191fb4d20f1b01a71ff
Transpilation MD5: d68b0626a7fbbd6ba251539274254082
Author: stefan
Category: javax / speech
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-18 08:50:13
Source code size: 4515 bytes / 133 lines
Pitched / IR pitched: No / No
Views / Downloads: 113 / 224
Version history: 15 change(s)
Referenced in: [show references]