Skip to content

Instantly share code, notes, and snippets.

@giuliom95
Created June 16, 2020 16:42
Show Gist options
  • Save giuliom95/bc19fbfcb32015d15f2c0bdbde747517 to your computer and use it in GitHub Desktop.
Save giuliom95/bc19fbfcb32015d15f2c0bdbde747517 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import subprocess
import time
import os
import sys
import pyaudio
audio = pyaudio.PyAudio()
audiostream = audio.open(
format=pyaudio.paInt16,
channels=2,
rate=44100,
output=True
)
while True:
print('### Deleting files')
try: os.remove('cdda.raw')
except: pass
print('### Getting number of tracks')
while True:
paranoia = subprocess.Popen(['cdparanoia', '-Q'], stderr=subprocess.PIPE)
paranoia.wait()
notracks = len(paranoia.stderr.readlines()) - 9
if notracks > 0:
print(f'### Found {notracks} tracks')
break
print('### CD not found. Retrying...')
time.sleep(1)
print('### Launching ripping')
paranoia = subprocess.Popen(['cdparanoia', '--force-read-speed', '2', f'1-{notracks}', '-r', '-q'])
while True:
print('### Waiting for paranoia to write something...')
try:
stat = os.stat('cdda.raw')
if stat.st_size > 0:
break
except Exception as e:
if not isinstance(e, FileNotFoundError):
raise
time.sleep(1)
print('### Playing')
wav = open('cdda.raw', 'rb')
while True:
if paranoia.poll() == 1:
print('### Paranoia quitted! CD out?')
break
chunk = wav.read(1024)
if len(chunk) > 0:
audiostream.write(chunk)
else:
break
wav.close()
audiostream.stop_stream()
audiostream.close()
audio.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment