Skip to content

Commit 7b47cc3

Browse files
committed
Added Gst-Python-sample applications
Signed-off-by: s.sushma <s.sushma@globaledgesoft.com>
1 parent 088f2a3 commit 7b47cc3

24 files changed

+1329
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# PulseAudio Record and Playback
3+
4+
This application demonstrates the audio record and playback with pulse audio.
5+
6+
## Audio Record
7+
8+
9+
Audio_record application records audio from a pulse audio source. Two formats are supported: WAV and MP3.
10+
11+
**Note:**
12+
- On board, DIP_SW_0 SW1 must be switched to ON.
13+
- If there is Alsa service enabled, please remove Alsa service to make PulseAudio source work
14+
15+
### Start Recording:
16+
Steps for running the audio recording python application in wav and mp3 format.
17+
18+
- Go to the downloaded directory on RB5 /gst-python-samples/Gstreamer-Applications/gst_audio
19+
```bash
20+
$ cd sample-apps-for-Qualcomm-Robotics-RB5-platform/gst-python-samples/Gstreamer-Applications/gst_audio
21+
```
22+
- Recording audio in wav and mp3 format. Provide the filename along with the format as an argument for the command.
23+
```bash
24+
$ python3 audio_record.py file.wav
25+
$ python3 audio_record.py file.mp3
26+
```
27+
28+
### Stop Automatically:
29+
+ The recording stops automatically in 10 seconds.
30+
31+
## Audio Playback
32+
33+
This application plays audio from a file to pulseaudio sink. Three formats are supported: WAV, AAC and MP3.
34+
35+
**Note:** Speaker must be connected to the board
36+
37+
### Start Playback:
38+
Steps for running the audio playback python application in wav, mp3 and aac format
39+
40+
- Go to the downloaded directory on RB5
41+
```bash
42+
$ cd sample-apps-for-Qualcomm-Robotics-RB5-platform/gst-python-samples/Gstreamer-Applications/gst_audio
43+
```
44+
45+
- Download the audio files of the wav, mp3 and aac formats in the same folder.
46+
- Audio playback supports three formats wav, mp3 and aac.
47+
Provide the filename along with the format as an argument for the command.
48+
```bash
49+
$ python3 audio_playback.py file.wav
50+
$ python3 audio_playback.py file.mp3
51+
$ python3 audio_playback.py file.aac
52+
```
53+
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import sys
2+
import gi
3+
import logging
4+
import os
5+
6+
gi.require_version("GLib", "2.0")
7+
gi.require_version("GObject", "2.0")
8+
gi.require_version("Gst", "1.0")
9+
10+
from gi.repository import Gst, GLib
11+
12+
logging.basicConfig(level=logging.DEBUG,
13+
format="[%(name)s] [%(levelname)8s] - %(message)s")
14+
logger = logging.getLogger(__name__)
15+
16+
if __name__ == "__main__":
17+
# Initialize GStreamer passing command line argument
18+
Gst.init(sys.argv)
19+
loop = GLib.MainLoop()
20+
21+
if len(sys.argv) < 1:
22+
print("Missing <output-location> parameter")
23+
sys.exit(1)
24+
25+
file_format = os.path.splitext(sys.argv[1])[1]
26+
print(file_format)
27+
28+
# Create the empty pipeline
29+
pipeline = Gst.Pipeline.new("audio playback")
30+
filesrc = Gst.ElementFactory.make("filesrc", "file_src")
31+
pulsesink = Gst.ElementFactory.make("pulsesink", "pulseaudio_sink")
32+
33+
if not pipeline or not filesrc or not pulsesink:
34+
print("Create element failed.")
35+
sys.exit(1)
36+
37+
filesrc.set_property('location', sys.argv[1])
38+
39+
if file_format == ".wav":
40+
wavparse = Gst.ElementFactory.make("wavparse", "wav_parse")
41+
42+
if not wavparse:
43+
print("Create element failed.\n")
44+
sys.exit(1)
45+
46+
pipeline.add(filesrc)
47+
pipeline.add(wavparse)
48+
pipeline.add(pulsesink)
49+
50+
if not filesrc.link(wavparse):
51+
print("ERROR: Could not link filesrc to wavparse")
52+
sys.exit(1)
53+
54+
if not wavparse.link(pulsesink):
55+
print("ERROR: Could not link decoder to video_convert")
56+
sys.exit(1)
57+
58+
elif file_format == ".aac":
59+
60+
aacparse = Gst.ElementFactory.make("aacparse", "aac_parse")
61+
avdec_aac = Gst.ElementFactory.make("avdec_aac", "avdec_aac")
62+
63+
if not aacparse or not avdec_aac:
64+
print("Create element failed.\n")
65+
sys.exit(1)
66+
67+
pipeline.add(filesrc)
68+
pipeline.add(aacparse)
69+
pipeline.add(avdec_aac)
70+
pipeline.add(pulsesink)
71+
72+
if not filesrc.link(aacparse):
73+
print("ERROR: Could not link filesrc to aacparse")
74+
sys.exit(1)
75+
76+
if not aacparse.link(avdec_aac):
77+
print("ERROR: Could not link aacparse to avdec_aac")
78+
sys.exit(1)
79+
80+
if not avdec_aac.link(pulsesink):
81+
print("ERROR: Could not link avdec_aac to pulsesink")
82+
sys.exit(1)
83+
84+
elif file_format == ".mp3":
85+
86+
mpegaudioparse = Gst.ElementFactory.make(
87+
"mpegaudioparse", "mpegaudio_parse")
88+
avdec_mp3 = Gst.ElementFactory.make("avdec_mp3", "avdec_mp3")
89+
90+
if not mpegaudioparse or not avdec_mp3:
91+
print("Create element failed.\n")
92+
sys.exit(1)
93+
94+
pipeline.add(filesrc)
95+
pipeline.add(mpegaudioparse)
96+
pipeline.add(avdec_mp3)
97+
pipeline.add(pulsesink)
98+
99+
if not filesrc.link(mpegaudioparse):
100+
print("ERROR: Could not link filesrc to mpegaudioparse")
101+
sys.exit(1)
102+
103+
if not mpegaudioparse.link(avdec_mp3):
104+
print("ERROR: Could not link mpegaudioparse to avdec_mp3")
105+
sys.exit(1)
106+
107+
if not avdec_mp3.link(pulsesink):
108+
print("ERROR: Could not link avdec_mp3 to pulsesink")
109+
sys.exit(1)
110+
else:
111+
print("Format not supported\n")
112+
sys.exit(1)
113+
114+
115+
pipeline.set_state(Gst.State.PLAYING)
116+
print("audio playback started")
117+
bus = pipeline.get_bus()
118+
bus_id = bus.add_signal_watch()
119+
msg = bus.timed_pop_filtered(
120+
Gst.CLOCK_TIME_NONE,
121+
Gst.MessageType.ERROR | Gst.MessageType.EOS)
122+
123+
if msg:
124+
t = msg.type
125+
if t == Gst.MessageType.ERROR:
126+
err, dbg = msg.parse_error()
127+
print("ERROR:", msg.src.get_name(), ":", err.message)
128+
if dbg:
129+
print("debugging info:", dbg)
130+
elif t == Gst.MessageType.EOS:
131+
print("End-Of-Stream reached stoped")
132+
else:
133+
print("ERROR: Unexpected message received.")
134+
135+
pipeline.set_state(Gst.State.NULL)
136+
Gst.Object.unref(bus)
137+
Gst.deinit()
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import sys
2+
import gi
3+
import logging
4+
import os
5+
6+
gi.require_version("GLib", "2.0")
7+
gi.require_version("GObject", "2.0")
8+
gi.require_version("Gst", "1.0")
9+
10+
from gi.repository import Gst, GLib, GObject
11+
12+
logging.basicConfig(level=logging.DEBUG,
13+
format="[%(name)s] [%(levelname)8s] - %(message)s")
14+
logger = logging.getLogger(__name__)
15+
16+
if __name__ == "__main__":
17+
# Initialize GStreamer passing command line argument
18+
Gst.init(sys.argv)
19+
loop = GLib.MainLoop()
20+
21+
if len(sys.argv) < 1:
22+
print("Missing <output-location> parameter")
23+
sys.exit(1)
24+
25+
file_format = os.path.splitext(sys.argv[1])[1]
26+
27+
# Create the empty pipeline
28+
pipeline = Gst.Pipeline.new("audio record")
29+
pulsesrc = Gst.ElementFactory.make("pulsesrc", "pulseaudio_src");
30+
audioconvert = Gst.ElementFactory.make("audioconvert", "audio_convert");
31+
capsfilter = Gst.ElementFactory.make("capsfilter", "caps_filter");
32+
queue = Gst.ElementFactory.make("queue", "queue");
33+
filesink = Gst.ElementFactory.make("filesink", "file_sink");
34+
35+
36+
if not pipeline or not pulsesrc or not audioconvert or not capsfilter or not queue or not filesink:
37+
print("Create element failed")
38+
sys.exit(1)
39+
40+
41+
pulsesrc.set_property('num-buffers', 1000)
42+
pulsesrc.set_property('buffer-time', 30000)
43+
44+
caps=Gst.Caps.new_empty_simple("audio/x-raw")
45+
capsfilter.set_property('caps', caps)
46+
47+
filesink.set_property('location', sys.argv[1])
48+
49+
if file_format == ".wav":
50+
wavenc=Gst.ElementFactory.make("wavenc", "wav_enc");
51+
if not wavenc:
52+
print("Create element failed.\n");
53+
sys.exit(1)
54+
55+
pipeline.add(pulsesrc)
56+
pipeline.add(capsfilter)
57+
pipeline.add(audioconvert)
58+
pipeline.add(wavenc)
59+
pipeline.add(queue)
60+
pipeline.add(filesink)
61+
62+
if not pulsesrc.link(capsfilter):
63+
print("ERROR: Could not link pulsesrc to capsfilter")
64+
sys.exit(1)
65+
if not capsfilter.link(audioconvert):
66+
print("ERROR: Could not link capsfilter to audioconvert")
67+
sys.exit(1)
68+
if not audioconvert.link(wavenc):
69+
print("ERROR: Could not link audioconvert to wavenc")
70+
sys.exit(1)
71+
if not wavenc.link(queue):
72+
print("ERROR: Could not link wavenc to queue")
73+
sys.exit(1)
74+
if not queue.link(filesink):
75+
print("ERROR: Could not link queue to filesink")
76+
sys.exit(1)
77+
78+
elif file_format == ".aac":
79+
avenc_aac=Gst.ElementFactory.make("avenc_aac", "aac_enc");
80+
aacparse=Gst.ElementFactory.make("aacparse", "aac_parse");
81+
aac_caps=Gst.ElementFactory.make("capsfilter", "aac_caps_filter");
82+
83+
if not avenc_aac or not aacparse or not aac_caps:
84+
print("Create element failed.\n");
85+
sys.exit(1)
86+
87+
caps=Gst.Caps.new_empty_simple('audio/mpeg')
88+
aac_caps.set_property('caps', caps)
89+
pipeline.add(pulsesrc)
90+
pipeline.add(capsfilter)
91+
pipeline.add(audioconvert)
92+
pipeline.add(avenc_aac)
93+
pipeline.add(aacparse)
94+
pipeline.add(aac_caps)
95+
pipeline.add(queue)
96+
pipeline.add(filesink)
97+
if not pulsesrc.link(capsfilter):
98+
print("ERROR: Could not link pulsesrc to capsfilter")
99+
sys.exit(1)
100+
101+
if not capsfilter.link(audioconvert):
102+
print("ERROR: Could not link capsfilter to audioconvert")
103+
sys.exit(1)
104+
105+
if not audioconvert.link(avenc_aac):
106+
print("ERROR: Could not link audioconvert to avenc_aac")
107+
sys.exit(1)
108+
109+
if not avenc_aac.link(aacparse):
110+
print("ERROR: Could not link avenc_aac to aacparse")
111+
sys.exit(1)
112+
113+
if not aacparse.link(aac_caps):
114+
print("ERROR: Could not link aacparse to aac_caps")
115+
sys.exit(1)
116+
117+
if not aac_caps.link(queue):
118+
print("ERROR: Could not link aac_caps to queue")
119+
sys.exit(1)
120+
121+
if not queue.link(filesink):
122+
print("ERROR: Could not link queue to filesink")
123+
sys.exit(1)
124+
125+
elif file_format == ".mp3":
126+
lamemp3enc=Gst.ElementFactory.make("lamemp3enc", "mp3_enc");
127+
128+
if not lamemp3enc:
129+
print("Create element failed.\n");
130+
sys.exit(1)
131+
132+
pipeline.add(pulsesrc)
133+
pipeline.add(capsfilter)
134+
pipeline.add(audioconvert)
135+
pipeline.add(lamemp3enc)
136+
pipeline.add(queue)
137+
pipeline.add(filesink)
138+
139+
if not pulsesrc.link(capsfilter):
140+
print("ERROR: Could not link pulsesrc to capsfilter")
141+
sys.exit(1)
142+
143+
if not capsfilter.link(audioconvert):
144+
print("ERROR: Could not link capsfilter to audioconvert")
145+
sys.exit(1)
146+
if not audioconvert.link(lamemp3enc):
147+
print("ERROR: Could not link audioconvert to lamemp3enc")
148+
sys.exit(1)
149+
150+
if not lamemp3enc.link(queue):
151+
print("ERROR: Could not link lamemp3enc to queue")
152+
sys.exit(1)
153+
if not queue.link(filesink):
154+
print("ERROR: Could not link queue to filesink")
155+
sys.exit(1)
156+
157+
else:
158+
print("Format %s not supported\n", format);
159+
sys.exit(1)
160+
161+
162+
pipeline.set_state(Gst.State.PLAYING)
163+
print("Audio record started");
164+
bus = pipeline.get_bus()
165+
bus_id = bus.add_signal_watch()
166+
msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE,Gst.MessageType.ERROR | Gst.MessageType.EOS)
167+
if msg:
168+
t = msg.type
169+
if t == Gst.MessageType.ERROR:
170+
err, dbg = msg.parse_error()
171+
print("ERROR:", msg.src.get_name(), ":", err.message)
172+
if dbg:
173+
print("debugging info:", dbg)
174+
elif t == Gst.MessageType.EOS:
175+
print("stopped recording")
176+
else:
177+
print("ERROR: Unexpected message received.")
178+
179+
pipeline.set_state(Gst.State.NULL)
180+
Gst.Object.unref(bus)
181+
Gst.deinit()

0 commit comments

Comments
 (0)