Exemple #1
0
class AudPlayer:
    def __init__(self, view):
        self._server = pyo.Server().boot()
        self._data = []
        self._state = "STOP"
        self._model = Song()
        self._view = view

    def _reload(self):
        voices = self._model.voices()
        section = self._model.selected_section()

        self._data = []
        for voice in voices:
            entry = self._view.slice_data(section, voice)
            ev = pyo.Events(instr=entry["instrument"],
                            midinote=entry["pitch"],
                            beat=entry["duration"],
                            bpm=entry["speed"],
                            amp=entry["volume"]).play()
            self._data.append(ev)

    def play_pause(self):
        if self._state == "PLAY":
            self.pause()
        else:
            if self._state != "PAUSE":
                self._reload()
            self.play()

    def play(self):
        self._state = "PLAY"
        self._server.start()

    def pause(self):
        self._state = "PAUSE"
        self._server.stop()

    def stop(self):
        if self._state == "PLAY":
            self._server.stop()
        for voice in self._data:
            voice.stop(0)
        self._state = "STOP"
Exemple #2
0
def load_slice(section_name, voice_name):
    result = {}
    result["selectors"] = extract_selectors(Song().scale())
    section_slice = Song().slice(section_name, voice_name)

    notes = section_slice.notes()
    result["speed"] = section_slice.speed()
    result["divisions"] = section_slice.divisions()
    result["length"] = sum(note.duration() for note in notes)
    result["notes"] = extract_notes(notes)

    return result
Exemple #3
0
def extract_notes(slice_notes):
    row = 0
    result = {}
    for note in slice_notes:
        note_value = {}
        if note.is_empty():
            row += note.duration()
            continue
        elif note.is_silent():
            note_value["pitch"] = SILENT_PITCH
        else:
            degree = note.degree()
            note_value["pitch"] = Song().scale().pitch_name(degree)
            note_value["octave"] = "{:01x}".format(note.octave())
            note_value["volume"] = "{:02x}".format(note.volume())
        result[row] = note_value
        row += note.duration()

    return result
Exemple #4
0
    def _create(self, section, voice):
        entry = {}

        # Instrument (only one choice here)
        entry["instrument"] = SquareWave

        # Slice (needed for later stuff
        sec_slice = self._model.slice(section, voice)

        # Speed (in bpm)
        entry["speed"] = sec_slice.speed()

        # Notes (for the rest of the stuff needed)
        notes = sec_slice.notes()

        # (midinote, beat, amp) triple for each note
        data = []
        cur_data = [-1, 0, 0]
        for note in notes:
            if note.is_empty():
                cur_data[1] += note.duration()
            elif note.is_silent():
                if cur_data[2] == 0:
                    cur_data[1] += note.duration()
                else:
                    data.append(cur_data)
                    cur_data = [-1, note.duration(), 0]
            else:
                if cur_data[1] > 0:
                    data.append(cur_data)
                degree = note.degree()
                octave = note.octave()
                midinote = Song().scale().pitch_midi(degree, octave)
                cur_data = [midinote, note.duration(), note.volume() / 256]
                pass
        if cur_data[1] > 0:
            data.append(cur_data)

        # Convert to midinote, beat, amp lists
        entry["pitch"], entry["duration"], entry["volume"] = zip(*data)

        self._data[section][voice] = entry
Exemple #5
0
 def __init__(self):
     EventHandler.__init__(self)
     self._model = Song()
     self.reset()
Exemple #6
0
class AudView(EventHandler):
    def __init__(self):
        EventHandler.__init__(self)
        self._model = Song()
        self.reset()

    def _create(self, section, voice):
        entry = {}

        # Instrument (only one choice here)
        entry["instrument"] = SquareWave

        # Slice (needed for later stuff
        sec_slice = self._model.slice(section, voice)

        # Speed (in bpm)
        entry["speed"] = sec_slice.speed()

        # Notes (for the rest of the stuff needed)
        notes = sec_slice.notes()

        # (midinote, beat, amp) triple for each note
        data = []
        cur_data = [-1, 0, 0]
        for note in notes:
            if note.is_empty():
                cur_data[1] += note.duration()
            elif note.is_silent():
                if cur_data[2] == 0:
                    cur_data[1] += note.duration()
                else:
                    data.append(cur_data)
                    cur_data = [-1, note.duration(), 0]
            else:
                if cur_data[1] > 0:
                    data.append(cur_data)
                degree = note.degree()
                octave = note.octave()
                midinote = Song().scale().pitch_midi(degree, octave)
                cur_data = [midinote, note.duration(), note.volume() / 256]
                pass
        if cur_data[1] > 0:
            data.append(cur_data)

        # Convert to midinote, beat, amp lists
        entry["pitch"], entry["duration"], entry["volume"] = zip(*data)

        self._data[section][voice] = entry

    def handle_event(self, event: Event):
        if event.type() == "MODEL_UPDATE":
            self._create(**event.values()[0])

    def reset(self):
        sections = self._model.sections()
        voices = self._model.voices()
        self._data = {}
        for section in sections:
            self._data[section] = {}
            for voice in voices:
                self._create(section=section, voice=voice)

    def slice_data(self, section, voice):
        return self._data[section][voice]
Exemple #7
0
    def __init__(self):
        self._data = {"section": Song().selected_section()}

        print(self._data)
Exemple #8
0
 def __init__(self, view):
     self._server = pyo.Server().boot()
     self._data = []
     self._state = "STOP"
     self._model = Song()
     self._view = view