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

121
LINES

< > BotCompany Repo | #1033253 // ContinuousOscillators_TestAudioRestoration

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

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

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

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033253
Snippet name: ContinuousOscillators_TestAudioRestoration
Eternal ID of this version: #1033253/39
Text MD5: 514b1ff4f197cde304de2a5ec297fa59
Transpilation MD5: a4b4f9b4e990eef1f76219cd0d37ea6d
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 03:26:18
Source code size: 4203 bytes / 121 lines
Pitched / IR pitched: No / No
Views / Downloads: 158 / 308
Version history: 38 change(s)
Referenced in: [show references]