Browse Source

MusicPlayer component

A (hopefully) working commit of the MusicPlayer component.
Farmerjoe12 5 years ago
parent
commit
1859d306a7
2 changed files with 216 additions and 0 deletions
  1. 125
    0
      DList.py
  2. 91
    0
      MusicPlayer.py

+ 125
- 0
DList.py View File

@@ -0,0 +1,125 @@
1
+""" A node is a container which encapsulates any type of data.
2
+    The node object also has a reference to the NODE preceding and following
3
+    said object."""
4
+class Node(object):
5
+ 
6
+    def __init__(self, data, prev, next):
7
+        self.data = data
8
+        self.prev = prev
9
+        self.next = next
10
+        
11
+    def getNext(self):
12
+        if self.next is not None:
13
+            return self.next
14
+        print('There\'s nothing next, trying prev')
15
+        if self.prev is not None:
16
+            return self.prev
17
+        print('There\s nothing before either, just this one')
18
+        return self
19
+        
20
+    def getPrev(self):
21
+        if self.prev is not None:
22
+            return self.prev
23
+        print('There\'s nothing before, trying next')
24
+        if self.next is not None:
25
+            return self.next
26
+        print('There\'s nothing next either, just this one')
27
+        return self
28
+        
29
+    def getData(self):
30
+        return self.data
31
+ 
32
+""" A doubly-linked list implementation 
33
+    NOTE: the getters and setters return a reference to a Node object,
34
+    inside which is the data you're probably looking for. If you want to
35
+    access data, remember; after you call a get or find, use .data"""
36
+class DoubleList(object):
37
+ 
38
+    head = None
39
+    tail = None
40
+    length = 0;
41
+ 
42
+    """ Add a node to the list """
43
+    def append(self, data):
44
+        new_node = Node(data, None, None)
45
+        if self.head is None:
46
+            self.head = self.tail = new_node
47
+        else:
48
+            new_node.prev = self.tail
49
+            new_node.next = None
50
+            self.tail.next = new_node
51
+            self.tail = new_node
52
+        self.length = self.length + 1
53
+ 
54
+    """ Find a node in the list, then remove it """
55
+    def remove(self, node):
56
+        current_node = self.head
57
+ 
58
+        while current_node is not None:
59
+            if current_node == node:
60
+                # if it's not the first element
61
+                if current_node.prev is not None:
62
+                    current_node.prev.next = current_node.next
63
+                    current_node.next.prev = current_node.prev
64
+                else:
65
+                    # otherwise we have no prev (it's None), head is the next one, and prev becomes None
66
+                    self.head = current_node.next
67
+                    current_node.next.prev = None
68
+ 
69
+            current_node = current_node.next
70
+        self.length = self.length - 1
71
+    
72
+    """ Display the contents of this list NOTE: Currently broken"""
73
+    def show(self):
74
+        print("Show list data:")
75
+        current_node = self.head
76
+        while current_node is not None:
77
+            print(current_node.data)
78
+            current_node = current_node.next
79
+        print("*"*50)
80
+        
81
+    """ Find a node in the list by value comparison """
82
+    def findNode(self, node_value):
83
+        current_node = self.head
84
+        
85
+        while current_node is not None:
86
+            if current_node.data == node_value:
87
+                return current_node
88
+            current_node = current_node.next
89
+    
90
+    """ Get a node by index position """
91
+    def get(self, index):
92
+        if index > self.length:
93
+            return none
94
+        elif index == 1:
95
+            return self.head
96
+        elif index == self.length:
97
+            return self.tail
98
+        else:
99
+            current_node = self.head
100
+            pos = 1
101
+            while pos < index:
102
+                current_node = current_node.next
103
+                pos = pos + 1
104
+            return current_node
105
+    
106
+    """ Checks if a node is in this list """
107
+    def contains(self, node):
108
+        current_node = self.head
109
+        while current_node is not None:
110
+            if current_node == node:
111
+                return true
112
+            current_node = current_node.next
113
+        return false
114
+        
115
+    """ Get the length of this list """
116
+    def getLength(self):
117
+        return self.length
118
+        
119
+    """ Get the head of this list """
120
+    def getHead(self):  # get head (;
121
+        return self.head
122
+        
123
+    """ Get the tail of this list """
124
+    def getTail(self):
125
+        return self.tail

+ 91
- 0
MusicPlayer.py View File

@@ -0,0 +1,91 @@
1
+import bge
2
+import os
3
+import aud
4
+import random
5
+import glob
6
+from collections import OrderedDict
7
+from tinytag import TinyTag
8
+
9
+""" This is the Music Player as a component """
10
+class MusicPlayer(bge.types.KX_PythonComponent):
11
+
12
+    args = OrderedDict([
13
+        ("Now Playing", ''),
14
+        ("Playlist", None),
15
+        ("Last Song", ''),
16
+        ("Directory", ''),
17
+        ("Current Artist", ''),
18
+        ("Current Track Title", ''),
19
+        ("Track Path", ''),
20
+        ("Is Playing", False),
21
+        ("Audio Device", None),
22
+        ("Audio Factory", None),
23
+        ("Keyboard", None)       
24
+    ])
25
+
26
+    def start(self, args):
27
+        print('Music Player Class Entered')
28
+        self.nowPlaying = args['Now Playing']
29
+        self.lastSong = args['Last Song']
30
+        self.directory = args['Directory']
31
+        self.currArtist = args['Current Artist']
32
+        self.currTrackTitle = args['Current Track Title']
33
+        self.currTrackPath = args['Track Path']
34
+        self.isPlaying = args['Is Playing']
35
+        self.device = args['Audio Device']
36
+        self.factory = args['Audio Factory']
37
+        self.keyboard = args['Keyboard']
38
+        
39
+        print('Initilizing...')
40
+        self.init()
41
+
42
+        print('Printing tag')
43
+        self.printTag()
44
+
45
+    def init(self):
46
+        # Init the playlist and directory
47
+        self.directory = bge.logic.expandPath('//Music')
48
+        file_name = self.directory + "\\*.mp3"
49
+        file_list = glob.glob(file_name)
50
+        print('Directory: ' + self.directory)
51
+        print(file_list)
52
+        self.playlist = file_list
53
+        self.nowPlaying = random.choice(self.playlist)
54
+        
55
+        # Init the audio device
56
+        self.device = aud.device()
57
+        self.factory = aud.Factory(self.nowPlaying)
58
+        
59
+        # Init the keyboard
60
+        self.keyboard = bge.logic.keyboard.events
61
+
62
+    def update(self):
63
+        JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
64
+        
65
+        if self.keyboard.events[bge.events.PKEY] == JUST_ACTIVATED:
66
+            if isPlaying:
67
+                self.stopMusic()
68
+            else:
69
+                self.playMusic()
70
+
71
+    def printTag(self):
72
+        print('Current Track: ', self.nowPlaying)
73
+        self.currTrackPath = os.path.join(self.directory, self.nowPlaying)
74
+
75
+        try:
76
+            tag = TinyTag.get(self.currTrackPath)
77
+            self.currArtist = tag.artist
78
+            self.currTrackTitle = tag.title
79
+            print('Artist: %s' % self.currArtist, 
80
+                ' Track: %s' % self.currTrackTitle)
81
+        except:
82
+            print('Track has no tag')
83
+
84
+    def playMusic(self):
85
+        handle = self.device.play(self.factory)
86
+        self.isPlaying = True
87
+        
88
+    def stopMusic(self):
89
+        handle = self.device.play(self.factory)
90
+        handle.stop(factory)
91
+        self.isPlaying = False

Loading…
Cancel
Save