diff options
author | James Cameron <quozl@laptop.org> | 2010-12-23 23:51:39 (GMT) |
---|---|---|
committer | James Cameron <quozl@laptop.org> | 2010-12-23 23:51:39 (GMT) |
commit | fc634f2bc1ef52382807fa4f1d8e8ee8a160cb9e (patch) | |
tree | c91042d29d12e1449dfc96415d76f0633cbe311d /library/pippy/sound.py | |
parent | 2e5d498830016447eac72f88cf2d6a592da737a8 (diff) |
pep8 adjustments
- excluded examples because we don't have consensus,
- excluded physics since this is a copy of an upstream,
- excluded query.py since this code does not currently work.
Co-authored-by: Nitin Gupta <gupta.nitingupta.nitin@gmail.com>
Tested-by: James Cameron <quozl@laptop.org>
Diffstat (limited to 'library/pippy/sound.py')
-rwxr-xr-x | library/pippy/sound.py | 251 |
1 files changed, 175 insertions, 76 deletions
diff --git a/library/pippy/sound.py b/library/pippy/sound.py index d0c78b5..c5f864d 100755 --- a/library/pippy/sound.py +++ b/library/pippy/sound.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - import os import sys from gettext import gettext as _ @@ -29,18 +28,21 @@ fnum = [100] """XXX: This function seems to be broken. (CSA) def quit(self): perf.Stop() - perf.Join() + perf.Join() cs.Reset() - cs = None + cs = None """ + def finddir(): for d in dirs: if os.path.isdir(d): return d + def defAdsr(attack=0.01, decay=0.1, sustain=0.8, release=0.1): - """Define an ADSR envelope. fnum = defADSR(attack = [0.01], decay = [0.1], sustain = [0.8], release = [0.1])""" + """ Define an ADSR envelope. fnum = defADSR(attack = [0.01], + decay = [0.1], sustain = [0.8], release = [0.1]) """ att = int(2048 * attack) dec = int(2048 * decay) rel = int(2048 * release) @@ -48,11 +50,15 @@ def defAdsr(attack=0.01, decay=0.1, sustain=0.8, release=0.1): sus = min(1., sustain) fnum[0] += 1 - scorelines.append("f%ld 0 2048 7 0 %ld 1. %ld %f %ld %f %ld 0\n" % (fnum[0], att, dec, sus, bal, sus, rel)) + scorelines.append("f%ld 0 2048 7 0 %ld 1. %ld %f %ld %f %ld 0\n" % + (fnum[0], att, dec, sus, bal, sus, rel)) return fnum[0] -def defLineSegments(list=[0,10,1,10,0,10,1,10,0]): - """Define a breakpoints envelope. list=[0,10,1,10,0,10,1,10,0]. list begin with the start value of the function and is follow by any pair values (duration, value). The number of elements in the list should odd.""" + +def defLineSegments(list=[0, 10, 1, 10, 0, 10, 1, 10, 0]): + """ Define a breakpoints envelope. list begin with the start + value of the function and is follow by any pair values (duration, + value). The number of elements in the list should be odd. """ totalLength = 0 newlist = [] @@ -61,44 +67,79 @@ def defLineSegments(list=[0,10,1,10,0,10,1,10,0]): totalLength += list[i] for i in range(len(list)): - if (i % 2) == 0: newlist.append(list[i]) - else: newlist.append(int(2048 * (list[i] / float(totalLength)))) + if (i % 2) == 0: + newlist.append(list[i]) + else: + newlist.append(int(2048 * (list[i] / float(totalLength)))) fnum[0] += 1 - scorelines.append("f" + str(fnum[0]) + " 0 2048 -7 " + " ".join([str(n) for n in newlist]) + '\n') + scorelines.append("f" + str(fnum[0]) + " 0 2048 -7 " + + " ".join([str(n) for n in newlist]) + '\n') return fnum[0] -def defComplexWave(list=[1,0,0,.3,0,.2,0,0,.1]): - """Define a complex waveform to be read with 'playComplex' function. list=[1,0,0,.3,0,.2,0,0,.1] -is a list of amplitude for succesive harmonics of a waveform""" + +def defComplexWave(list=[1, 0, 0, .3, 0, .2, 0, 0, .1]): + """ Define a complex waveform to be read with 'playComplex' + function. list=[1, 0, 0, .3, 0, .2, 0, 0, .1] is a list of + amplitude for succesive harmonics of a waveform """ fnum[0] += 1 - scorelines.append("f" + str(fnum[0]) + " 0 2048 10 " + " ".join([str(n) for n in list]) + '\n') + scorelines.append("f" + str(fnum[0]) + " 0 2048 10 " + + " ".join([str(n) for n in list]) + '\n') return fnum[0] -def playSine( pitch=1000, amplitude=5000, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default'): - """Play a sine wave (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], pitch_envelope=['default'], amplitude_envelope=['default'])""" - _play(pitch, amplitude, duration, starttime, pitch_envelope, amplitude_envelope, 1) - -def playSquare( pitch=1000, amplitude=5000, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default'): - """Play a square wave (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], pitch_envelope=['default'], amplitude_envelope=['default'])""" - _play(pitch, amplitude, duration, starttime, pitch_envelope, amplitude_envelope, 2) -def playSawtooth( pitch=1000, amplitude=5000, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default'): - """Play a sawtooth wave (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], pitch_envelope=['default'], amplitude_envelope=['default'])""" - _play(pitch, amplitude, duration, starttime, pitch_envelope, amplitude_envelope, 3) +def playSine(pitch=1000, amplitude=5000, duration=1, starttime=0, + pitch_envelope='default', amplitude_envelope='default'): + """ Play a sine wave + (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], + pitch_envelope=['default'], amplitude_envelope=['default']) """ + _play(pitch, amplitude, duration, starttime, pitch_envelope, + amplitude_envelope, 1) + + +def playSquare(pitch=1000, amplitude=5000, duration=1, starttime=0, + pitch_envelope='default', amplitude_envelope='default'): + """ Play a square wave + (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], + pitch_envelope=['default'], amplitude_envelope=['default']) """ + _play(pitch, amplitude, duration, starttime, pitch_envelope, + amplitude_envelope, 2) + + +def playSawtooth(pitch=1000, amplitude=5000, duration=1, starttime=0, + pitch_envelope='default', amplitude_envelope='default'): + """ Play a sawtooth wave (pitch = [1000], amplitude = [5000], + duration = [1], starttime = [0], pitch_envelope=['default'], + amplitude_envelope=['default']) """ + _play(pitch, amplitude, duration, starttime, pitch_envelope, + amplitude_envelope, 3) + + +def playComplex(pitch=1000, amplitude=5000, duration=1, starttime=0, + pitch_envelope='default', amplitude_envelope='default', + wave='default'): + """ Play a complex wave + (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], + pitch_envelope = ['default'], amplitude_envelope, wave = ['default'] ) """ + if wave == 'default': + wavetable = 10 + else: + wavetable = wave + _play(pitch, amplitude, duration, starttime, pitch_envelope, + amplitude_envelope, wavetable) -def playComplex( pitch=1000, amplitude=5000, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default', wave='default'): - """Play a complex wave (pitch = [1000], amplitude = [5000], duration = [1], starttime = [0], pitch_envelope = ['default'], amplitude_envelope, wave = ['default'] )""" - if wave == 'default': wavetable = 10 - else: wavetable = wave - _play(pitch, amplitude, duration, starttime, pitch_envelope, amplitude_envelope, wavetable) -def _play( pitch, amplitude, duration, starttime, pitch_envelope, amplitude_envelope, instrument): - if pitch_envelope == 'default': pitenv = 99 - else: pitenv = pitch_envelope +def _play(pitch, amplitude, duration, starttime, pitch_envelope, + amplitude_envelope, instrument): + if pitch_envelope == 'default': + pitenv = 99 + else: + pitenv = pitch_envelope - if amplitude_envelope == 'default': ampenv = 100 - else: ampenv = amplitude_envelope + if amplitude_envelope == 'default': + ampenv = 100 + else: + ampenv = amplitude_envelope if not 1 in instrlist: orchlines.append("instr 1\n") @@ -109,27 +150,54 @@ def _play( pitch, amplitude, duration, starttime, pitch_envelope, amplitude_enve orchlines.append("endin\n\n") instrlist.append(1) - scorelines.append("i1 %s %s %s %s %s %s %s\n" % (str(starttime), str(duration), str(pitch), str(amplitude), str(pitenv), str(ampenv), str(instrument))) - -def playFrequencyModulation( pitch=500, amplitude=5000, duration=2, starttime=0, carrier=1, modulator=.5, index=5, pitch_envelope='default', amplitude_envelope='default', carrier_envelope='default', modulator_envelope='default', index_envelope='default', wave='default'): - """Play a frequency modulation synthesis sound (pitch = [100], amplitude = [5000], duration = [2], starttime = [0], carrier = [1], modulator = [.5], index = [5], pitch_envelope = ['default'], amplitude_envelope = ['default'], carrier_envelope = ['default'], modulator_envelope = ['default'], index_envelope = ['default'], wave = ['default'] )""" - if pitch_envelope == 'default': pitenv = 99 - else: pitenv = pitch_envelope + scorelines.append("i1 %s %s %s %s %s %s %s\n" % + (str(starttime), str(duration), str(pitch), + str(amplitude), str(pitenv), str(ampenv), + str(instrument))) + + +def playFrequencyModulation(pitch=500, amplitude=5000, duration=2, starttime=0, + carrier=1, modulator=.5, index=5, + pitch_envelope='default', + amplitude_envelope='default', + carrier_envelope='default', + modulator_envelope='default', + index_envelope='default', wave='default'): + """ Play a frequency modulation synthesis sound (pitch = [100], + amplitude = [5000], duration = [2], starttime = [0], carrier = + [1], modulator = [.5], index = [5], pitch_envelope = ['default'], + amplitude_envelope = ['default'], carrier_envelope = ['default'], + modulator_envelope = ['default'], index_envelope = ['default'], + wave = ['default'] ) """ + if pitch_envelope == 'default': + pitenv = 99 + else: + pitenv = pitch_envelope - if amplitude_envelope == 'default': ampenv = 100 - else: ampenv = amplitude_envelope + if amplitude_envelope == 'default': + ampenv = 100 + else: + ampenv = amplitude_envelope - if carrier_envelope == 'default': carenv = 99 - else: carenv = carrier_envelope + if carrier_envelope == 'default': + carenv = 99 + else: + carenv = carrier_envelope - if modulator_envelope == 'default': modenv = 99 - else: modenv = modulator_envelope + if modulator_envelope == 'default': + modenv = 99 + else: + modenv = modulator_envelope - if index_envelope == 'default': indenv = 99 - else: indenv = index_envelope + if index_envelope == 'default': + indenv = 99 + else: + indenv = index_envelope - if wave == 'default': wavetable = 1 - else: wavetable = wave + if wave == 'default': + wavetable = 1 + else: + wavetable = wave if not 7 in instrlist: orchlines.append("instr 7\n") @@ -138,20 +206,33 @@ def playFrequencyModulation( pitch=500, amplitude=5000, duration=2, starttime=0, orchlines.append("kcarenv oscil 1, 1/p3, p12\n") orchlines.append("kmodenv oscil 1, 1/p3, p13\n") orchlines.append("kindenv oscil 1, 1/p3, p14\n") - orchlines.append("asig foscil p5*kenv, p4*kpitenv, p6*kcarenv, p7*kmodenv, p8*kindenv, p9\n") + orchlines.append("asig foscil p5*kenv, p4*kpitenv, p6*kcarenv, " + "p7*kmodenv, p8*kindenv, p9\n") orchlines.append("out asig\n") orchlines.append("endin\n\n") instrlist.append(7) - scorelines.append("i7 %s %s %s %s %s %s %s %s %s %s %s %s %s\n" % (str(starttime), str(duration), str(pitch), str(amplitude), str(carrier), str(modulator), str(index), str(wavetable), str(pitenv), str(ampenv), str(carenv), str(modenv), str(indenv))) + scorelines.append("i7 %s %s %s %s %s %s %s %s %s %s %s %s %s\n" % + (str(starttime), str(duration), str(pitch), + str(amplitude), str(carrier), str(modulator), + str(index), str(wavetable), str(pitenv), str(ampenv), + str(carenv), str(modenv), str(indenv))) -def playPluck( pitch=100, amplitude=5000, duration=2, starttime=0, pitch_envelope='default', amplitude_envelope='default'): - """Play a string physical modeling sound (pitch = [100], amplitude = [5000], duration = [2], starttime = [0], pitch_envelope = ['default'], amplitude_envelope )""" - if pitch_envelope == 'default': pitenv = 99 - else: pitenv = pitch_envelope - if amplitude_envelope == 'default': ampenv = 100 - else: ampenv = amplitude_envelope +def playPluck(pitch=100, amplitude=5000, duration=2, starttime=0, + pitch_envelope='default', amplitude_envelope='default'): + """ Play a string physical modeling sound (pitch = [100], + amplitude = [5000], duration = [2], starttime = [0], + pitch_envelope = ['default'], amplitude_envelope ) """ + if pitch_envelope == 'default': + pitenv = 99 + else: + pitenv = pitch_envelope + + if amplitude_envelope == 'default': + ampenv = 100 + else: + ampenv = amplitude_envelope if not 8 in instrlist: orchlines.append("instr 8\n") @@ -163,35 +244,51 @@ def playPluck( pitch=100, amplitude=5000, duration=2, starttime=0, pitch_envelop orchlines.append("endin\n\n") instrlist.append(8) - scorelines.append("i8 %s %s %s %s %s %s\n" % (str(starttime), str(duration), str(pitch), str(amplitude), str(pitenv), str(ampenv))) + scorelines.append("i8 %s %s %s %s %s %s\n" % + (str(starttime), str(duration), str(pitch), + str(amplitude), str(pitenv), str(ampenv))) + -def playWave(sound='horse', pitch=1, amplitude=1, loop=False, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default'): - """Play a wave file (sound = ['horse'], pitch = [1], amplitude = [1], loop = [False], duration = [1], starttime = [0], pitch_envelope=['default'], amplitude_envelope=['default'])""" +def playWave(sound='horse', pitch=1, amplitude=1, loop=False, duration=1, + starttime=0, pitch_envelope='default', + amplitude_envelope='default'): + """ Play a wave file (sound = ['horse'], pitch = [1], amplitude = + [1], loop = [False], duration = [1], starttime = [0], + pitch_envelope=['default'], amplitude_envelope=['default']) """ if '/' in sound: fullname = sound else: fullname = finddir() + str(sound) - if loop == False: lp = 0 - else: lp = 1 + if loop == False: + lp = 0 + else: + lp = 1 - if pitch_envelope == 'default': pitenv = 99 - else: pitenv = pitch_envelope + if pitch_envelope == 'default': + pitenv = 99 + else: + pitenv = pitch_envelope - if amplitude_envelope == 'default': ampenv = 100 - else: ampenv = amplitude_envelope + if amplitude_envelope == 'default': + ampenv = 100 + else: + ampenv = amplitude_envelope if not 9 in instrlist: - orchlines.append("instr 9\n") + orchlines.append("instr 9\n") orchlines.append("kpitenv oscil 1, 1/p3, p8\n") orchlines.append("aenv oscil 1, 1/p3, p9\n") - orchlines.append("asig diskin p4, p5*kpitenv, 0, p7\n") + orchlines.append("asig diskin p4, p5*kpitenv, 0, p7\n") orchlines.append("out asig*p6*aenv\n") orchlines.append("endin\n\n") instrlist.append(9) - scorelines.append('i9 %f %f "%s" %s %s %s %s %s\n' % (float(starttime), float(duration), fullname, str(pitch), str(amplitude), str(lp), str(pitenv), str(ampenv))) - + scorelines.append('i9 %f %f "%s" %s %s %s %s %s\n' % + (float(starttime), float(duration), fullname, str(pitch), + str(amplitude), str(lp), str(pitenv), str(ampenv))) + + def getSoundList(): list = finddir() if list == None: @@ -199,14 +296,16 @@ def getSoundList(): sys.exit(0) return sorted(os.listdir(list)) -temp_path=None +temp_path = None + + def audioOut(file=None): - """Compile a .csd file and start csound to run it. If a string is given as argument, it write a wave file on disk instead of sending sound to hp. (file = [None])""" + """ Compile a .csd file and start csound to run it. If a string is + given as argument, it write a wave file on disk instead of sending + sound to hp. (file = [None]) """ global temp_path - import os if temp_path is None: from sugar import env - import os.path temp_path = env.get_profile_path() + '/pippy' if not os.path.isdir(temp_path): os.mkdir(temp_path) @@ -240,5 +339,5 @@ def audioOut(file=None): csd.write("\n</CsScore>\n") csd.write("\n</CsoundSynthesizer>") csd.close() - + os.system('csound ' + path + '/temp.csd >/dev/null 2>/dev/null') |