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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
# -*- coding: utf-8 -*-
"""
Copyright 2010 Olivier Belanger
This file is part of pyo, a python module to help digital signal
processing script creation.
pyo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pyo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyo. If not, see <http://www.gnu.org/licenses/>.
"""
from _core import *
from _widgets import createServerGUI
######################################################################
### Proxy of Server object
######################################################################
class Server(object):
"""
Main processing audio loop callback handler.
The Server object handles all communications with Portaudio and
Portmidi. It keeps track of all audio streams created as well as
connections between them.
An instance of the Server must be booted before defining any
signal processing chain.
Parameters:
sr : int, optional
Sampling rate used by Portaudio and the Server to compute samples.
Defaults to 44100.
nchnls : int, optional
Number of input and output channels. Defaults to 2.
buffersize : int, optional
Number of samples that Portaudio will request from the callback loop.
This value has an impact on CPU use (a small buffer size is harder
to compute) and on the latency of the system. Latency is
`buffer size / sampling rate` in seconds. Defaults to 256.
duplex : int {0, 1}, optional
Input - output mode. 0 is output only and 1 is both ways.
Defaults to 1.
audio : string {'portaudio', 'pa', 'jack', 'coreaudio', 'offline'}, optional
Audio backend to use. 'pa' is equivalent to 'portaudio'.
Default is 'portaudio'.
jackname : string, optional
Name of jack client. Defaults to 'pyo'
Methods:
setAmp(x) : Set the overall amplitude.
boot() : Boot the server. Must be called before defining any signal
processing chain.
shutdown() : Shut down and clear the server.
setStartOffset(x) : Set the starting time of the real-time processing.
setGlobalSeed(x) : Set the server's global seed used by random objects.
start() : Start the audio callback loop.
stop() : Stop the audio callback loop.
gui(locals, meter, timer) : Show the server's user interface.
recordOptions(dur, filename, fileformat, sampletype) : Rendering settings.
recstart(str) : Begins recording of the sound sent to output.
This method creates a file called `pyo_rec.aif` in the
user's home directory if a path is not supplied.
recstop() : Stops previously started recording.
getSamplingRate() : Returns the current sampling rate.
getNchnls() : Returns the current number of channels.
getBufferSize() : Returns the current buffer size.
getGlobalSeed() : Returns the server's global seed.
getIsStarted() : Returns 1 if the server is started, otherwise returns 0.
getMidiActive() : Returns 1 if Midi callback is active, otherwise returns 0.
The next methods must be called before booting the server
setInOutDevice(x) : Set both input and output devices. See `pa_list_devices()`.
setInputDevice(x) : Set the audio input device number. See `pa_list_devices()`.
setOutputDevice(x) : Set the audio output device number. See `pa_list_devices()`.
setMidiInputDevice(x) : Set the MIDI input device number. See `pm_list_devices()`.
setSamplingRate(x) : Set the sampling rate used by the server.
setBufferSize(x) : Set the buffer size used by the server.
setNchnls(x) : Set the number of channels used by the server.
setDuplex(x) : Set the duplex mode used by the server.
setVerbosity(x) : Set the server's verbosity.
reinit(sr, nchnls, buffersize, duplex, audio, jackname) : Reinit the server's settings.
Attributes:
amp : Overall amplitude of the Server. This value is applied on any
stream sent to the soundcard.
verbosity : Control the messages printed by the server. It is a sum of
values to display different levels: 1 = error, 2 = message,
4 = warning , 8 = debug.
startoffset : Starting time of the real-time processing.
globalseed : Global seed used by random objects. Defaults to 0 (means always seed from the system clock).
Examples:
>>> # For an 8 channels server in duplex mode with
>>> # a sampling rate of 48000 Hz and buffer size of 512
>>> s = Server(sr=48000, nchnls=8, buffersize=512, duplex=1).boot()
>>> s.start()
"""
def __init__(self, sr=44100, nchnls=2, buffersize=256, duplex=1, audio='portaudio', jackname='pyo'):
self._nchnls = nchnls
self._amp = 1.
self._verbosity = 7
self._startoffset = 0
self._dur = -1
self._filename = None
self._fileformat = 0
self._sampletype = 0
self._server = Server_base(sr, nchnls, buffersize, duplex, audio, jackname)
def reinit(self, sr=44100, nchnls=2, buffersize=256, duplex=1, audio='portaudio', jackname='pyo'):
"""
Reinit the server'settings. Useful to alternate between real-time and offline server.
Parameters:
Same as in the __init__ method.
"""
self._nchnls = nchnls
self._amp = 1.
self._verbosity = 7
self._startoffset = 0
self._dur = -1
self._filename = None
self._fileformat = 0
self._sampletype = 0
self._globalseed = 0
self._server.__init__(sr, nchnls, buffersize, duplex, audio, jackname)
def gui(self, locals=None, meter=True, timer=True):
"""
Show the server's user interface.
Parameters:
locals : locals namespace {locals(), None}, optional
If locals() is given, the interface will show an interpreter extension,
giving a way to interact with the running script. Defaults to None.
meter : boolean, optinal
If True, the interface will show a vumeter of the global output signal.
Defaults to True.
timer : boolean, optional
If True, the interface will show a clock of the current time.
Defaults to True.
"""
f, win = createServerGUI(self._nchnls, self.start, self.stop, self.recstart, self.recstop,
self.setAmp, self.getIsStarted(), locals, self.shutdown, meter, timer, self._amp)
if meter:
self._server.setAmpCallable(f)
if timer:
self._server.setTimeCallable(f)
try:
win.mainloop()
except:
if win != None:
win.MainLoop()
def setTimeCallable(self, func):
self.setTime = func
self._server.setTimeCallable(self)
def setInOutDevice(self, x):
"""
Set both input and output audio devices. See `pa_list_devices()`.
Parameters:
x : int
Number of the audio input and output devices.
"""
self._server.setInOutDevice(x)
def setInputDevice(self, x):
"""
Set the audio input device number. See `pa_list_devices()`.
Parameters:
x : int
Number of the audio device listed by Portaudio.
"""
self._server.setInputDevice(x)
def setOutputDevice(self, x):
"""
Set the audio output device number. See `pa_list_devices()`.
Parameters:
x : int
Number of the audio device listed by Portaudio.
"""
self._server.setOutputDevice(x)
def setMidiInputDevice(self, x):
"""
Set the Midi input device number. See `pm_list_devices()`.
Parameters:
x : int
Number of the Midi device listed by Portmidi.
"""
self._server.setMidiInputDevice(x)
def setSamplingRate(self, x):
"""
Set the sampling rate used by the server.
Parameters:
x : int
New sampling rate, must be supported by the soundcard.
"""
self._server.setSamplingRate(x)
def setBufferSize(self, x):
"""
Set the buffer size used by the server.
Parameters:
x : int
New buffer size.
"""
self._server.setBufferSize(x)
def setNchnls(self, x):
"""
Set the number of channels used by the server.
Parameters:
x : int
New number of channels.
"""
self._nchnls = x
self._server.setNchnls(x)
def setDuplex(self, x):
"""
Set the duplex mode used by the server.
Parameters:
x : int {0 or 1}
New mode. 0 is output only, 1 is both ways.
"""
self._server.setDuplex(x)
def setVerbosity(self, x):
"""
Set the server's verbosity.
Parameters:
x : int
A sum of values to display different levels: 1 = error, 2 = message,
4 = warning , 8 = debug.
"""
self._verbosity = x
self._server.setVerbosity(x)
def setGlobalSeed(self, x):
"""
Set the server's global seed used by random objects.
Parameters:
x : int
A positive integer that will be used as the seed by random objects.
If zero, randoms will be seeded with the system clock current value.
"""
self._globalseed = x
self._server.setGlobalSeed(x)
def setStartOffset(self, x):
"""
Set the server's starting time offset. First `x` seconds will be rendered
offline as fast as possible.
Parameters:
x : float
Starting time of the real-time processing.
"""
self._startoffset = x
self._server.setStartOffset(x)
def setAmp(self, x):
"""
Set the overall amplitude.
Parameters:
x : float
New amplitude.
"""
self._amp = x
self._server.setAmp(x)
def shutdown(self):
"""
Shut down and clear the server. This method will erase all objects
from the callback loop. This method need to be called before changing
server's parameters like `samplingrate`, `buffersize`, `nchnls`, ...
"""
self._server.shutdown()
def boot(self):
"""
Boot the server. Must be called before defining any signal processing
chain. Server's parameters like `samplingrate`, `buffersize` or
`nchnls` will be effective after a call to this method.
"""
self._server.boot()
return self
def start(self):
"""
Start the audio callback loop and begin processing.
"""
self._server.start()
return self
def stop(self):
"""
Stop the audio callback loop.
"""
self._server.stop()
def recordOptions(self, dur=-1, filename=None, fileformat=0, sampletype=0):
"""
Sets options for soundfile created by offline rendering or global recording.
Parameters:
dur : float
Duration, in seconds, of the recorded file. Only used by
offline rendering. Must be positive. Defaults to -1.
filename : string
Full path of the file to create. If None, a file called
`pyo_rec.wav` will be created in the user's home directory.
Defaults to None.
fileformat : int, optional
Format type of the audio file. This function will first try to
set the format from the filename extension. If it's not possible,
it uses the fileformat parameter. Defaults to 0.
Supported formats are:
0 : WAV - Microsoft WAV format (little endian) {.wav, .wave}
1 : AIFF - Apple/SGI AIFF format (big endian) {.aif, .aiff}
sampletype : int, optional
Bit depth encoding of the audio file. Defaults to 0.
Supported types are:
0 : 16 bits int
1 : 24 bits int
2 : 32 bits int
3 : 32 bits float
4 : 64 bits float
"""
FORMATS = {'wav': 0, 'wave': 0, 'aif': 1, 'aiff': 1}
self._dur = dur
if filename == None:
filename = os.path.join(os.path.expanduser("~"), "pyo_rec.wav")
self._filename = filename
ext = filename.rsplit('.')
if len(ext) >= 2:
ext = ext[-1].lower()
if FORMATS.has_key(ext):
fileformat = FORMATS[ext]
else:
print 'Warning: Unknown file extension. Using fileformat value.'
else:
print 'Warning: Filename has no extension. Using fileformat value.'
self._fileformat = fileformat
self._sampletype = sampletype
self._server.recordOptions(dur, filename, fileformat, sampletype)
def recstart(self, filename=None):
"""
Begins a default recording of the sound that is sent to the
soundcard. This will create a file called `pyo_rec.wav` in
the user's home directory if no path is supplied or defined
with recordOptions method. Uses file format and sample type
defined with recordOptions method.
Parameters:
filename : string, optional
Name of the file to be created. Defaults to None.
"""
FORMATS = {'wav': 0, 'wave': 0, 'aif': 1, 'aiff': 1}
if filename == None:
if self._filename != None:
filename = self._filename
else:
filename = os.path.join(os.path.expanduser("~"), "pyo_rec.wav")
ext = filename.rsplit('.')
if len(ext) >= 2:
ext = ext[-1].lower()
if FORMATS.has_key(ext):
fileformat = FORMATS[ext]
if fileformat != self._fileformat:
self._fileformat = fileformat
self._server.recordOptions(self._dur, filename, self._fileformat, self._sampletype)
self._server.recstart(filename)
def recstop(self):
"""
Stop the previously started recording.
"""
self._server.recstop()
def getStreams(self):
"""
Return the list of streams loaded in the server.
"""
return self._server.getStreams()
def getSamplingRate(self):
"""
Return the current sampling rate.
"""
return self._server.getSamplingRate()
def getNchnls(self):
"""
Return the current number of channels.
"""
return self._server.getNchnls()
def getBufferSize(self):
"""
Return the current buffer size.
"""
return self._server.getBufferSize()
def getGlobalSeed(self):
"""
Return the current global seed.
"""
return self._server.getGlobalSeed()
def getIsStarted(self):
"""
Returns 1 if the server is started, otherwise returns 0.
"""
return self._server.getIsStarted()
def getMidiActive(self):
"""
Returns 1 if Midi callback is active, otherwise returns 0.
"""
return self._server.getMidiActive()
@property
def amp(self):
"""float. Overall amplitude."""
return self._amp
@amp.setter
def amp(self, x): self.setAmp(x)
@property
def startoffset(self):
"""float. Starting time of the real-time processing."""
return self._startoffset
@startoffset.setter
def startoffset(self, x): self.setStartOffset(x)
@property
def verbosity(self):
"""int. Server verbosity."""
return self._verbosity
@verbosity.setter
def verbosity(self, x):
if (type(x) == int):
self.setVerbosity(x)
else:
raise Exception("verbosity must be an integer")
@property
def globalseed(self):
"""int. Server global seed."""
return self._globalseed
@globalseed.setter
def globalseed(self, x):
if (type(x) == int):
self.setGlobalSeed(x)
else:
raise Exception("global seed must be an integer")
|