Skip to content

Commit

Permalink
Merge pull request #1858 from dvbridges/soundDur
Browse files Browse the repository at this point in the history
BF: Allows sound duration defined from cond. file. Fixes 4) from #1821
  • Loading branch information
peircej committed Jun 20, 2018
2 parents 21fd57f + a1ca68c commit b53df91
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions psychopy/experiment/components/sound/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,24 @@ def __init__(self, exp, parentName, name='sound_1', sound='A', volume=1,
def writeInitCode(self, buff):
# replaces variable params with sensible defaults
inits = getInitVals(self.params)
if inits['stopVal'].val in ['', None, 'None']:
inits['stopVal'].val = -1
elif float(inits['stopVal'].val) > 2:
if '$' in inits['stopVal'].val:
inits['stopVal'].val = -1
else:
if inits['stopVal'].val in ['', None, 'None']:
inits['stopVal'].val = -1
elif float(inits['stopVal'].val) > 2:
inits['stopVal'].val = -1
buff.writeIndented("%s = sound.Sound(%s, secs=%s)\n" %
(inits['name'], inits['sound'], inits['stopVal']))
buff.writeIndented("%(name)s.setVolume(%(volume)s)\n" % (inits))

def writeRoutineStartCode(self, buff):
inits = getInitVals(self.params)
if '$' in inits['stopVal'].val:
buff.writeIndented("%s.setSound(%s, secs=%s)\n" %
(inits['name'], inits['sound'], inits['stopVal']))
buff.writeIndented("%(name)s.setVolume(%(volume)s)\n" % (inits))

def writeFrameCode(self, buff):
"""Write the code that will be called every frame
"""
Expand All @@ -98,12 +108,16 @@ def writeFrameCode(self, buff):
# because of the 'if' statement of the time test
buff.setIndentLevel(-1, relative=True)
if not self.params['stopVal'].val in ['', None, -1, 'None']:
if not float(self.params['stopVal'].val) < 2: # Reduce spectral splatter but not stopping short sounds
if '$' in self.params['stopVal'].val:
code = 'if %(name)s.status == STARTED and t >= %(stopVal)s:\n' \
' %(name)s.stop() # stop the sound (if longer than duration)\n'
buff.writeIndentedLines(code % self.params)
elif not float(self.params['stopVal'].val) < 2: # Reduce spectral splatter but not stopping short sounds
self.writeStopTestCode(buff)
code = "%s.stop() # stop the sound (if longer than duration)\n"
buff.writeIndented(code % self.params['name'])
# because of the 'if' statement of the time test
buff.setIndentLevel(-1, relative=True)
# because of the 'if' statement of the time test
# buff.setIndentLevel(-1, relative=True)

def writeRoutineEndCode(self, buff):
code = "%s.stop() # ensure sound has stopped at end of routine\n"
Expand Down

0 comments on commit b53df91

Please sign in to comment.