We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Need API for saving to file.
Currently we only have a load function. But after training the network it would be great if we can save the generated tensor to a file.

@soumith I think we can reuse a lot of code from this repo https://github.com/MattVitelli/GRUV

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchaudio/__init__.py`
Content:
```
1 import torch
2 
3 from cffi import FFI
4 ffi = FFI()
5 from ._ext import th_sox
6 
7 def load(filename, out=None):
8     if out is not None:
9         assert torch.is_tensor(out)
10         assert not out.is_cuda
11     else:
12         out = torch.FloatTensor()
13 
14     if isinstance(out, torch.FloatTensor):
15         func = th_sox.libthsox_Float_read_audio_file
16     elif isinstance(out, torch.DoubleTensor):
17         func = th_sox.libthsox_Double_read_audio_file
18     elif isinstance(out, torch.ByteTensor):
19         func = th_sox.libthsox_Byte_read_audio_file
20     elif isinstance(out, torch.CharTensor):
21         func = th_sox.libthsox_Char_read_audio_file
22     elif isinstance(out, torch.ShortTensor):
23         func = th_sox.libthsox_Short_read_audio_file
24     elif isinstance(out, torch.IntTensor):
25         func = th_sox.libthsox_Int_read_audio_file
26     elif isinstance(out, torch.LongTensor):
27         func = th_sox.libthsox_Long_read_audio_file
28         
29     sample_rate_p = ffi.new('int*')    
30     func(bytes(filename), out, sample_rate_p)
31     sample_rate = sample_rate_p[0]
32     return out, sample_rate
33
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchaudio/__init__.py b/torchaudio/__init__.py
--- a/torchaudio/__init__.py
+++ b/torchaudio/__init__.py
@@ -1,32 +1,40 @@
+import os
+
 import torch
 
 from cffi import FFI
+
 ffi = FFI()
 from ._ext import th_sox
 
+
+def check_input(src):
+    if not torch.is_tensor(src):
+        raise TypeError('Expected a tensor, got %s' % type(src))
+    if not src.__module__ == 'torch':
+        raise TypeError('Expected a CPU based tensor, got %s' % type(src))
+
+
 def load(filename, out=None):
     if out is not None:
-        assert torch.is_tensor(out)
-        assert not out.is_cuda
+        check_input(out)
     else:
         out = torch.FloatTensor()
-
-    if isinstance(out, torch.FloatTensor):
-        func = th_sox.libthsox_Float_read_audio_file
-    elif isinstance(out, torch.DoubleTensor):
-        func = th_sox.libthsox_Double_read_audio_file
-    elif isinstance(out, torch.ByteTensor):
-        func = th_sox.libthsox_Byte_read_audio_file
-    elif isinstance(out, torch.CharTensor):
-        func = th_sox.libthsox_Char_read_audio_file
-    elif isinstance(out, torch.ShortTensor):
-        func = th_sox.libthsox_Short_read_audio_file
-    elif isinstance(out, torch.IntTensor):
-        func = th_sox.libthsox_Int_read_audio_file
-    elif isinstance(out, torch.LongTensor):
-        func = th_sox.libthsox_Long_read_audio_file
-        
-    sample_rate_p = ffi.new('int*')    
+    typename = type(out).__name__.replace('Tensor', '')
+    func = getattr(th_sox, 'libthsox_{}_read_audio_file'.format(typename))
+    sample_rate_p = ffi.new('int*')
     func(bytes(filename), out, sample_rate_p)
     sample_rate = sample_rate_p[0]
     return out, sample_rate
+
+
+def save(filepath, src, sample_rate):
+    filename, extension = os.path.splitext(filepath)
+    if type(sample_rate) != int:
+        raise TypeError('Sample rate should be a integer')
+
+    check_input(src)
+    typename = type(src).__name__.replace('Tensor', '')
+    func = getattr(th_sox, 'libthsox_{}_write_audio_file'.format(typename))
+
+    func(bytes(filepath), src, extension[1:], sample_rate)

{"golden_diff": "diff --git a/torchaudio/__init__.py b/torchaudio/__init__.py\n--- a/torchaudio/__init__.py\n+++ b/torchaudio/__init__.py\n@@ -1,32 +1,40 @@\n+import os\n+\n import torch\n \n from cffi import FFI\n+\n ffi = FFI()\n from ._ext import th_sox\n \n+\n+def check_input(src):\n+    if not torch.is_tensor(src):\n+        raise TypeError('Expected a tensor, got %s' % type(src))\n+    if not src.__module__ == 'torch':\n+        raise TypeError('Expected a CPU based tensor, got %s' % type(src))\n+\n+\n def load(filename, out=None):\n     if out is not None:\n-        assert torch.is_tensor(out)\n-        assert not out.is_cuda\n+        check_input(out)\n     else:\n         out = torch.FloatTensor()\n-\n-    if isinstance(out, torch.FloatTensor):\n-        func = th_sox.libthsox_Float_read_audio_file\n-    elif isinstance(out, torch.DoubleTensor):\n-        func = th_sox.libthsox_Double_read_audio_file\n-    elif isinstance(out, torch.ByteTensor):\n-        func = th_sox.libthsox_Byte_read_audio_file\n-    elif isinstance(out, torch.CharTensor):\n-        func = th_sox.libthsox_Char_read_audio_file\n-    elif isinstance(out, torch.ShortTensor):\n-        func = th_sox.libthsox_Short_read_audio_file\n-    elif isinstance(out, torch.IntTensor):\n-        func = th_sox.libthsox_Int_read_audio_file\n-    elif isinstance(out, torch.LongTensor):\n-        func = th_sox.libthsox_Long_read_audio_file\n-        \n-    sample_rate_p = ffi.new('int*')    \n+    typename = type(out).__name__.replace('Tensor', '')\n+    func = getattr(th_sox, 'libthsox_{}_read_audio_file'.format(typename))\n+    sample_rate_p = ffi.new('int*')\n     func(bytes(filename), out, sample_rate_p)\n     sample_rate = sample_rate_p[0]\n     return out, sample_rate\n+\n+\n+def save(filepath, src, sample_rate):\n+    filename, extension = os.path.splitext(filepath)\n+    if type(sample_rate) != int:\n+        raise TypeError('Sample rate should be a integer')\n+\n+    check_input(src)\n+    typename = type(src).__name__.replace('Tensor', '')\n+    func = getattr(th_sox, 'libthsox_{}_write_audio_file'.format(typename))\n+\n+    func(bytes(filepath), src, extension[1:], sample_rate)\n", "issue": "Need API for saving to file.\nCurrently we only have a load function. But after training the network it would be great if we can save the generated tensor to a file.\r\n\r\n@soumith I think we can reuse a lot of code from this repo https://github.com/MattVitelli/GRUV\n", "before_files": [{"content": "import torch\n\nfrom cffi import FFI\nffi = FFI()\nfrom ._ext import th_sox\n\ndef load(filename, out=None):\n    if out is not None:\n        assert torch.is_tensor(out)\n        assert not out.is_cuda\n    else:\n        out = torch.FloatTensor()\n\n    if isinstance(out, torch.FloatTensor):\n        func = th_sox.libthsox_Float_read_audio_file\n    elif isinstance(out, torch.DoubleTensor):\n        func = th_sox.libthsox_Double_read_audio_file\n    elif isinstance(out, torch.ByteTensor):\n        func = th_sox.libthsox_Byte_read_audio_file\n    elif isinstance(out, torch.CharTensor):\n        func = th_sox.libthsox_Char_read_audio_file\n    elif isinstance(out, torch.ShortTensor):\n        func = th_sox.libthsox_Short_read_audio_file\n    elif isinstance(out, torch.IntTensor):\n        func = th_sox.libthsox_Int_read_audio_file\n    elif isinstance(out, torch.LongTensor):\n        func = th_sox.libthsox_Long_read_audio_file\n        \n    sample_rate_p = ffi.new('int*')    \n    func(bytes(filename), out, sample_rate_p)\n    sample_rate = sample_rate_p[0]\n    return out, sample_rate\n", "path": "torchaudio/__init__.py"}], "after_files": [{"content": "import os\n\nimport torch\n\nfrom cffi import FFI\n\nffi = FFI()\nfrom ._ext import th_sox\n\n\ndef check_input(src):\n    if not torch.is_tensor(src):\n        raise TypeError('Expected a tensor, got %s' % type(src))\n    if not src.__module__ == 'torch':\n        raise TypeError('Expected a CPU based tensor, got %s' % type(src))\n\n\ndef load(filename, out=None):\n    if out is not None:\n        check_input(out)\n    else:\n        out = torch.FloatTensor()\n    typename = type(out).__name__.replace('Tensor', '')\n    func = getattr(th_sox, 'libthsox_{}_read_audio_file'.format(typename))\n    sample_rate_p = ffi.new('int*')\n    func(bytes(filename), out, sample_rate_p)\n    sample_rate = sample_rate_p[0]\n    return out, sample_rate\n\n\ndef save(filepath, src, sample_rate):\n    filename, extension = os.path.splitext(filepath)\n    if type(sample_rate) != int:\n        raise TypeError('Sample rate should be a integer')\n\n    check_input(src)\n    typename = type(src).__name__.replace('Tensor', '')\n    func = getattr(th_sox, 'libthsox_{}_write_audio_file'.format(typename))\n\n    func(bytes(filepath), src, extension[1:], sample_rate)\n", "path": "torchaudio/__init__.py"}]}