How do I read in wav files in .gz?

时间:2018-05-06 17:06:04

标签: python gzip gz

I am learning machine learning and data analysis on $query = "SELECT year, brand, model, class, stocknr, price, status, pic FROM stock WHERE class = 'ldv' ORDER BY brand ASC"; $i = 0; $result = mysqli_query($con,$query); if (!$result) { die("Connection Error: " . mysqli_connect_error()); } while ($row = mysqli_fetch_array($result)) { $title = $row['model'] ; $uppic = $row['pic']; $price = $row['price']; $stock = $row['stocknr']; if (($i == 1) or (($i - 1) % 4) == 0) { echo '<tr>' . "\n"; } echo "<td width=150 align=center>"; echo "<a href=../imagesize.php?scode=$uppic><img src='images/$uppic.jpg' border='0' alt='Item $title'></a>"; echo "<br><a href='details.php?stocknr=$stock'> <span class=fs13>$title</span></a>"; echo "<br><a href='details.php?stocknr=$stock'> <span class=sapri>$price</span></a>"; echo "</td>"; // for 5th, 10th, 15th etc record insert a tag for end of the row if ((($i) % 5) == 0) { echo '</tr>'; } $i++; } // filling row with empty cells while(($i - 1) % 5 != 0) { echo '<td> </td>'; $i++; // for 5th, 10th, 15th etc record insert a tag for end of the row if (($i - 1) % 5 == 0) { echo '</tr>'; } } echo '</table>'; files. I know if I have public contactAPI(){ var uri = 'localhost:3000'; //Or whatever the link is for your node server return this.http.get(uri); } files directly I can do something like this to read in the data

wav

Now I'm given a gz-file wav I'm not sure what to do now.

I tried:

import librosa

mono, fs = librosa.load('./small_data/time_series_audio.wav', sr = 44100)

but it gives me:

"music_feature_extraction_test.tar.gz"

Can anyone help me out?

2 个答案:

答案 0 :(得分:2)

有几件事情在发生:

  • 您获得的文件是一个压缩压缩的tarball。看看tarfile module,它可以直接读取gzip压缩文件。你会在它的成员上得到一个迭代器,每个成员都是一个单独的文件。
  • AFAIKS librosa无法从内存缓冲区中读取,因此您必须将tar-members解包为临时文件。 tempfile - module是您的朋友,NamedTemporaryFile会为您提供一个自动删除的文件,您可以将其解压缩并提供给librosa

您可能希望将此实现为一个简单的生成器函数,它将tarfile-name作为其输入,迭代它的成员以及yield librosa.load()为您提供的内容。这样一切都会自动清理。

因此基本循环

  1. 使用tarfile - 模块打开tarball。对于每个成员
  2. 使用NamedTemporaryFile获取新的临时文件。将tarball-member的内容复制到该文件。您可能希望使用shutil.copyfileobj以避免在将整个wav文件写入磁盘之前将其读入内存。
  3. NamedTemporaryFile具有文件名属性。将其传递给librosa.open
  4. yield librosa.open对来电者的返回值。

答案 1 :(得分:0)

您可以使用PySoundFile从压缩文件中读取。 https://pysoundfile.readthedocs.io/en/0.9.0/#virtual-io

import soundfile

with gzip.open('music_train.tar.gz', 'rb') as gz_f:
    for file in gz_f : 
        fs, mono = soundfile.read(file, samplerate=44100)

也许您还应该在使用librosa处理数据之前检查是否需要重新采样数据: https://librosa.github.io/librosa/ioformats.html#read-specific-formats