Alina Lozovskaya commited on
Commit
ce72bd9
·
1 Parent(s): f6c84a9

Remove AUDIO_OUT and AUDIO_IN from gstreamer.py

Browse files
src/reachy_mini_conversation_demo/gstreamer.py CHANGED
@@ -11,7 +11,7 @@ from gi.repository import GLib, Gst, GstApp
11
 
12
 
13
  class GstPlayer:
14
- def __init__(self):
15
  self._logger = logging.getLogger(__name__)
16
  Gst.init(None)
17
  self._loop = GLib.MainLoop()
@@ -19,15 +19,12 @@ class GstPlayer:
19
 
20
  self.pipeline = Gst.Pipeline.new("audio_player")
21
 
22
- # Optional device name from env (substring match)
23
- audio_out = os.getenv("AUDIO_OUT")
24
-
25
  # Create elements
26
  self.appsrc = Gst.ElementFactory.make("appsrc", None)
27
  self.appsrc.set_property("format", Gst.Format.TIME)
28
  self.appsrc.set_property("is-live", True)
29
  caps = Gst.Caps.from_string(
30
- "audio/x-raw,format=S16LE,channels=1,rate=24000,layout=interleaved"
31
  )
32
  self.appsrc.set_property("caps", caps)
33
  queue = Gst.ElementFactory.make("queue")
@@ -36,7 +33,7 @@ class GstPlayer:
36
 
37
  # Try to pin specific output device; fallback to autoaudiosink
38
  audiosink = _create_device_element(
39
- direction="sink", name_substr=audio_out
40
  ) or Gst.ElementFactory.make("autoaudiosink")
41
 
42
  self.pipeline.add(self.appsrc)
@@ -82,15 +79,15 @@ class GstPlayer:
82
 
83
  def stop(self):
84
  logger = logging.getLogger(__name__)
85
-
86
  self._loop.quit()
87
  self.pipeline.set_state(Gst.State.NULL)
88
- self._thread_bus_calls.join()
 
89
  logger.info("Stopped Player")
90
 
91
 
92
  class GstRecorder:
93
- def __init__(self):
94
  self._logger = logging.getLogger(__name__)
95
  Gst.init(None)
96
  self._loop = GLib.MainLoop()
@@ -98,11 +95,9 @@ class GstRecorder:
98
 
99
  self.pipeline = Gst.Pipeline.new("audio_recorder")
100
 
101
- audio_in = os.getenv("AUDIO_IN")
102
-
103
  # Create elements: try specific mic; fallback to default
104
  autoaudiosrc = _create_device_element(
105
- direction="source", name_substr=audio_in
106
  ) or Gst.ElementFactory.make("autoaudiosrc", None)
107
 
108
  queue = Gst.ElementFactory.make("queue", None)
@@ -114,7 +109,7 @@ class GstRecorder:
114
  raise RuntimeError("Failed to create GStreamer elements")
115
 
116
  # Force mono/S16LE at 24000; resample handles device SR (e.g., 16000 → 24000)
117
- caps = Gst.Caps.from_string("audio/x-raw,channels=1,rate=24000,format=S16LE")
118
  self.appsink.set_property("caps", caps)
119
 
120
  # Build pipeline
@@ -168,13 +163,12 @@ class GstRecorder:
168
 
169
  def stop(self):
170
  logger = logging.getLogger(__name__)
171
-
172
  self._loop.quit()
173
  self.pipeline.set_state(Gst.State.NULL)
174
- self._thread_bus_calls.join()
 
175
  logger.info("Stopped Recorder")
176
 
177
-
178
  def _create_device_element(
179
  direction: str, name_substr: Optional[str]
180
  ) -> Optional[Gst.Element]:
 
11
 
12
 
13
  class GstPlayer:
14
+ def __init__(self, sample_rate: int = 24000, device_name: Optional[str] = None):
15
  self._logger = logging.getLogger(__name__)
16
  Gst.init(None)
17
  self._loop = GLib.MainLoop()
 
19
 
20
  self.pipeline = Gst.Pipeline.new("audio_player")
21
 
 
 
 
22
  # Create elements
23
  self.appsrc = Gst.ElementFactory.make("appsrc", None)
24
  self.appsrc.set_property("format", Gst.Format.TIME)
25
  self.appsrc.set_property("is-live", True)
26
  caps = Gst.Caps.from_string(
27
+ f"audio/x-raw,format=S16LE,channels=1,rate={sample_rate},layout=interleaved"
28
  )
29
  self.appsrc.set_property("caps", caps)
30
  queue = Gst.ElementFactory.make("queue")
 
33
 
34
  # Try to pin specific output device; fallback to autoaudiosink
35
  audiosink = _create_device_element(
36
+ direction="sink", name_substr=device_name
37
  ) or Gst.ElementFactory.make("autoaudiosink")
38
 
39
  self.pipeline.add(self.appsrc)
 
79
 
80
  def stop(self):
81
  logger = logging.getLogger(__name__)
 
82
  self._loop.quit()
83
  self.pipeline.set_state(Gst.State.NULL)
84
+ if self._thread_bus_calls is not None:
85
+ self._thread_bus_calls.join()
86
  logger.info("Stopped Player")
87
 
88
 
89
  class GstRecorder:
90
+ def __init__(self, sample_rate: int = 24000, device_name: Optional[str] = None):
91
  self._logger = logging.getLogger(__name__)
92
  Gst.init(None)
93
  self._loop = GLib.MainLoop()
 
95
 
96
  self.pipeline = Gst.Pipeline.new("audio_recorder")
97
 
 
 
98
  # Create elements: try specific mic; fallback to default
99
  autoaudiosrc = _create_device_element(
100
+ direction="source", name_substr=device_name
101
  ) or Gst.ElementFactory.make("autoaudiosrc", None)
102
 
103
  queue = Gst.ElementFactory.make("queue", None)
 
109
  raise RuntimeError("Failed to create GStreamer elements")
110
 
111
  # Force mono/S16LE at 24000; resample handles device SR (e.g., 16000 → 24000)
112
+ caps = Gst.Caps.from_string(f"audio/x-raw,channels=1,rate={sample_rate},format=S16LE")
113
  self.appsink.set_property("caps", caps)
114
 
115
  # Build pipeline
 
163
 
164
  def stop(self):
165
  logger = logging.getLogger(__name__)
 
166
  self._loop.quit()
167
  self.pipeline.set_state(Gst.State.NULL)
168
+ if self._thread_bus_calls is not None:
169
+ self._thread_bus_calls.join()
170
  logger.info("Stopped Recorder")
171
 
 
172
  def _create_device_element(
173
  direction: str, name_substr: Optional[str]
174
  ) -> Optional[Gst.Element]: