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:
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()