2

I am trying to read a FM signal which is recorded as WAV file using GNU radio Companion, using python. I am attaching the .grc file used.

I can clearly hear the recorded signal but reading the data gives a null ([] ) value.

The python code

import soundfile as sf
data, fs = sf.read('/home/fm_record_RSM_10_01_2019_dat.wav')
for i in data:
    print(i)

this gives

data
     array([], dtype=float64) 
fs 
     96000

When the following code is used,

import wave
input_wave_file= wave.open('/home/fm_record_RSM_10_01_2019_dat.wav', 'r')
nc,sw,fr,nf,ct,cn = input_wave_file.getparams()

another error is raised as given below

Error                                     Traceback (most recent call last)
<ipython-input-3-5009fe3506e7> in <module>()
      1 import wave
      2 
----> 3 input_wave_file= wave.open('/home/fm_record_RSM_10_01_2019_dat.wav', 'r')
      4 nc,sw,fr,nf,ct,cn = input_wave_file.getparams()
      5 frame_data = input_wave_file.readframes(5)

~/anaconda3/lib/python3.7/wave.py in open(f, mode)
    508             mode = 'rb'
    509     if mode in ('r', 'rb'):
--> 510         return Wave_read(f)
    511     elif mode in ('w', 'wb'):
    512         return Wave_write(f)

~/anaconda3/lib/python3.7/wave.py in __init__(self, f)
    162         # else, assume it is an open file object already
    163         try:
--> 164             self.initfp(f)
    165         except:
    166             if self._i_opened_the_file:

~/anaconda3/lib/python3.7/wave.py in initfp(self, file)
    131             raise Error('file does not start with RIFF id')
    132         if self._file.read(4) != b'WAVE':
--> 133             raise Error('not a WAVE file')
    134         self._fmt_chunk_read = 0
    135         self._data_chunk = None

Error: not a WAVE file

Could someone help me to find what the problem could be? Is it because of any mistake in the setting of record wav block in .grc file or any mistake in python file? Kindly help

Thanks a lot Msr

MSR
  • 77
  • 1
  • 3
  • 14
  • I dont have enough reputation to comment but Pl check the directory locations. Are you saving as 16 bits or 64 bits? – Rensi Sam Jan 22 '19 at 07:31
  • If I am not wrong there is also another reference about this question. Check it here: [https://stackoverflow.com/questions/2060628/reading-wav-files-in-python](https://stackoverflow.com/questions/2060628/reading-wav-files-in-python) – dpapadopoulos Jan 22 '19 at 07:25
  • data shows array([], dtype=float64) – MSR Jan 23 '19 at 06:41

1 Answers1

0
#! /usr/bin/env python3
import soundfile as sf
import wave
import sys
if len(sys.argv) < 2:
    print("Expected filename.wav on cmdline")
    quit(1)
data, fs = sf.read(sys.argv[1])
for i in range(10):
    print(i)

print('...')
input_wave_file= wave.open(sys.argv[1], 'r')
nc,sw,fr,nf,ct,cn = input_wave_file.getparams()
print('nc', nc) 
print('sw', sw) 
print('fr', fr) 
print('nf', nf) 
print('ct', ct) 
print('cn', cn)
chunk = 1024
data = input_wave_file.readframes(chunk)
print('data[0:10] =', data[0:10])
print('data[0:10] =', end='')
for i in range(10):
    print(data[i],' ',end='')
print('')

In a linux environment, I put the above into a file named playsound.py . Then I executed (at the cmdline prompt)

$ chmod +x playsound.py
$ ./playsound.py file.wav
[ 0.06454468  0.05557251]
[ 0.06884766  0.05664062]
[ 0.0552063   0.06777954]
[ 0.04733276  0.0708313 ]
[ 0.05505371  0.065979  ]
[ 0.05358887  0.06677246]
[ 0.05621338  0.06045532]
[ 0.04891968  0.06298828]
[ 0.04986572  0.06817627]
[ 0.05410767  0.06661987]
...
nc 2
sw 2
fr 44100
nf 32768
ct NONE
cn not compressed
data[0:10] = b'C\x08\x1d\x07\xd0\x08@\x07\x11\x07'
data[0:10] =67  8  29  7  208  8  64  7  17  7 

file.wav was some existing .wav file I had handy. I previously tried for i in data: print(i) as you had done, that worked also, but the output was too much.

I think you should check that the filename you are supplying points to a valid WAV file. For instance, the path you list is "/home/filename.wav" . Usually it will be at least "/home/username/filename.wav"