forked from Make-Magazine/PirateRadio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PirateRadio.py
executable file
·124 lines (93 loc) · 2.83 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
#!/usr/bin/env python
# Pirate Radio
# Author: Wynter Woods (Make Magazine)
import os
import sys
import subprocess
import configparser
import re
import random
import threading
import time
fm_process = None
on_off = ["off", "on"]
frequency = 87.9
shuffle = False
repeat_all = False
merge_audio_in = False
play_stereo = True
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("/pirateradio"):
folders.sort()
files.sort()
for filename in files:
if re.search(".(aac|mp3|wav|flac|m4a)$", 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)
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
try:
config = configparser.ConfigParser()
config.read("/pirateradio/pirateradio.config")
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)
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()