Skip to content

Commit

Permalink
Merge branch 'patch-1' of git://github.com/o-alexandre-felipe/demucs …
Browse files Browse the repository at this point in the history
…into o-alexandre-felipe-patch-1
  • Loading branch information
adefossez committed Jan 3, 2020
2 parents f36e73d + 664518b commit 9946479
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions demucs/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,25 @@ def read(self,
wav = torch.from_numpy(wav)
wav = wav.view(-1, self.channels()).t()
if channels == 1:
# Case 1:
# The caller asked 1-channel audio, but the stream have multiple
# channels, downmix all channels.
# We do mono convertion here as ffmpeg mess up the volume of mono output
# otherwise. See https://sound.stackexchange.com/a/42710.
wav = wav.mean(dim=0, keepdim=True)
elif self.channels() == 1 and channels != 1:
# Case 2:
# The caller asked for multiple channels, but the input file have
# one single channel, replicate the audio over all channels.
wav = wav.as_strided(size=(channels, wav.shape[1]), stride = (0,1));
elif self.channels() >= channels:
# Case 3:
# The caller asked for multiple channels, and the input file have
# more channels than requested. In that case return the first channels.
wav = wav[:channels,:];
else:
# Case 4: What is a reasonable choice here?
raise ValueException('The input file has less channels than requested')
if target_size is not None:
wav = wav[..., :target_size]
wavs.append(wav)
Expand Down

0 comments on commit 9946479

Please sign in to comment.