forked from Make-Magazine/PirateRadio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPirateRadio.py
executable file
·181 lines (144 loc) · 4.75 KB
/
PirateRadio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# Pirate Radio
# Author: Wynter Woods (Make Magazine)
try: # the following tests for a python3.x module
import configparser
except: # if the module isn't found, we're likely running python2.x and will just trick it into working
import ConfigParser as configparser
finally:
import re
import re
import random
import sys
import os
import threading
import time
import subprocess
fm_process = None
on_off = ["off", "on"]
config_location = "/pirateradio/pirateradio.conf"
frequency = 87.9
shuffle = False
repeat_all = False
merge_audio_in = False
play_stereo = True
music_dir = "/pirateradio"
music_pipe_r,music_pipe_w = os.pipe()
microphone_pipe_r,microphone_pipe_w = os.pipe()
def main():
daemonize()
setup()
files = build_file_list()
if repeat_all == True:
while(True):
play_songs(files)
else:
play_songs(files)
return 0
def build_file_list():
file_list = []
for root, folders, files in os.walk(music_dir):
folders.sort()
files.sort()
for filename in files:
if re.search(".(aac|mp3|wav|flac|m4a|ogg|pls|m3u)$", filename) != None:
file_list.append(os.path.join(root, filename))
return file_list
def play_songs(file_list):
print("Playing songs to frequency ", str(frequency))
print("Shuffle is " + on_off[shuffle])
print("Repeat All is " + on_off[repeat_all])
# print("Stereo playback is " + on_off[play_stereo])
if shuffle == True:
random.shuffle(file_list)
with open(os.devnull, "w") as dev_null:
for filename in file_list:
print("Playing ",filename)
if re.search(".pls$", filename) != None:
streamurl = parse_pls(filename, 1)
if streamurl != None:
print("streaming radio from " + streamurl)
subprocess.call(["ffmpeg","-i",streamurl,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)
elif re.search(".m3u$", filename) != None:
streamurl = parse_m3u(filename, 1)
if streamurl != None:
print("streaming radio from " + streamurl)
subprocess.call(["ffmpeg","-i",streamurl,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)
else:
subprocess.call(["ffmpeg","-i",filename,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)
def read_config():
global frequency
global shuffle
global repeat_all
global play_stereo
global music_dir
try:
config = configparser.ConfigParser()
config.read(config_location)
except:
print("Error reading from config file.")
else:
play_stereo = config.get("pirateradio", 'stereo_playback', fallback=True)
frequency = config.get("pirateradio",'frequency')
shuffle = config.getboolean("pirateradio",'shuffle',fallback=False)
repeat_all = config.getboolean("pirateradio",'repeat_all', fallback=False)
music_dir = config.get("pirateradio", 'music_dir', fallback="/pirateradio")
def parse_pls(src, titleindex):
# breaking up the pls file in separate strings
lines = []
with open( src, "r" ) as f:
lines = f.readlines()
# and parse the lines
if lines != None:
for line in lines:
# search for the URI
match = re.match( "^[ \\t]*file" + str(titleindex) + "[ \\t]*=[ \\t]*(.*$)", line, flags=re.IGNORECASE )
if match != None:
if match.group( 1 ) != None:
# URI found, it's saved in the second match group
# output the URI to the destination file
return match.group( 1 )
return None
def parse_m3u(src, titleindex):
# create a list of strings, one per line in the source file
lines = []
searchindex = int(1)
with open( src, "r" ) as f:
lines = f.readlines()
if lines != None:
for line in lines:
# search for the URI
if '://' in line:
if searchindex == titleindex:
return line
else:
searchindex += 1
return None
def daemonize():
fpid=os.fork()
if fpid!=0:
sys.exit(0)
def setup():
#threading.Thread(target = open_microphone).start()
global frequency
read_config()
# open_microphone()
run_pifm()
def run_pifm(use_audio_in=False):
global fm_process
with open(os.devnull, "w") as dev_null:
fm_process = subprocess.Popen(["/root/pifm","-",str(frequency),"44100", "stereo" if play_stereo else "mono"], stdin=music_pipe_r, stdout=dev_null)
#if use_audio_in == False:
#else:
# fm_process = subprocess.Popen(["/root/pifm2","-",str(frequency),"44100"], stdin=microphone_pipe_r, stdout=dev_null)
def record_audio_input():
return subprocess.Popen(["arecord", "-fS16_LE", "--buffer-time=50000", "-r", "44100", "-Dplughw:1,0", "-"], stdout=microphone_pipe_w)
def open_microphone():
global fm_process
audio_process = None
if os.path.exists("/proc/asound/card1"):
audio_process = record_audio_input()
run_pifm(merge_audio_in)
else:
run_pifm()
main()