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