Pratyush101 commited on
Commit
c461f72
·
verified ·
1 Parent(s): 799d74a

Delete pages/10_sendonly_audio.py

Browse files
Files changed (1) hide show
  1. pages/10_sendonly_audio.py +0 -87
pages/10_sendonly_audio.py DELETED
@@ -1,87 +0,0 @@
1
- """A sample to use WebRTC in sendonly mode to transfer audio frames
2
- from the browser to the server and visualize them with matplotlib
3
- and `st.pyplot`."""
4
-
5
- import logging
6
- import queue
7
-
8
- import matplotlib.pyplot as plt
9
- import numpy as np
10
- import pydub
11
- import streamlit as st
12
- from streamlit_webrtc import WebRtcMode, webrtc_streamer
13
-
14
- from sample_utils.turn import get_ice_servers
15
-
16
- logger = logging.getLogger(__name__)
17
-
18
-
19
- webrtc_ctx = webrtc_streamer(
20
- key="sendonly-audio",
21
- mode=WebRtcMode.SENDONLY,
22
- audio_receiver_size=256,
23
- rtc_configuration={"iceServers": get_ice_servers()},
24
- media_stream_constraints={"audio": True},
25
- )
26
-
27
- fig_place = st.empty()
28
-
29
- fig, [ax_time, ax_freq] = plt.subplots(2, 1, gridspec_kw={"top": 1.5, "bottom": 0.2})
30
-
31
- sound_window_len = 5000 # 5s
32
- sound_window_buffer = None
33
- while True:
34
- if webrtc_ctx.audio_receiver:
35
- try:
36
- audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
37
- except queue.Empty:
38
- logger.warning("Queue is empty. Abort.")
39
- break
40
-
41
- sound_chunk = pydub.AudioSegment.empty()
42
- for audio_frame in audio_frames:
43
- sound = pydub.AudioSegment(
44
- data=audio_frame.to_ndarray().tobytes(),
45
- sample_width=audio_frame.format.bytes,
46
- frame_rate=audio_frame.sample_rate,
47
- channels=len(audio_frame.layout.channels),
48
- )
49
- sound_chunk += sound
50
-
51
- if len(sound_chunk) > 0:
52
- if sound_window_buffer is None:
53
- sound_window_buffer = pydub.AudioSegment.silent(
54
- duration=sound_window_len
55
- )
56
-
57
- sound_window_buffer += sound_chunk
58
- if len(sound_window_buffer) > sound_window_len:
59
- sound_window_buffer = sound_window_buffer[-sound_window_len:]
60
-
61
- if sound_window_buffer:
62
- # Ref: https://own-search-and-study.xyz/2017/10/27/python%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E9%9F%B3%E5%A3%B0%E3%83%87%E3%83%BC%E3%82%BF%E3%81%8B%E3%82%89%E3%82%B9%E3%83%9A%E3%82%AF%E3%83%88%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0%E3%82%92%E4%BD%9C/ # noqa
63
- sound_window_buffer = sound_window_buffer.set_channels(1) # Stereo to mono
64
- sample = np.array(sound_window_buffer.get_array_of_samples())
65
-
66
- ax_time.cla()
67
- times = (np.arange(-len(sample), 0)) / sound_window_buffer.frame_rate
68
- ax_time.plot(times, sample)
69
- ax_time.set_xlabel("Time")
70
- ax_time.set_ylabel("Magnitude")
71
-
72
- spec = np.fft.fft(sample)
73
- freq = np.fft.fftfreq(sample.shape[0], 1.0 / sound_chunk.frame_rate)
74
- freq = freq[: int(freq.shape[0] / 2)]
75
- spec = spec[: int(spec.shape[0] / 2)]
76
- spec[0] = spec[0] / 2
77
-
78
- ax_freq.cla()
79
- ax_freq.plot(freq, np.abs(spec))
80
- ax_freq.set_xlabel("Frequency")
81
- ax_freq.set_yscale("log")
82
- ax_freq.set_ylabel("Magnitude")
83
-
84
- fig_place.pyplot(fig)
85
- else:
86
- logger.warning("AudioReciver is not set. Abort.")
87
- break