-
Notifications
You must be signed in to change notification settings - Fork 11
/
audio_player.py
187 lines (137 loc) · 6.52 KB
/
audio_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from enum import Enum
from juce_init import START_JUCE_COMPONENT
import popsicle as juce
class TransportState(Enum):
Stopped = 0
Starting = 1
Playing = 2
Stopping = 3
class AudioSource(juce.AudioSource):
transportSource = juce.AudioTransportSource()
hasReader = juce.Atomic[bool](False)
def __init__(self):
juce.AudioSource.__init__(self)
def prepareToPlay(self, samplesPerBlockExpected, sampleRate):
self.transportSource.prepareToPlay(samplesPerBlockExpected, sampleRate)
def getNextAudioBlock(self, bufferToFill):
if not self.hasReader.get():
bufferToFill.clearActiveBufferRegion()
else:
self.transportSource.getNextAudioBlock(bufferToFill)
def releaseResources(self):
self.transportSource.releaseResources()
class MainContentComponent(juce.Component, juce.ChangeListener, juce.Timer):
openButton = juce.TextButton()
playButton = juce.TextButton()
stopButton = juce.TextButton()
loopingToggle = juce.ToggleButton()
currentPositionLabel = juce.Label()
audioSource = AudioSource()
audioSourcePlayer = juce.AudioSourcePlayer()
readerSource = None
isLooping = False
deviceManager = juce.AudioDeviceManager()
formatManager = juce.AudioFormatManager()
state = TransportState.Stopped
def __init__(self):
juce.Component.__init__(self)
juce.ChangeListener.__init__(self)
juce.Timer.__init__(self)
self.addAndMakeVisible(self.openButton)
self.openButton.setButtonText("Open...")
self.openButton.onClick = self.openButtonClicked
self.addAndMakeVisible (self.playButton)
self.playButton.setButtonText ("Play")
self.playButton.onClick = self.playButtonClicked
self.playButton.setColour(juce.TextButton.buttonColourId, juce.Colours.green)
self.playButton.setEnabled(False)
self.addAndMakeVisible(self.stopButton)
self.stopButton.setButtonText("Stop")
self.stopButton.onClick = self.stopButtonClicked
self.stopButton.setColour(juce.TextButton.buttonColourId, juce.Colours.red)
self.stopButton.setEnabled(False)
self.addAndMakeVisible(self.loopingToggle)
self.loopingToggle.setButtonText("Loop")
self.loopingToggle.onClick = self.loopButtonChanged
self.addAndMakeVisible(self.currentPositionLabel)
self.currentPositionLabel.setText("Stopped", juce.dontSendNotification)
self.formatManager.registerBasicFormats()
self.audioSource.transportSource.addChangeListener(self)
self.deviceManager.initialise(0, 2, None, True)
self.deviceManager.addAudioCallback(self.audioSourcePlayer)
self.audioSourcePlayer.setSource(self.audioSource)
self.setSize(400, 400)
self.startTimer(20)
def visibilityChanged(self):
if self.isVisible() or not self.deviceManager:
return
self.audioSource.hasReader.set(False)
self.deviceManager.removeAudioCallback(self.audioSourcePlayer)
self.audioSource.transportSource.setSource(None)
self.audioSourcePlayer.setSource(None)
self.deviceManager.closeAudioDevice()
def resized(self):
self.openButton.setBounds(10, 10, self.getWidth() - 20, 20)
self.playButton.setBounds(10, 40, self.getWidth() - 20, 20)
self.stopButton.setBounds(10, 70, self.getWidth() - 20, 20)
self.loopingToggle.setBounds(10, 100, self.getWidth() - 20, 20)
self.currentPositionLabel.setBounds(10, 130, self.getWidth() - 20, 20)
def changeListenerCallback(self, source):
print("changeListenerCallback", source, self.audioSource.transportSource)
if source == self.audioSource.transportSource:
if self.audioSource.transportSource.isPlaying():
self.changeState(TransportState.Playing)
else:
self.changeState(TransportState.Stopped)
def timerCallback(self):
if self.audioSource.transportSource.isPlaying():
position = juce.RelativeTime(self.audioSource.transportSource.getCurrentPosition())
minutes = int(position.inMinutes()) % 60
seconds = int(position.inSeconds()) % 60
millis = int(position.inMilliseconds()) % 1000
positionString = "{:02d}:{:02d}:{:03d}".format(minutes, seconds, millis)
self.currentPositionLabel.setText(positionString, juce.dontSendNotification)
else:
self.currentPositionLabel.setText("Stopped", juce.dontSendNotification)
def updateLoopState(self, shouldLoop):
self.isLooping = shouldLoop
if self.readerSource:
self.readerSource.setLooping(self.isLooping)
def changeState (self, newState):
if self.state == newState:
return
self.state = newState
print(self.state)
if self.state == TransportState.Stopped:
self.stopButton.setEnabled(False)
self.playButton.setEnabled(True)
self.audioSource.transportSource.setPosition(0.0)
elif self.state == TransportState.Starting:
self.playButton.setEnabled(False)
self.audioSource.transportSource.start()
elif self.state == TransportState.Playing:
self.stopButton.setEnabled(True)
elif self.state == TransportState.Stopping:
self.audioSource.transportSource.stop()
def openButtonClicked(self):
chooser = juce.FileChooser("Select a Wave file to play...", juce.File(), "*.wav")
if chooser.browseForFileToOpen():
self.reader = self.formatManager.createReaderFor(chooser.getResult())
if self.reader:
self.readerSource = juce.AudioFormatReaderSource(self.reader, False)
if self.isLooping:
self.readerSource.setLooping(self.isLooping)
self.audioSource.transportSource.setSource(self.readerSource, 0, None, self.reader.sampleRate, 2)
self.audioSource.hasReader.set(True)
self.playButton.setEnabled(True)
else:
self.audioSource.hasReader.set(False)
def playButtonClicked(self):
self.updateLoopState(self.loopingToggle.getToggleState())
self.changeState(TransportState.Starting)
def stopButtonClicked(self):
self.changeState(TransportState.Stopping)
def loopButtonChanged(self):
self.updateLoopState(self.loopingToggle.getToggleState())
if __name__ == "__main__":
START_JUCE_COMPONENT(MainContentComponent, name="Audio Player")