PageRenderTime 46ms CodeModel.GetById 36ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/IronPython/Samples/IronTunes/Player.py

https://github.com/rickardraysearch/main
Python | 188 lines | 164 code | 8 blank | 16 comment | 0 complexity | 5f59dd896a211fd82b46750ba0c97f4e MD5 | raw file
  1#####################################################################################
  2#
  3#  Copyright (c) Microsoft Corporation. All rights reserved.
  4#
  5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6# copy of the license can be found in the License.html file at the root of this distribution. If 
  7# you cannot locate the  Apache License, Version 2.0, please send an email to 
  8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9# by the terms of the Apache License, Version 2.0.
 10#
 11# You must not remove this notice, or any other, from this software.
 12#
 13#
 14#####################################################################################
 15
 16import clr
 17
 18clr.AddReferenceToFile("QuartzTypeLib.dll")
 19
 20import QuartzTypeLib
 21import System.Math
 22
 23from System.ComponentModel import INotifyPropertyChanged
 24from System.ComponentModel import PropertyChangedEventArgs
 25
 26#define for window message to tell us there are filter graph events
 27WM_GRAPHNOTIFY = 0x8001
 28
 29#event code that indicates the file finished playing
 30EC_COMPLETE = 1
 31
 32class Player(INotifyPropertyChanged):
 33    "class that handles the playing operations"
 34    
 35    def __init__(self,app,dataModel):
 36        self.graphManager = QuartzTypeLib.FilgraphManagerClass()
 37        self.__filename = ""
 38        self.__app = app
 39        self.__selectedItem = None
 40        self.__playingItem = None
 41        self.__dataModel = dataModel
 42        self.__volume = 100
 43        self._events = []
 44    
 45    def add_PropertyChanged(self, value):
 46        self._events.append(value)
 47
 48    def remove_PropertyChanged(self, value):
 49        self._events.remove(value)
 50
 51    def NotifyPropertyChanged(self, info):
 52        for x in self._events:
 53            x(self,PropertyChangedEventArgs(info))
 54        
 55    def setupEvents(self):
 56        if (hasattr(self.__dataModel,"windowHandle")):
 57            try:
 58                QIHelper.SetNotifyWindowForMe(self.graphManager,self.__dataModel.windowHandle.ToInt32(),WM_GRAPHNOTIFY,0)
 59            except:
 60                pass
 61    
 62    def LoadFile(self, filename):
 63        self.graphManager = QuartzTypeLib.FilgraphManagerClass()
 64        self.graphManager.RenderFile(filename)
 65        self.setVolume(self.__volume) #we need to reset the volume in to the new graph manager
 66        self.setupEvents()
 67        self.filename = filename
 68    
 69    def Play(self):
 70        self.graphManager.Run()
 71    
 72    def Pause(self):
 73        self.graphManager.Pause()
 74        
 75    def Stop(self):
 76        self.graphManager.Stop()
 77        
 78    def getPosition(self):
 79        return self.graphManager.CurrentPosition
 80        
 81    def setPosition(self, newPosition):
 82        self.graphManager.CurrentPosition = newPosition
 83
 84        
 85    def PlayPause(self):
 86        state = self.graphManager.GetState(10)
 87        if (state == 2): # playing
 88            self.Pause()
 89        elif (state == 1): # paused
 90            self.Play()
 91        elif (state == 0): # stopped
 92            if ((self.SelectedItem != None) and (self.filename != self.SelectedItem.Path)):
 93                self.Stop()
 94                self.PlayingItem = self.SelectedItem
 95                self.LoadFile(self.SelectedItem.Path)
 96                self.Play()
 97            else:
 98                self.Play()
 99        else:
100            pass  #for now just do nothing
101        self.NotifyPropertyChanged("IsPlaying")
102        self.NotifyPropertyChanged("Duration")
103
104    def ProcessEvents(self):
105        while (True):
106            eventCode,param1,param2 = self.graphManager.GetEvent(10) #raises exception when no events are left
107            if (eventCode == EC_COMPLETE):
108                self.PlayingIndex += 1
109            
110    def Tick(self):
111        self.NotifyPropertyChanged("Position")
112        
113    def LibraryMouseDown(self,eventargs):
114        if (eventargs.ClickCount == 2):
115            if (self.selectedItem != None):
116                self.Stop()
117                self.RenderFile(self.selectedItem.Path)
118                self.Play()
119    
120    def getFilename(self):
121        return self.__filename
122        
123    def setFilename(self, value):
124        self.__filename = value
125        self.NotifyPropertyChanged("filename")
126        
127    def getVolume(self):
128        return self.__volume
129        
130    def setVolume(self,value):
131        self.__volume = value
132        newVal = int((value - 100.0)*25) #clipping the bottom 3/4 of the range since it's basically unhearable
133        if (newVal < -2499):
134            newVal = -10000
135        self.graphManager.Volume = newVal
136        self.NotifyPropertyChanged("Volume")
137        
138    def getSelectedItem(self):
139        return self.__selectedItem
140        
141    def setSelectedItem(self,value):
142        self.__selectedItem = value
143        self.NotifyPropertyChanged("SelectedItem")
144        
145    def getSelectedIndex(self):
146        return self.__dataModel.library.ViewIndexOf(self.SelectedItem)
147        
148    def setSelectedIndex(self,value):
149        self.SelectedItem = self.__dataModel.library.ViewGetValue(value)
150        self.NotifyPropertyChanged("SelectedIndex")
151        
152    def setPlayingItem(self,value):
153        if value:
154            self.Stop()
155            self.__playingItem = value
156            self.LoadFile(value.Path)
157            self.Play()
158            self.SelectedItem = self.PlayingItem
159            self.NotifyPropertyChanged("PlayingItem")
160        
161    def getPlayingItem(self):
162        return self.__playingItem
163    
164    def setPlayingIndex(self,value):
165        self.PlayingItem = self.__dataModel.library.ViewGetValue(value)
166        self.NotifyPropertyChanged("PlayingIndex")
167        
168    def getPlayingIndex(self):
169        return self.__dataModel.library.ViewIndexOf(self.PlayingItem)
170        
171    def isPlaying(self):
172        return (self.graphManager.GetState(10) == 2)
173        
174    def getDuration(self):
175        try:
176            return self.graphManager.StopTime
177        except:
178            return 0
179        
180Player.filename = property(Player.getFilename, Player.setFilename)
181Player.volume = property(Player.getVolume, Player.setVolume)
182Player.SelectedItem = property(Player.getSelectedItem, Player.setSelectedItem)
183Player.SelectedIndex = property(Player.getSelectedIndex, Player.setSelectedIndex)
184Player.PlayingItem = property(Player.getPlayingItem, Player.setPlayingItem)
185Player.PlayingIndex = property(Player.getPlayingIndex, Player.setPlayingIndex)
186Player.IsPlaying = property(Player.isPlaying)
187Player.Duration = property(Player.getDuration)
188Player.Position = property(Player.getPosition,Player.setPosition)