Fugazi1978
Posts: 21
Joined: Sun Jul 01, 2012 11:43 am

A working command line ogg jukebox...

Thu Jul 05, 2012 10:26 am

Firstly, my code is clumsy and cluttered, feel free to edit to streamline and post it back.
Secondly, requires pygame and ogginfo to be installed.
Thirdly, the controls are "v" = previous track, "b" = pause/unpause track, "n" = next track.
Fourthly, music must be in a folder tree beginning with a folder called music located in same folder as the program:
python(folder):
jukebox.py
suspicious_photo.jpg
music(folder):
track1.ogg
flashdance(folder):
track2.ogg
Here's the code:

Code: Select all

import os
import sys
import pygame
import curses
#pygame.init()
loop = 0
esc = 0
os.system('clear')

# music files (ogg) must be in a folder, 'music', in same folder as program
os.system('ls -1pR music > files.out')

# open list of files
f = open('files.out', 'r')

# create a list C[ ]
C = ['this','is','a','list']

# empty C
C[:] = []

# read first line of files.out
A = f.readline()

# reads all lines of files.out, places full path to each file in C[ ]
while esc == 0:
	if A[-2:-1] == ":":
		while loop == 0:
			B = f.readline()
			if B[-2:-1] == ":":
				A = B
			if B[-5:-1] == ".ogg":
				C[len(C)+1:] = [A[:-2] + '/' + B[:-1]]
			if B == "":
				esc = 1
				loop = 1
	else:
		A = f.readline()

# create a list I[ ]
I = ['this','is','a','list']

# empty I
I[:] = []

# get the sample rate of the ogg files, puts in I[ ]
files = 0
while files < len(C):
	os.system('ogginfo ' + C[files] + ' > ogginfo.out')
	g = open('ogginfo.out', 'r')
	read = 0
	while read < 8:
		H = g.readline()
		read = read + 1
	J = H[6:]
	J = J[:-1]
	I[len(I)+1:] = [J]
	files = files + 1


#init the curses screen
stdscr = curses.initscr()

#use cbreak to not require a return key press
curses.cbreak()

track = 0
breaker = 0

# starts pygame clock
clock = pygame.time.Clock()

# set up the mixer
freq = 48000     # audio CD quality
bitsize = -16    # unsigned 16 bit
channels = 2     # 1 is mono, 2 is stereo
buffer = 2048    # number of samples (experiment to get right sound)
pygame.mixer.init(freq, bitsize, channels, buffer)

while breaker == 0:
	pz = 1
	pygame.mixer.quit()
	# set up the mixer
	freq = int(I[track])     # audio CD quality
	bitsize = -16    # unsigned 16 bit
	channels = 2     # 1 is mono, 2 is stereo
	buffer = 2048    # number of samples (experiment to get right sound)
	pygame.mixer.init(freq, bitsize, channels, buffer)
	# optional volume 0 to 1.0
	pygame.mixer.music.set_volume(1.0)
	pygame.mixer.music.load(C[track])
	pygame.mixer.music.play()
	# check if playback has finished
	while pygame.mixer.music.get_busy():
		clock.tick(30)
		# python curses to 'get' keyboard input
		k = stdscr.getch()
		# press b to pause/play track
		if curses.keyname(k)=="b":
			if pz == 1:
				pygame.mixer.music.pause()
				pz = 2
			else:
				pygame.mixer.music.unpause()
				pz = 1
		# press n to move to next track
		if curses.keyname(k)=="n":
			pygame.mixer.music.stop()
		# press v to go back a track
		if curses.keyname(k)=="v":
			track = track -2
			pygame.mixer.music.stop()
		# press q to quit
		if curses.keyname(k)=="q":
			breaker = 1
			pygame.mixer.music.stop()
	track = track + 1

	# check track number isn't less than first track
	if track < 0:
		track = len(C) + track

	# check track number isn't more than last track
	if track > len(C) - 1:
		track = 0
curses.endwin()
pygame.mixer.quit()

User avatar
clickykbd
Posts: 117
Joined: Wed Jun 06, 2012 2:21 am
Location: Austin, Texas
Contact: Website

Re: A working command line ogg jukebox...

Thu Jul 05, 2012 11:56 am

Fun.

For those interested in the CLI-musicplayer topic, might also check out xmms2 , which is client/server model and has command line clients in the repository (plus tons of optional libraries). Also mp3blaster (supports ogg) is an old favorite of mine, console mode via ncurses.

Fugazi1978
Posts: 21
Joined: Sun Jul 01, 2012 11:43 am

Re: A working command line ogg jukebox...

Thu Jul 05, 2012 1:37 pm

Changes so far involve changing:

Code: Select all

# create a list C[ ]
C = ['this','is','a','list']

# empty C
C[:] = []
to:

Code: Select all

# create an empty list C[ ]
C = []
People keep mentioning os.path (and I shall look in a bit) but I like having a file with the list of paths so eventually I can work out how to restart the play, after turning off, at the last track played prior to turning off.

Return to “Python”