Beispiel #1
0
class LilypondNotePlayer:

    def __init__(self, beatsPerMinute, beatUnit, tuningSystem, note, samplingRate = 44100):
        self.note = note
        self.volume = 1 # Full colume for starters
        self.samplingRate = samplingRate
        # How many samples will I have to play?
        self.totalSamples = int(beatUnit * samplingRate * 60 / (note.duration * beatsPerMinute))
        if self.note.times != None:
            self.totalSamples /= self.note.times
        if note.dots:
            self.totalSamples = int(self.totalSamples * ( 2.0 - pow(2, - note.dots)))
        self.wave = Wave(tuningSystem.getFrequency(note), samplingRate)
        self.counter = 0
        self.volumeRate = None
        self.tied = False
        
    def getNextValue(self):
        self.counter+=1

        # Do we have to lower the volume?
        if not self.tied and self.totalSamples - self.counter <= self.samplingRate * 0.05: # 5 hundredths of a second
            # Have to calculate a volume rate
            if self.totalSamples <= self.counter:
                self.volumeRate = 0
            else:
                self.volumeRate = math.exp(math.log(0.01) / (self.totalSamples - self.counter))
            sys.stderr.flush()
        if self.volumeRate != None:
            self.wave.setVolume(self.volume * self.volumeRate)

        return self.wave.getNextValue()
    
    def setTied(self, value):
        self.tied = value
    
    def isTied(self):
        return self.tied

    def isFinished(self):
        return self.counter >= self.totalSamples
    
    def setNewDuration(self, totalSamples):
        self.counter = 0
        self.totalSamples = totalSamples
    
    def getAngle(self):
        return self.wave.getAngle()
    
    def setAngle(self, angle):
        self.wave.setAngle(angle)
    
    def getFrequency(self):
        return self.wave.getFrequency()
Beispiel #2
0
class TrackPlayer:
    """
    Will play a note that's provided to it
    """

    def __init__(self, system, samplingRate = 44100):
        self.system = system
        self.wave = None
        self.samplingRate = samplingRate

        self.samplesToMute = None

    def play(self, musicalNote):
        if musicalNote == None:
            self.mute()
        else:
            self.wave = Wave(self.system.getFrequency(musicalNote), self.samplingRate)
            self.samplesToMute = None

    def mute(self, samplesToMute = None):
        if samplesToMute == None:
            # mute inmediately
            self.volume=0
            if self.wave != None:
                self.wave.setVolume(0)
        else:
            self.samplesToMute = samplesToMute
            # What is the rate for each volume decrease to apply?
            self.volume=1
            self.decrementRate = math.exp(math.log(0.01) / samplesToMute)

    def getNextValue(self):
        if self.wave == None:
            return 0
        if self.samplesToMute != None:
            self.samplesToMute -= 1
            # The volume?
            self.volume*=self.decrementRate
            self.wave.setVolume(self.volume)
        return self.wave.getNextValue()

    def getFrequency(self):
        if self.wave == None:
            return None
        else:
            return self.wave.getFrequency()
Beispiel #3
0
class TrackPlayer:
    """
    Will play a note that's provided to it
    """
    def __init__(self, system, samplingRate=44100):
        self.system = system
        self.wave = None
        self.samplingRate = samplingRate

        self.samplesToMute = None

    def play(self, musicalNote):
        if musicalNote == None:
            self.mute()
        else:
            self.wave = Wave(self.system.getFrequency(musicalNote),
                             self.samplingRate)
            self.samplesToMute = None

    def mute(self, samplesToMute=None):
        if samplesToMute == None:
            # mute inmediately
            self.volume = 0
            if self.wave != None:
                self.wave.setVolume(0)
        else:
            self.samplesToMute = samplesToMute
            # What is the rate for each volume decrease to apply?
            self.volume = 1
            self.decrementRate = math.exp(math.log(0.01) / samplesToMute)

    def getNextValue(self):
        if self.wave == None:
            return 0
        if self.samplesToMute != None:
            self.samplesToMute -= 1
            # The volume?
            self.volume *= self.decrementRate
            self.wave.setVolume(self.volume)
        return self.wave.getNextValue()

    def getFrequency(self):
        if self.wave == None:
            return None
        else:
            return self.wave.getFrequency()
Beispiel #4
0
class LilypondNotePlayer:
    def __init__(self,
                 beatsPerMinute,
                 beatUnit,
                 tuningSystem,
                 note,
                 samplingRate=44100):
        self.note = note
        self.volume = 1  # Full colume for starters
        self.samplingRate = samplingRate
        # How many samples will I have to play?
        self.totalSamples = int(beatUnit * samplingRate * 60 /
                                (note.duration * beatsPerMinute))
        if self.note.times != None:
            self.totalSamples /= self.note.times
        if note.dots:
            self.totalSamples = int(self.totalSamples *
                                    (2.0 - pow(2, -note.dots)))
        self.wave = Wave(tuningSystem.getFrequency(note), samplingRate)
        self.counter = 0
        self.volumeRate = None
        self.tied = False

    def getNextValue(self):
        self.counter += 1

        # Do we have to lower the volume?
        if not self.tied and self.totalSamples - self.counter <= self.samplingRate * 0.05:  # 5 hundredths of a second
            # Have to calculate a volume rate
            if self.totalSamples <= self.counter:
                self.volumeRate = 0
            else:
                self.volumeRate = math.exp(
                    math.log(0.01) / (self.totalSamples - self.counter))
            sys.stderr.flush()
        if self.volumeRate != None:
            self.wave.setVolume(self.volume * self.volumeRate)

        return self.wave.getNextValue()

    def setTied(self, value):
        self.tied = value

    def isTied(self):
        return self.tied

    def isFinished(self):
        return self.counter >= self.totalSamples

    def setNewDuration(self, totalSamples):
        self.counter = 0
        self.totalSamples = totalSamples

    def getAngle(self):
        return self.wave.getAngle()

    def setAngle(self, angle):
        self.wave.setAngle(angle)

    def getFrequency(self):
        return self.wave.getFrequency()