Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 e0e01ad
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions psychopy/sound/backend_sounddevice.py
Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit e0e01ad

Please sign in to comment.