3

Python 2.7.3 (default, Sep 26 2013, 16:35:25) [GCC 4.7.2] on linux2 Linux Mint 14

Using Python, I am reading a wav and putting the data to a text file. The entire script is below. It is my first Python Script.

The values for the micsec can be anything from 000000 to 999999. There are frequently leading zero's which I am stripping with lstrip('0').

If the value is 000000, I end up with null. I'm using an extra step to change null to 0.

What is the best way to do this in a single step ?

Thanks for any input.

!/usr/bin/python

import contextlib, datetime, math, os, time, wave, glob
from scipy.io.wavfile import read
from numpy import log10, sqrt, mean
import numpy as np

path = "/the/path/*.wav"
for fname in glob.glob(path):

print "process_wav is processing - " + fname

outfname = fname + ".txt"
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(fname))
tm=0.000000

with contextlib.closing(wave.open(fname,'r')) as f:
channels = f.getnchannels()
sampwidth = f.getsampwidth()
comptype = f.getcomptype()
frames = f.getnframes()
rate = f.getframerate()
wav_duration = frames / float(rate)
frame_duration = round((wav_duration / frames),6)

samprate, wavdata = read(fname)
chunks = np.array_split(wavdata, frames)

hdr = (
"# audio file processed - " + fname +
"\n# this file name - " + outfname +    
"\n# audio file mod time-" + str(mtime) +
"\n# channels - " + str(channels) +
"\n# sampwidth - " + str(sampwidth) +
"\n# comptype - " + str(comptype) +
"\n# frames - " + str(frames) +
"\n# rate - " + str(rate) +
"\n# wav_duration - " + str(wav_duration) +
"\n# frame_dutation - " + "{0:0.6f}".format(frame_duration) +
"\n# chunk|wave_file_name|audio_file_timestamp|chunk_second|chunk_microsecond|chunk_time_in_audio|frame_duration|dB\r\n"
)

out = open(outfname,'w')
out.write(hdr)

for i,chunk in enumerate(chunks):
try:
sec = int("{0:0.6f}".format(round(float(tm),6)).split('.')[0])
micsec = "{0:0.6f}".format(round(float(tm),6)).split('.')[1].lstrip('0')
#handle 000000 - there has to be a better way
micsec = int(float(micsec)) if len(micsec)>=1 else 0
cm = mtime + datetime.timedelta(seconds=sec,microseconds=micsec)
out.write("{}|{}|{}|{}|{}|{}|{}|{}\n".format(i, fname, mtime, sec, micsec, cm, frame_duration, 20*log10( sqrt(mean(chunk**2)))))
tm += frame_duration
except ValueError as ex:
print("value error" + str(ex))
out.write(str(i) + str(ex))
except Exception,e:
print("ERROR" + str(e))
out.write(str(i) + str(e))

out.close()
Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47
todwith1d
  • 77
  • 1
  • 6
  • 2
    pre-emptively vote to reopen – mob Feb 24 '14 at 19:38
  • 2
    This is a similar question with an answer provided in python: http://stackoverflow.com/a/13244011/2113090 – johanno Feb 24 '14 at 20:26
  • 1
    What is it with the off-topic lately? Op did "Instead, describe the problem and what has been done so far to solve it." Had he omitted the word "suggest" and instead asked for help with "reading a wav file and extracting/calculating dB from wav?" the question would have been perfectly fine but so it is unsalvagable? – DeVadder Feb 25 '14 at 07:05
  • @DeVadder No. Had he omitted "Can anyone help me get started ... python, perl, java, ruby, bash are all options" and replaced it with "I am trying to read a WAV file with X, here is what I have so far, this is the problem I'm seeing and this is what I've tried to do to fix it", then it would have been perfectly fine. As it stands it is not salvageable. – Jason C Feb 25 '14 at 07:10
  • @todwith1d I suggest picking your favorite language, and either a) reading its documentation to see if it as an API to audio files or b) reading the [WAV format specification](https://ccrma.stanford.edu/courses/422/projects/WaveFormat/) (they're quite easy to read), then coming back here once you have a specific problem. SO is not a replacement for documentation that you do not feel like reading. – Jason C Feb 25 '14 at 07:12
  • @JasonC: The sentence you just quoted is from me. Also: You just explained how switching out a single sentence would make the question perfectly fine (He does show what he tried and what the problem is) and call it not salvageable? Maybe we have a different understanding of that word. – DeVadder Feb 25 '14 at 07:13
  • @DeVadder: It is irrelevant that the sentence is from you because had I quoted the original sentence, it would not have changed a thing. If you believe that this is *not* a question asking to "recommend or find a tool, library or favorite off-site resource" and/or that this question will *not* tend to attract opinionated answers and spam, then I'm afraid we do not seem to be reading the same question. – Jason C Feb 25 '14 at 07:14
  • @JasonC I am quite sure that questions that concern problems not solvable with built-ins may have value. There are hundreds of questions in C++ for example that state a specific problem and get told "Use boost". And that is okay and helpful. I am convinced that off-topic mask is aimed at questions that only ask for a tool. It even states "Instead, describe the problem and what has been done so far to solve it."! But whatever. I just thought with the whole "be nice to new users" discussion going on, it might be okay to not close every question that just barely falls under one of the masks. – DeVadder Feb 25 '14 at 07:21
  • I will not say anymore to this, but it is my opinion that with a little rewording, this question could have remained and even might have generated valuable answers that could have helped future readers. – DeVadder Feb 25 '14 at 07:22
  • It *does* have value. It is also not a question that is on topic at SO. It will have to generate valuable answers somewhere else. It *may* be appropriate over at Programmers.sx. Being nice to new users does not imply bending existing topic guidelines to accommodate them. – Jason C Feb 25 '14 at 07:32
  • @JasonC i have edited and am requesting that this be reopened. still learning the rules here – todwith1d Mar 05 '14 at 19:58
  • @mob i have edited as requested and am requesting that this be reopened. is that possible or should I just post it again. thanks for your help – todwith1d Mar 07 '14 at 18:28

0 Answers0