Skip to content

Commit 61b5635

Browse files
authored
Merge pull request #55 from sushma-s1/gst-python-application
Added Gst-Python-sample applications
2 parents 088f2a3 + 54aadc8 commit 61b5635

37 files changed

+2432
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Gst_ALSA
2+
The purpose of this application is to implement the playback and recording functions of gstreamer+alsa on the QTI platform.
3+
4+
## Note:
5+
6+
- If pulseaudio service is enabled, disable it and start the alsa-restore service.
7+
```bash
8+
$ systemctl stop pulseaudio
9+
$ apt-get install alsa-utils
10+
$ systemctl start alsa-restore.service
11+
```
12+
BTW, if you want to disable this service, please run:
13+
```
14+
$ systemctl disable qc-alsa-restore
15+
```
16+
### Steps to run alsa playback and recording application
17+
- Go to the downloaded directory on RB5
18+
```bash
19+
cd /gst-python-samples/Gstreamer-Applications/gst-alsa
20+
```
21+
- Run the alsa playback and record application
22+
23+
-**Alsa audio playback:** Provide playback as the first argument for audio playback.
24+
```bash
25+
python3 gst-alsa playback audiofile.wav
26+
```
27+
-**Alsa audio recording:** Provide capture as the first argument for alsa audio recording.
28+
```bash
29+
python3 gst-alsa capture audiorecord.wav
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import sys
2+
import gi
3+
import logging
4+
import os
5+
gi.require_version("GLib", "2.0")
6+
gi.require_version("GObject", "2.0")
7+
gi.require_version("Gst", "1.0")
8+
from gi.repository import Gst, GLib, GObject
9+
from numpy import ndarray
10+
11+
12+
#Playback sample code
13+
def gst_sample_playback(content):
14+
# Create the empty pipeline
15+
pipeline = Gst.Pipeline.new("audio playback")
16+
filesrc = Gst.ElementFactory.make("filesrc", "file_src")
17+
wavparse = Gst.ElementFactory.make("wavparse", "wav_parse");
18+
alsasink = Gst.ElementFactory.make("alsasink", "alsa_sink");
19+
20+
if not pipeline or not filesrc or not wavparse or not alsasink:
21+
print("Create pipeline/filesrc/wavparse/alsasink element failed.")
22+
sys.exit(1)
23+
24+
#set properties to the elements
25+
filesrc.set_property('location',content)
26+
alsasink.set_property('device',"hw:0,0")
27+
28+
#Build pipeline
29+
#Add elements to the pipeline and link them
30+
pipeline.add(filesrc)
31+
pipeline.add(wavparse)
32+
pipeline.add(alsasink)
33+
34+
if not filesrc.link(wavparse):
35+
print("ERROR: Could not link filesrc to wavparse")
36+
sys.exit(1)
37+
if not wavparse.link(alsasink):
38+
print("ERROR: Could not link wavparse to alsasink")
39+
sys.exit(1)
40+
41+
pipeline.set_state(Gst.State.PLAYING)
42+
print("Start");
43+
bus = pipeline.get_bus()
44+
bus_id = bus.add_signal_watch()
45+
msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE,Gst.MessageType.ERROR | Gst.MessageType.EOS)
46+
47+
if msg:
48+
t = msg.type
49+
if t == Gst.MessageType.ERROR:
50+
err, dbg = msg.parse_error()
51+
print("ERROR:", msg.src.get_name(), ":", err.message)
52+
if dbg:
53+
print("debugging info:", dbg)
54+
elif t == Gst.MessageType.EOS:
55+
print("Stop end-Of-Stream reached")
56+
else:
57+
print("ERROR: Unexpected message received.")
58+
59+
pipeline.set_state(Gst.State.NULL)
60+
Gst.Object.unref(bus)
61+
Gst.deinit()
62+
sys.exit(1)
63+
64+
65+
#Capture sample code
66+
def gst_sample_capture(content):
67+
#create the empty pipeline
68+
alsasrc = Gst.ElementFactory.make("alsasrc", "alsa_src");
69+
capsfilter = Gst.ElementFactory.make("capsfilter", "caps_filter");
70+
wavenc = Gst.ElementFactory.make("wavenc", "wav_enc");
71+
filesink = Gst.ElementFactory.make("filesink", "file_sink");
72+
pipeline = Gst.Pipeline.new("audio_capture_stream");
73+
74+
if not pipeline or not alsasrc or not capsfilter or not wavenc or not filesink:
75+
print("Create pipeline/alsasrc/capsfilter/wavenc/filesink element failed.")
76+
sys.exit(1)
77+
78+
#set properties to the elements
79+
alsasrc.set_property("device","hw:0,0")
80+
alsasrc.set_property("num-buffers",1000)
81+
alsasrc.set_property("buffer-time",10000)
82+
caps=Gst.Caps.new_empty_simple("audio/x-raw")
83+
capsfilter.set_property("caps",caps)
84+
filesink.set_property("location",content)
85+
86+
#Build the pipeline. Add elements to the pipeline and link them
87+
pipeline.add(alsasrc)
88+
pipeline.add(capsfilter)
89+
pipeline.add(wavenc)
90+
pipeline.add(filesink)
91+
92+
if not alsasrc.link(capsfilter):
93+
print("ERROR: Could not link alsasrc to capsfilter")
94+
sys.exit(1)
95+
if not capsfilter.link(wavenc):
96+
print("ERROR: Could not link capsfilter to wavenc")
97+
sys.exit(1)
98+
if not wavenc.link(filesink):
99+
print("ERROR: Could not link wavenc to filesink")
100+
sys.exit(1)
101+
102+
pipeline.set_state(Gst.State.PLAYING)
103+
print("Start");
104+
105+
bus = pipeline.get_bus()
106+
bus_id = bus.add_signal_watch()
107+
msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE,Gst.MessageType.ERROR | Gst.MessageType.EOS)
108+
if msg:
109+
t = msg.type
110+
if t == Gst.MessageType.ERROR:
111+
err, dbg = msg.parse_error()
112+
print("ERROR:", msg.src.get_name(), ":", err.message)
113+
if dbg:
114+
print("debugging info:", dbg)
115+
elif t == Gst.MessageType.EOS:
116+
print("Stop end-Of-Stream reached")
117+
else:
118+
print("ERROR: Unexpected message received.")
119+
120+
pipeline.set_state(Gst.State.NULL)
121+
Gst.Object.unref(bus)
122+
Gst.deinit()
123+
sys.exit(1)
124+
125+
126+
def main():
127+
Gst.init(sys.argv)
128+
if len(sys.argv) < 2:
129+
print("Missing <output-location> parameter")
130+
sys.exit(1)
131+
else:
132+
if (sys.argv[1]=="playback"):
133+
gst_sample_playback(sys.argv[2]);
134+
elif(sys.argv[1]=="capture"):
135+
gst_sample_capture(sys.argv[2]);
136+
else:
137+
print("Unexpected operation %s", sys.argv[1]);
138+
139+
140+
if __name__ == "__main__":
141+
main()
30.3 KB
Loading
33.1 KB
Loading
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()

0 commit comments

Comments
 (0)