#82 player looks good to go.

Merged
shuvit merged 3 commits from shuvit/shuvit:Music-Player_fix into dev 5 years ago

+ 172
- 0
MusicPlayer.py View File

@@ -0,0 +1,172 @@
1
+import bge
2
+import os
3
+import aud
4
+import random
5
+import glob
6
+from DList import DoubleList
7
+from collections import OrderedDict
8
+from tinytag import TinyTag
9
+
10
+""" This is the Music Player as a component """
11
+class MusicPlayer(bge.types.KX_PythonComponent):
12
+
13
+    args = OrderedDict([])    
14
+
15
+    def start(self, args):
16
+        # dictionary
17
+        self.dictionary = bge.logic.globalDict
18
+        
19
+        # get mp3 files from the disk and create a list
20
+        self.directory = bge.logic.expandPath('//Music')
21
+        file_name = self.directory + '\\*.mp3'
22
+        file_list =  glob.glob(file_name)
23
+        
24
+        # use the mp3 list to make a playlist of nodes
25
+        self.playlist = DoubleList()
26
+        for name in file_list:
27
+            self.playlist.append(name)
28
+        
29
+        # previously played list of nodes
30
+        self.alreadyPlayed = DoubleList()
31
+        
32
+        # get a random song from the playlist, 
33
+        # remove it from the playlist
34
+        # add it to the alreadyPlayed list
35
+        # NOTICE: currentNode is a reference to a NODE in a list, if you want to play the song inside the node, use .data
36
+        self.currentNode = self.playlist.get(random.randint(0, self.playlist.getLength()))
37
+        self.nowPlaying = self.currentNode.getData()
38
+        self.playlist.remove(self.currentNode)
39
+        self.alreadyPlayed.append(self.nowPlaying)
40
+        print("Now Playing: " + self.nowPlaying)
41
+        self.setTag()
42
+        self.isPlaying = False
43
+
44
+        # create audio devices
45
+        self.device = aud.device()
46
+        self.factory = aud.Factory(self.nowPlaying)
47
+        self.handle = None        
48
+    
49
+    """ Update is called once every frame, and this is where
50
+        any input handling should go
51
+        
52
+        Controller Buttons: 
53
+        Y-Button - Play/Pause
54
+        LB       - Previous Track
55
+        RB       - Next Track
56
+        
57
+        TODO: Keyboard Buttons if these aren't alright
58
+        P        - Play/Pause
59
+        Pad Plus - Next Track
60
+        Pad Minus- Prev Track
61
+    """
62
+    def update(self):
63
+        keyboard = bge.logic.keyboard.events
64
+        up_dict = bge.logic.globalDict
65
+        #print(dictionary)
66
+        pKey = keyboard[bge.events.PKEY]
67
+        pPlus = keyboard[bge.events.PADPLUSKEY]
68
+        pMinus = keyboard[bge.events.PADMINUS]
69
+        lb = up_dict['lBump']
70
+        last_lb = up_dict['last_lBump']
71
+        rb = up_dict['rBump']
72
+        last_rb = up_dict['last_rBump']
73
+        y = up_dict['yBut']
74
+        last_y = up_dict['last_yBut']
75
+        JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
76
+        
77
+
78
+        #Run player on startup
79
+        if up_dict['music_start'] == True:
80
+            if self.isPlaying:
81
+                self.stopMusic()
82
+            else:
83
+                self.playMusic()                 
84
+            up_dict['music_start'] = False               
85
+
86
+        # Music controls only during pause/replay
87
+        if (up_dict['npause'] == True or up_dict['playback'] == True) or up_dict['music_start'] == True:
88
+            #stop/play
89
+            if (y == False and last_y == True) or pKey == JUST_ACTIVATED:
90
+                if self.isPlaying:
91
+                    self.stopMusic()
92
+                    up_dict['music_player'] = 0
93
+                else:
94
+                    self.playMusic()
95
+                    up_dict['music_player'] = 1
96
+            # Prev/Next
97
+            if self.isPlaying and self.handle.status:
98
+                if pMinus == JUST_ACTIVATED or (lb == False and last_lb == True):
99
+                    self.prevSong()
100
+                
101
+                if pPlus == JUST_ACTIVATED or (rb == False and last_rb == True):
102
+                    self.nextSong()
103
+        # if song ends, play next song
104
+        if self.isPlaying and not self.handle.status:
105
+            self.nextSong()
106
+
107
+    """ get info about song using TinyTag, then print to console """
108
+    def setTag(self):                
109
+        self.currTrackPath = os.path.join(self.directory, self.nowPlaying)
110
+        tag = TinyTag.get(self.currTrackPath)
111
+        self.currArtist = tag.artist
112
+        self.currTrackTitle = tag.title
113
+        self.dictionary['mu_artist'] = self.currArtist
114
+        self.dictionary['mu_title'] = self.currTrackTitle
115
+                
116
+        print('Artist: %s' % self.currArtist, 
117
+                ' Track: %s' % self.currTrackTitle)
118
+
119
+    def playMusic(self):
120
+        print('Play Music')
121
+        self.setTag()
122
+        self.handle = self.device.play(self.factory)
123
+        self.isPlaying = True
124
+        
125
+    def stopMusic(self):
126
+        print('Stop Music')
127
+        self.handle.stop()
128
+        self.isPlaying = False
129
+        
130
+    def nextSong(self):
131
+        print('Next Song')
132
+        # stop the current song
133
+        self.handle.stop()
134
+        
135
+        # get a song from the playlist
136
+        self.currentNode = self.playlist.get(random.randint(0, self.playlist.getLength()))
137
+        self.nowPlaying = self.currentNode.getData()
138
+        self.playlist.remove(self.currentNode)
139
+                
140
+        # add the song to the already played list
141
+        self.alreadyPlayed.append(self.nowPlaying)  
142
+        
143
+        # start the song
144
+        self.factory = aud.Factory(self.nowPlaying)
145
+        self.handle = self.device.play(self.factory)
146
+        self.setTag()
147
+        
148
+        # If playlist is empty, re-fill it
149
+        if self.playlist.getLength() == 0:
150
+            file_name = self.directory + '\\*.mp3'
151
+            file_list =  glob.glob(file_name)
152
+            for name in file_list:
153
+                self.playlist.append(name)
154
+        
155
+    """ Note on first call of this method, the current node will be equal to the tail of the already played list, 
156
+        however, it will be a node from a different list. We need to set current node equal to the tail node
157
+        of the already played list so that we can iterate through that list, instead of the other list """
158
+    def prevSong(self):
159
+        print('Prev Song')
160
+        # stop the current song
161
+        self.handle.stop()        
162
+        
163
+        if self.currentNode.getData() == self.alreadyPlayed.getTail().getData():
164
+            self.currentNode = self.alreadyPlayed.getTail()
165
+            
166
+        self.currentNode = self.currentNode.getPrev()
167
+        self.nowPlaying = self.currentNode.getData()
168
+        
169
+        # start the song
170
+        self.factory = aud.Factory(self.nowPlaying)
171
+        self.handle = self.device.play(self.factory)
172
+        self.setTag()

+ 12
- 0
active_camera_loc.py View File

@@ -0,0 +1,12 @@
1
+import bge
2
+
3
+
4
+def set(cont):
5
+    own = cont.owner
6
+
7
+    #own.getWorldPosition
8
+    scene = bge.logic.getCurrentScene()
9
+    cam = scene.active_camera
10
+    own.worldPosition = cam.worldPosition
11
+
12
+# dont call main(bge.logic.getCurrentController()), the py controller will

+ 19
- 0
curves.osl View File

@@ -0,0 +1,19 @@
1
+
2
+
3
+uniform sampler2D bgl_RenderedTexture;
4
+
5
+uniform float BC_BRIGHTNESS = 2;
6
+uniform float BC_CONTRAST = 1;
7
+
8
+//const float BC_CONTRAST = 1.3;    //1 is no contrast.
9
+//const float BC_BRIGHTNESS = 1.15;    //1 is no brightness 1.15 is good
10
+
11
+void main()
12
+{
13
+    vec4 color = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
14
+    
15
+    color = ((color-1)*max(BC_CONTRAST,0));
16
+    color = color+BC_BRIGHTNESS;
17
+     
18
+    gl_FragColor = color; 
19
+}

+ 72
- 0
lensflare01.osl View File

@@ -0,0 +1,72 @@
1
+
2
+uniform sampler2D bgl_RenderedTexture;
3
+uniform float bgl_RenderedTextureWidth;
4
+uniform float bgl_RenderedTextureHeight;
5
+uniform float sunX;
6
+uniform float sunY;
7
+uniform float timer;
8
+uniform float sundirect;
9
+uniform float sunScale; 
10
+
11
+float noise( float n )
12
+{
13
+     return fract(sin(n+timer/2.0));
14
+}
15
+
16
+//float noise2(vec2 co){
17
+    //return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
18
+//}
19
+
20
+vec3 lensflare(vec2 uv,vec2 pos,float sunscale)
21
+{
22
+	vec2 main = uv-pos;
23
+	vec2 uvd = uv*(length(uv));
24
+	
25
+	float ang = atan(main.y, main.x);
26
+	float dist=length(main); dist = pow(dist,.1);
27
+	float n = noise1(vec2((ang-timer/9.0)*16.0,dist*32.0));
28
+	
29
+	float f0 = 1.0/(length(uv-pos)/sunscale+1.0);
30
+	
31
+	f0 = f0+f0*(sin((ang+timer/18.0 + noise1(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8);
32
+
33
+	float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;
34
+	float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;
35
+	float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;
36
+	
37
+	vec2 uvx = mix(uv,uvd,-0.5);
38
+	
39
+	float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),.0)*6.0;
40
+	float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),.0)*5.0;
41
+	float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),.0)*3.0;
42
+	
43
+	uvx = mix(uv,uvd,-.4);
44
+	
45
+	float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),.0)*2.0;
46
+	float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),.0)*2.0;
47
+	float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),.0)*2.0;
48
+	
49
+	uvx = mix(uv,uvd,-0.5);
50
+	
51
+	float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),.0)*6.0;
52
+	float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),.0)*3.0;
53
+	float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),.0)*5.0;
54
+	
55
+	vec3 c = vec3(.0);
56
+	
57
+	c.r+=f2+f4+f5+f6; c.g+=f22+f42+f52+f62; c.b+=f23+f43+f53+f63;
58
+	c+=vec3(f0);
59
+	
60
+	return c;
61
+}
62
+
63
+void main()
64
+{
65
+    vec4 renderedTex = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
66
+    float xconv = bgl_RenderedTextureWidth / bgl_RenderedTextureHeight;
67
+    vec2 sunPos = vec2((sunX - 0.5) * xconv, sunY - 0.5);
68
+    vec2 uv = vec2((gl_TexCoord[0].s - 0.5) * xconv, gl_TexCoord[0].t - 0.5);
69
+    vec3 lens = lensflare(uv, sunPos, sunScale);
70
+    renderedTex.rgb += lens * sundirect;
71
+    gl_FragColor = renderedTex;
72
+}

Loading…
Cancel
Save