1

I get a blob out of a ms access database which contains mono wav data from an instrument. It's just 1 second. It begins with different values, so I mean it is pure wav Data

b'\\f_?\xb2\xca^??;]?\xa9(Z?\xf8\x87V?\x8e\x11S?PCP?\xcc\x06O?\xf1RN?a\x07M?\x83DK?\xa9hH?\x94\nE?\x9d.B?mb??\xf9\x05=?\xf5X;?\xeb|:?M\xc7:?Qg;?\xbd\x0c<?\x8c\x05=?\x85I>?v\xd2??/`B?\xfe\xafF?\xafFL?b3S?,\x8dZ?\xc1}`?\x16Le?\xef\xb2h?@#j?a\x02k?\xb3\xcck?D\xd2l?\xec\xaen?\x19\xccp?\x9e\xdfr?\x82Uu?\x81\x03x?tCz?\xa5E{?\xec\xecz?5\xcey?\x08\x82x?\x83\xd2v?\xec\xb3t?\x8f\x04r?\xce\x08n?\x8d3h?\x11@a?\x1e\xfbX?\x9b\x11P?\xfc}G?\x08\'

...

I used the packages wave and soundfile to create a wav file, but it never meets the original format. I could export a file from the original software, so it should work.

import wave


    wav = wave.open(wavfile, 'rb')
    print("filename :", wavfile)
    print("channels :", wav.getnchannels())
    print("sampwidth:", wav.getsampwidth())
    print("framerate:", wav.getframerate())
    print("nframes  :", wav.getnframes())

The output is:

filename : c:\DATEN\TS_org.wav
channels : 1
sampwidth: 2
framerate: 32768
nframes  : 16384

I did set these parameters to th new file and added the BLOB:

def create_audiofile(blobin, saveto):
    nchannels = 1
    #ok to read in another analyser, but wrong sound sampwidth = 1
    sampwidth = 2
    framerate = 32768
    nframes = 16384

    audio = wave.open (saveto, 'wb')
    audio.setnchannels (nchannels)
    audio.setsampwidth (sampwidth)
    audio.setframerate (framerate)
    audio.setnframes (nframes)
    audio.writeframes(blobin)

    #also no success
    #audio.writeframesraw(blobin)

I checked serveral websites and answers here in SO. There is a good description here, but I never got a working wav file.

The first 36 bytes of the original file look like this:

Bytenr :0       1       2       3       4       5       6       7       8       9       10      11      12      13      14      15      16      17      18      19      20      21      22      23      24      25      26      27      28      29      30      31      32      33      34      35      
integer:82      73      70      70      36      128     0       0       87      65      86      69      102     109     116     32      16      0       0       0       1       0       1       0       0       128     0       0       0       0       1       0       2       0       16      0       
hexcode:0x52    0x49    0x46    0x46    0x24    0x80    0x0 0x0 0x57    0x41    0x56    0x45    0x66    0x6d    0x74    0x20    0x10    0x0 0x0 0x0 0x1 0x0 0x1 0x0 0x0 0x80    0x0 0x0 0x0 0x0 0x1 0x0 0x2 0x0 0x10    0x0 
ASCII  :R       I       F       F       $                               W       A       V       E       f       m       t               #                               #               #                                                               #               #               #               

How can I use these information to build a new wav File?

Edit

This is the funny script from @mivk as python script to test conversion:

import subprocess
infile =("e:\\data\\wav\\raw.pcm")
ffmpeg = "e:\\data\\wav\\ffmpeg.exe"
samples = [8000, 11025, 22050, 16000, 32000, 22050, 44100]
formats = ['alaw', 'f32be', 'f32le', 'f64be', 'f64le', 'mulaw', 's16be', 's16le', 's24be', 's24le', 's32be',
              's32le', 's8', 'u16be', 'u16le', 'u24be', 'u24le', 'u32be', 'u32le', 'u8']
for ar in samples:
    for f in formats:
        outfile = infile[:-4] + "_%s_%s.wav"%(f, ar)
        order = '%s -f %s -ar %d -ac 1 -i %s %s'%(ffmpeg, f, ar, infile, outfile)
        subprocess.call(order)

Papageno
  • 305
  • 3
  • 15
  • 1
    Does your BLOB in the database start with "RIFF....WAVE", or doesn't it? If it does, it's already wav data and you can save it to a file as-is. If it doesn't, and the "RIFF..." in your question comes from a file you generated, then your original data is raw PCM. In that case, you have to find out the exact details of that PCM data (16, 24 or other bits per sample; 44.1, 48 or other Mhz sampling rate etc.). I guess the easiest if you need to guess that info is to feed the raw PCM data to `ffmpeg` with various options until it puts out the correct .wav file. – mivk Feb 09 '20 at 18:48
  • The BLOB doesn't start wit RIFF. The mentioned first 36 Bytes are from a file how it should be. I have a VB code, but I'm not sure if its correct because I can't test it. The only thing I have is the 16Bit and 32768 samples/s. – Papageno Feb 09 '20 at 18:52
  • See [this answer](https://stackoverflow.com/a/11990796/111036) for examples of options to try with `ffmpeg` to find the correct PCM information. You need to provide this info so that the raw PCM can be wrapped into the correct .wav container. If you can post a link to one such PCM blob, we could try it to find the correct info. – mivk Feb 09 '20 at 18:56
  • Ok, I've checked the files with `ffprobe` and adjusted the parameters with `ffmpeg` to: _Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 32768 Hz, 1 channels, s16, 524 kb/s_ (which is the original file) but I still get only white noise. So, my next trial will be the _BLOB_. Maybe I have to change the format. Nethertheless, thanks. – Papageno Feb 09 '20 at 19:56
  • Here is a fun command to generate various wavs from a "raw.pcm" file: `for ar in 8000 11025 22050 16000 32000 22050 44100; do for f in alaw f32be f32le f64be f64le mulaw s16be s16le s24be s24le s32be s32le s8 u16be u16le u24be u24le u32be u32le u8; do ffmpeg -f $f -ar $ar -ac 1 -i raw.pcm test-$f-$ar-ac1.wav; done; done` . One of those should have a good chance to sound right... – mivk Feb 09 '20 at 20:06
  • Really funny. Unfortunally no luck. – Papageno Feb 09 '20 at 21:17
  • Are you sure the bytes you extract are correct? In your example of 224 bytes, most bytes appear 1 to 3 times, except "?" which appears 58 times. That seems strange. Isn't something on the way out of the database replacing some bytes by "?". – mivk Feb 09 '20 at 21:34
  • It is part of a timesignal out of an instrument. I couldn't post the file itself. In the meantime I got the full VB Code and I'm discussing with the coder of it. Will take time... – Papageno Feb 10 '20 at 08:08

0 Answers0