Skip to content

Commit

Permalink
added cloud decoding for Raspberry PI
Browse files Browse the repository at this point in the history
  • Loading branch information
xuancong84 committed Feb 23, 2024
1 parent 7e8be0b commit 9a54a3d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
22 changes: 7 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
#!/usr/bin/env python3

import argparse
import datetime
import json
import locale
import logging
import os, sys, io
import shutil
import signal
import subprocess
import threading
import time
import traceback
import webbrowser
import argparse, datetime, json, locale, os, sys, io, shutil
import signal, subprocess, threading, time, traceback, webbrowser
from functools import wraps

import cherrypy
Expand All @@ -36,7 +25,7 @@
app = Flask(__name__)
app.secret_key = os.urandom(24)
site_name = "PiKaraoke"
admin_password = None
admin_password = K = None
os.texts = defaultdict(lambda: "")
getString = lambda ii: os.texts[ii]
getString1 = lambda lang, ii: os.langs[lang].get(ii, os.langs['en_US'][ii])
Expand Down Expand Up @@ -895,6 +884,10 @@ def get_default_browser_cookie(platform):
help = "Run in flask developer mode. Only useful for tweaking the web UI in real time. Will disable the splash screen due to pygame main thread conflicts and may require FLASK_ENV=development env variable for full dev mode features.",
action = "store_true",
)
parser.add_argument('--cloud', '-C',
default='',
help='full URL for cloud decoding',
)
args = parser.parse_args()

set_language(args.lang)
Expand Down Expand Up @@ -963,7 +956,6 @@ def get_default_browser_cookie(platform):
args.hide_splash_screen = True

# Configure karaoke process
global K
os.K = K = karaoke.Karaoke(args)

if (args.developer_mode):
Expand Down
40 changes: 37 additions & 3 deletions karaoke.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os, sys, io, random, time, json, hashlib
import logging, socket, subprocess
import logging, socket, subprocess, threading
import multiprocessing as mp
import shutil, psutil
import shutil, psutil, traceback, tarfile, requests
from subprocess import check_output
from collections import *

Expand All @@ -19,6 +19,7 @@
from app import getString

STD_VOL = 65536/8/np.sqrt(2)
TMP_DIR = '/dev/shm'

if get_platform() != "windows":
from signal import SIGALRM, alarm, signal
Expand Down Expand Up @@ -142,7 +143,6 @@ def __init__(self, args):
except:
pass


# clean up old sessions
self.kill_player()

Expand All @@ -159,6 +159,37 @@ def __init__(self, args):
self.initialize_screen(not args.windowed)
self.render_splash_screen()

self.cloud = args.cloud
if args.cloud:
self.cloud_trigger = threading.Event()
self.cloud_tasks = []
threading.Thread(target=self._cloud_thread).start()


def _cloud_thread(self):
while True:
self.cloud_trigger.wait()
self.cloud_trigger.clear()
while self.cloud_tasks:
try:
fn = self.cloud_tasks.pop(0)
bn, dn = os.path.basename(fn), os.path.dirname(fn)
if os.path.isfile(f'{self.download_path}nonvocal/{bn}.m4a') and os.path.isfile(f'{self.download_path}vocal/{bn}.m4a'):
continue
os.system(f'ffmpeg -y -i "{fn}" -vn -c copy {TMP_DIR}/input.m4a')
with open(f'{TMP_DIR}/input.m4a', 'rb') as f:
r = requests.post(self.cloud, files={'file': f})
with open(f'{TMP_DIR}/output.tar.gz', 'wb') as f:
f.write(r.content)
with tarfile.open(f'{TMP_DIR}/output.tar.gz') as tar:
tar.extract('nonvocal.m4a', f'{self.download_path}nonvocal')
os.rename(f'{self.download_path}nonvocal/nonvocal.m4a', f'{self.download_path}nonvocal/{bn}.m4a')
tar.extract('vocal.m4a', f'{self.download_path}vocal')
os.rename(f'{self.download_path}vocal/vocal.m4a', f'{self.download_path}vocal/{bn}.m4a')
except:
traceback.print_exc()


# Other ip-getting methods are unreliable and sometimes return 127.0.0.1
# https://stackoverflow.com/a/28950776
def get_ip(self):
Expand Down Expand Up @@ -1191,6 +1222,9 @@ def run(self):
self.handle_run_loop()
head = self.queue.pop(0)
self.play_file(head['file'])
if self.cloud:
self.cloud_tasks += [head['file']]
self.cloud_trigger.set()
if not self.firstSongStarted:
if self.streamer_alive():
self.streamer_restart(1)
Expand Down

0 comments on commit 9a54a3d

Please sign in to comment.