Permalink
Browse files

BF: fix for soundfile float division bug in duration

As it turns out it was inefficient to create sf.info(filename) for
this anyway. We now do tha calculation ourselves which means only
opening the file once
  • Loading branch information...
peircej committed Apr 10, 2017
1 parent 3b2fc7d commit e0e01ade0d1d3a64a21b6f619ac50b3d7c1f40ec
Showing with 5 additions and 6 deletions.
  1. +5 −6 psychopy/sound/backend_sounddevice.py
@@ -360,7 +360,7 @@ def _setSndFromFile(self, filename):
self.sampleRate = f.samplerate
if self.channels == -1: # if channels was auto then set to file val
self.channels = f.channels
info = sf.info(filename) # needed for duration?
fileDuration = float(len(f))/f.samplerate # needed for duration?
# process start time
if self.startTime and self.startTime > 0:
startFrame = self.startTime*self.sampleRate
@@ -371,19 +371,18 @@ def _setSndFromFile(self, filename):
# process stop time
if self.stopTime and self.stopTime > 0:
requestedDur = self.stopTime - self.t
maxDur = info.duration
self.duration = min(requestedDur, maxDur)
self.duration = min(requestedDur, fileDuration)
else:
self.duration = info.duration - self.t
self.duration = fileDuration - self.t
# can now calculate duration in frames
self.durationFrames = int(round(self.duration*self.sampleRate))
# are we preloading or streaming?
if self.preBuffer == 0:
# no buffer - stream from disk on each call to nextBlock
pass
elif self.preBuffer == -1:
# no buffer - stream from disk on each call to nextBlock
sndArr = self.sndFile.read(frames=len(self.sndFile))
# full pre-buffer. Load requested duration to memory
sndArr = self.sndFile.read(frames=int(self.sampleRate*self.duration))
self.sndFile.close()
self._setSndFromArray(sndArr)

0 comments on commit e0e01ad

Please sign in to comment.