| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006 by Patrick Stinson and Jonathan Saggau |
|---|
| 4 | # patrickkidd@gmail.com saggau@gmail.com |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program; if not, write to the |
|---|
| 18 | # Free Software Foundation, Inc., |
|---|
| 19 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 20 | # |
|---|
| 21 | """ |
|---|
| 22 | High level Server class, like the one in sclang. |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | import os |
|---|
| 26 | import socket |
|---|
| 27 | import scosc |
|---|
| 28 | import process |
|---|
| 29 | import pool |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | class _Server(scosc.Controller): |
|---|
| 33 | |
|---|
| 34 | """ Strictly for convenince. This is poorly thought-out and so |
|---|
| 35 | probably bad design. |
|---|
| 36 | """ |
|---|
| 37 | |
|---|
| 38 | proc = None |
|---|
| 39 | |
|---|
| 40 | def __init__(self, addr, verbose=False): |
|---|
| 41 | scosc.Controller.__init__(self, addr, verbose) |
|---|
| 42 | self.audiobuspool = pool.IntPool(29) |
|---|
| 43 | self.controlbuspool = pool.IntPool(0) |
|---|
| 44 | self.synthpool = pool.IntPool(1000) |
|---|
| 45 | self.bufferpool = pool.IntPool(0) |
|---|
| 46 | |
|---|
| 47 | def quit(self): |
|---|
| 48 | self.sendMsg('/quit') |
|---|
| 49 | #self.receive('/done', '/fail') |
|---|
| 50 | |
|---|
| 51 | def kill(self): |
|---|
| 52 | if self.proc: |
|---|
| 53 | self.proc.kill() |
|---|
| 54 | |
|---|
| 55 | def ensure_dead(self): |
|---|
| 56 | if self.proc and self.proc.isAlive(): |
|---|
| 57 | self.kill() |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | def connect(iphost='localhost', port=57110, verbose=False, spew=False): |
|---|
| 61 | ip = socket.gethostbyname(iphost) |
|---|
| 62 | s = _Server((ip, port), verbose) |
|---|
| 63 | s.spew = spew |
|---|
| 64 | #s.sendMsg('/status', 1) |
|---|
| 65 | #s.receive('status.reply') |
|---|
| 66 | return s |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | def start(exe='scsynth', exedir='/Applications/SuperCollider', |
|---|
| 70 | port=57110, |
|---|
| 71 | inputs=2, outputs=2, samplerate=48000, |
|---|
| 72 | verbose=False, spew=False): |
|---|
| 73 | instance = process.start_local(exe, exedir, port, |
|---|
| 74 | inputs, outputs, samplerate, |
|---|
| 75 | verbose) |
|---|
| 76 | import time |
|---|
| 77 | time.sleep(1) |
|---|
| 78 | s = connect('127.0.0.1', 57110, verbose=verbose, spew=spew) |
|---|
| 79 | s.instance = instance |
|---|
| 80 | return s |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | def test_start(): |
|---|
| 84 | import time |
|---|
| 85 | s = start('scsynth', os.getcwd(), 2, 2, 48000) |
|---|
| 86 | time.sleep(1) |
|---|
| 87 | s.quit() |
|---|
| 88 | |
|---|
| 89 | def test_connect(): |
|---|
| 90 | s = connect('127.0.0.1', 57110) |
|---|
| 91 | s.quit() |
|---|
| 92 | |
|---|
| 93 | if __name__ == '__main__': |
|---|
| 94 | test_start() |
|---|
| 95 | #test_connect() |
|---|