소스 검색

write audio to files

subDesTagesMitExtraKaese 2 년 전
부모
커밋
f26b6a59fb
7개의 변경된 파일228개의 추가작업 그리고 128개의 파일을 삭제
  1. 14 28
      audio.py
  2. 214 100
      beleg.ipynb
  3. BIN
      output/coif4_f2.wav
  4. BIN
      output/r.wav
  5. BIN
      output/stft_f2.wav
  6. BIN
      output/t_f2.wav
  7. BIN
      output/x.wav

+ 14 - 28
audio.py

@@ -1,34 +1,20 @@
 import IPython.display
 import numpy as np
 import json
+from scipy.io.wavfile import write
 
-def Audio(audio: np.ndarray, rate: int):
-    """
-    Use instead of IPython.display.Audio as a workaround for VS Code.
-    `audio` is an array with shape (channels, samples) or just (samples,) for mono.
-    """
+def Audio(audio: np.ndarray, rate: int, name: str):
 
-    if np.ndim(audio) == 1:
-        channels = [audio.tolist()]
-    else:
-        channels = audio.tolist()
+    filename = f"output/{name}.wav"
+    amplitude = np.iinfo(np.int16).max
 
-    return IPython.display.HTML("""
-        <script>
-            if (!window.audioContext) {
-                window.audioContext = new AudioContext();
-                window.playAudio = function(audioChannels, sr) {
-                    const buffer = audioContext.createBuffer(audioChannels.length, audioChannels[0].length, sr);
-                    for (let [channel, data] of audioChannels.entries()) {
-                        buffer.copyToChannel(Float32Array.from(data), channel);
-                    }
-            
-                    const source = audioContext.createBufferSource();
-                    source.buffer = buffer;
-                    source.connect(audioContext.destination);
-                    source.start();
-                }
-            }
-        </script>
-        <button onclick="playAudio(%s, %s)" style="padding: 10px">Play</button>
-    """ % (json.dumps(channels), rate))
+    write(filename, rate, (audio*amplitude).astype(np.int16))
+
+    return IPython.display.HTML(f"""
+        <audio controls>
+            <source src="{filename}" type="audio/wav">
+            Your browser does not support the audio element.
+        </audio>
+        <br/>
+        <a href="{filename}">{filename}</a>
+    """)

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 214 - 100
beleg.ipynb


BIN
output/coif4_f2.wav


BIN
output/r.wav


BIN
output/stft_f2.wav


BIN
output/t_f2.wav


BIN
output/x.wav