Repository: speechbrain/speechbrain

Issue:
E   TypeError: save() missing 1 required positional argument: 'sample_rate'
Hello,

Getting error in using 

`enhancer = SpectralMaskEnhancement.from_hparams(
    source="speechbrain/mtl-mimic-voicebank",
    savedir='stream_folder/',
    run_opts={"device":"cuda"})

enhanced = enhancer.enhance_file("stream_folder/fr_yourname.wav", output="stream_folder/test.wav)`

test_speechbrain.py:None (test_speechbrain.py)
test_speechbrain.py:13: in <module>
    enhanced = enhancer.enhance_file("stream_folder/fr_yourname.wav",
/home/tim/env/python3.8/lib/python3.8/site-packages/speechbrain/pretrained/interfaces.py:1929: in enhance_file
    torchaudio.save(output_filename, enhanced, channels_first=False)
E   TypeError: save() missing 1 required positional argument: 'sample_rate'

probably an update on torchaudio.save you need to handle.

Regards


Discussion:
@pplantinga ? 
I guess this error no longer occurs.
Hi,

I've installed SpeechBrain today from the git repos following the [guide](https://github.com/speechbrain/speechbrain#install-with-github) and ran the [example](https://github.com/speechbrain/speechbrain/blob/e498b87d6d7a99dc9989bf943b79eda9b4e284e7/speechbrain/pretrained/interfaces.py#L2166) shown in code.

The above mentioned issue still occurs.

So I modified the code to be able to pass in the sample_rate :
`torchaudio.save(output_filename, enhanced, sample_rate, channels_first=False)`

But the saved file is 58 bytes (the input noisy wav file was 2.9MB).

I tried to save the `noisy` variable and the same occured : output file was 59 bytes.

Please note that I am running on CPU.

I can confirm this successfully reproduces the issue:

```python
from speechbrain.pretrained import SpectralMaskEnhancement
enhancer = SpectralMaskEnhancement.from_hparams(
    source="speechbrain/metricgan-plus-voicebank"
)
enhanced = enhancer.enhance_file(
    "speechbrain/metricgan-plus-voicebank/example.wav",
    output_filename="test.wav"
)
```

There are two issues:
1. `sample_rate` is never passed to `torchaudio.save` (I suspect file output was actually never tested)
2. `channels_first=False` is incorrect, the shape of `enhanced` is `(num_channels, num_samples)`, not `(num_samples, num_channels)`.

@Ca-ressemble-a-du-fake For now you can work around the issue with:

```python
torchaudio.save(output_filename, enhanced, sample_rate=self.audio_normalizer.sample_rate, channels_first=True)
```

The audio output seems okay for me afterwards.

I can make a PR but I'm not sure about some details:
1. The standard shape for signals in SB should be `(num_samples, num_channels)` in the first place, right? It's probably cleaner to fix what causes the shape to be non-standard earlier in the code.
2. Can the enhancement output always be assumed to be the same sample rate as the input? Is `self.audio_normalizer.sample_rate` a sane way to achieve this?

cc @Adel-Moumen for reopening and insight
Hey @AsuMagic  thanks for this workaround! I thought at first that it did not change anything, but then I used another wav file and it worked! I don't know why it still does not work with the first audio file I tried (they are both 16kHz mono they only differ from their duration : the first one  is 84 s and the second one 132 s). Anyway thanks for this workaround.

Regarding your 2. question you could do `sample_rate=None` in the signature and then `sample_rate is None:  sample_rate = self.audio_normalizer.sample_rate` otherwise we use the provided sample rate.
> I don't know why it still does not work with the first audio file I tried (they are both 16kHz mono they only differ from their duration : the first one is 84 s and the second one 132 s).

Weird. What is the shape of the of the signal within `load_audio`? If you print the path in `load_audio`, does it seem correct (and does checking the file manually confirm that)? If both are ok, then what is the shape of `enhanced` just before writing?
Thanks for your support @AsuMagic I really appreciate! So following your troubleshooting guide :
- The file actually exists (printed path = where the file is)
- `noisy.size()` returns "torch.Size([1350654])"
- `enhanced.size()` returns "torch.Size([1, 1350654])"
Strange, I really don't see why this would save to an empty file. The parameters should be about right and I would be surprised if `torchaudio.save` was broken...  
Make sure it's not some silly mistake where you're looking at the wrong file/directory and that the file doesn't get overwritten with an file save later on in the code, just in case.
Hello, 

Is the issue still up? 

Thanks. 
Ping @pplantinga on this issue :p 

Resolving patch:
diff --git a/speechbrain/inference/enhancement.py b/speechbrain/inference/enhancement.py
--- a/speechbrain/inference/enhancement.py
+++ b/speechbrain/inference/enhancement.py
@@ -105,7 +105,11 @@ def enhance_file(self, filename, output_filename=None, **kwargs):
             enhanced = self.enhance_batch(batch)
 
         if output_filename is not None:
-            torchaudio.save(output_filename, enhanced, channels_first=False)
+            torchaudio.save(
+                uri=output_filename,
+                src=enhanced,
+                sample_rate=self.hparams.compute_stft.sample_rate,
+            )
 
         return enhanced.squeeze(0)
 
@@ -169,7 +173,11 @@ def enhance_file(self, filename, output_filename=None, **kwargs):
         enhanced = self.enhance_batch(batch)
 
         if output_filename is not None:
-            torchaudio.save(output_filename, enhanced, channels_first=False)
+            torchaudio.save(
+                uri=output_filename,
+                src=enhanced,
+                sample_rate=self.audio_normalizer.sample_rate,
+            )
 
         return enhanced.squeeze(0)
 


Test patch: