Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert CSV File to WAV

3,538 views
Skip to first unread message

abr...@cjaudio.com

unread,
Apr 7, 2013, 5:30:38 AM4/7/13
to

My Hantek signal generator only saves in CSV format. I would like to
covert this to WAV so it may be played back as an audio loop using one
of the commonly available Windows programs.

Does anyone know how to do this "in house", eg. without resorting to a
specialized scripting language?

There is an assortment of converters online (one selection below), but
none I can find that compiles the data into WAV format.

http://www.bluechillies.com/software/file-conversion-csv-to-wav.html

It can be done, apprently, if you are proficient in Python.
Unfortunately, I am not.

https://gist.github.com/Pretz/1773870

Arthur Brooks

Jim Thompson

unread,
Apr 9, 2013, 4:33:59 PM4/9/13
to
I can think of a round-about way...

Play the CSV as a PWL file in LTspice and then write to a WAV, from
the LTspice Help...

.WAVE -- Write Selected Nodes to a .Wav File.
LTspice can write .wav audio files. These files can then be listened
to or be used as the input of another simulation.

Syntax: .wave <filename.wav> <Nbits> <SampleRate> V(out) [V(out2)
...]

Example: .wave C:\output.wav 16 44.1K V(left) V(right)

<filename.wav> is either a complete absolute path for the .wav file
you wish to create or a relative path computed from the directory
containing the simulation schematic or netlist. Double quotes may be
used to specify a path containing spaces. <Nbits> is the number of
sampling bits. The valid range is from 1 to 32 bits.
<SampleRate> is the number of samples to write per simulated second.
The valid range is 1 to 4294967295 samples be second. The remainder of
the syntax lists the nodes that you wish to save. Each node will be an
independent channel in the .wav file. The number of channels may be
as few as one or as many as 65535. It is possible to write a device
current, e.g., Ib(Q1) as well as node voltage. The .wav analog to
digital converter has a full scale range of -1 to +1 Volt or Amp.

Note that it is possible to write .wav files that cannot be played on
your PC sound system because of the number of channels, sample rate or
number of bits due to limitations of your PC's codec. But these .wav
files may still be used in LTspice as input for another simulation.
See the sections LTspice=>Circuit Elements=>V. Voltage Source and I.
Current source for information on playing a .wav file into an LTspice
simulation. If you want to play the .wav file on your PC sound card,
keep in mind that the more popularly supported .wav file formats have
1 or 2 channels; 8 or 16 bits/channel; and a sample rate of 11025,
22050, or 44100 Hz.


...Jim Thompson
--
| James E.Thompson | mens |
| Analog Innovations | et |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona 85048 Skype: Contacts Only | |
| Voice:(480)460-2350 Fax: Available upon request | Brass Rat |
| E-mail Icon at http://www.analog-innovations.com | 1962 |

I love to cook with wine. Sometimes I even put it in the food.

mrob...@att.net

unread,
Apr 9, 2013, 5:46:55 PM4/9/13
to
abr...@cjaudio.com wrote:
> Does anyone know how to do this "in house", eg. without resorting to a
> specialized scripting language?

No. Saying you have a CSV file is about like saying you have an Excel
file; it allows you to reliably produce the same set of numbers, but it
doesn't carry any information about what the numbers represent. You
have to provide this knowledge to whatever program you use to convert
the CSV file to something else. For widely-used CSV formats (maybe US
Census data?) there are ready-made programs for it, but for all the
other millions of CSV formats in the world, you get to roll your own.

For jobs like this, it usually works out better to implement the program
in more of a scripting language (awk, Perl, Python, etc) instead of a
real, official compiled language (FORTRAN, C, C++, etc). The scripting
language will have built-in features for parsing the CSV file that you
will take a long time for you to reimplement badly in an official
compiled language. Also, the requirements are almost guaranteed to be
poorly specified, which means more revisions of the code, which is
usually easier with a scripting language.

> http://www.bluechillies.com/software/file-conversion-csv-to-wav.html

A lot of these are probably wrappers around existing open-source
libraries that they then charge you $17 to $80 for. Which would be OK
if the perpetrators provided any end-user support in return, or
supported further development of the underlying code, but they don't.

> It can be done, apprently, if you are proficient in Python.
> Unfortunately, I am not.
>
> https://gist.github.com/Pretz/1773870

That looks like a reasonable approach, actually. It expects a CSV file
with two columns: time and amplitude. It reads both values, discards
the time (it seems to assume that the input data is sampled at 100 kHz),
keeps the amplitudes in an array, normalizes the entire array, resamples
to 44.1 kHz, and writes a 16-bit mono 44.1 kHz WAV file.

I am not sure what a couple of the fixed constants do, so this might die
or do an incomplete job if you try to convert a really huge file
(several dozen minutes maybe?), but for shorter files it should be fine.

On Linux you would save the contents of that github file to (say)
hantek-csv-converter.py . Then you would put the CSV data in (say)
hantek.csv in the same directory. Then, in that directory, do

chmod 755 hantek-csv-converter.py

./hantek-csv-converter.py hantek.csv

and after a bit, a new file named hantek.wav should appear in the same
directory.

On Windows you need to install Python for Windows first, which exists.
If the installer asks you about configuring it to run Python scripts
from the command line, say yes. Save the contents of that github file
to (say) hantek-csv-converter.py . Then you would put the CSV data in
(say) hantek.csv in the same directory. Then, in a cmd.exe window,
navigate to that directory, and do

python hantek-csv-converter.py hantek.csv

and after a bit, a new file named hantek.wav should appear in the same
directory.

If the only thing that will do is a single .exe with a nice happy clicky
interface, you'll probably either need to get it from Hantek or pay to
have one written. It will probably be cheaper to buy a different signal
generator that saves files in a reasonable format than to buy a custom-
written program.

Matt Roberds

Jim Thompson

unread,
Apr 9, 2013, 6:05:47 PM4/9/13
to
CSV, as it comes out of the Hantek will be of the form of PAIRS...

time, voltage

which is the exact same format as a Spice PWL file.

So it is quite trivial to add nodes and the text PWL...

v1 1 2 PWL (0,1) (1.2,5) (1.4,2) (2,4) (3,1)

Most spices will accept the list-style format...

v1 1 2 PWL
+ (0,1)
+ (1.2,5)
+ (1.4,2)
+ (2,4)
+ (3,1)

So the most work will be in adding the "+" continuation to the CSV as
it comes straight out of Excel... easier to do with an editor such as
UltraEdit with macro capability.

I go back and forth all the time from PSpice to Excel to PSpice
producing PWL's or processing .DAT files from PSpice Probe.

miso

unread,
Apr 10, 2013, 3:53:39 PM4/10/13
to
I've done the reverse with sox, i.e. wave to data. The webpage says it
can take data to wav, but I doubt you will be doing this without minimal
scripting or a simple program. Sox can't anticipate every data format.
> http://sox.sourceforge.net/Main/HomePage

The C programs I've thrown together for reformatting data are less than
a hundred lines. It isn't a major project.


abr...@cjaudio.com

unread,
Apr 11, 2013, 1:38:53 AM4/11/13
to
On Sun, 07 Apr 2013 19:30:38 +1000, abr...@cjaudio.com wrote:


For anyone who is interested, I found tbis free arb gen software that
saves in several formats including WAV.

http://www.tti1.co.uk/downloads/waveman-plus.htm

Arthur Brooks

comb...@gmail.com

unread,
Dec 4, 2015, 2:27:35 AM12/4/15
to
A few comments about the Python "Pretz" approach

* before using it, you must
1) install libsamplerate, with yum, apt-get, brew, etc.
2) install scikits.samplerate with pip (looks like you need the development version of Python for that)

* the Pretz code, commendable as it is, appears to have a few errors or inconsistencies
a) on line 29, the encoding for struct.pack should be 'l' (long) instead of 'h' (short):

frames.append(struct.pack('h', mul)) =>
frames.append(struct.pack('l', mul))

b) line 32 should be removed, and line 33 'out'-dented accordingly

for x in xrange(0, 7200):
wavfile.writeframes(frames) =>
wavfile.writeframes(frames)

c) I think line 57 should be changed so

write_wav(sampled, filename_head + ".wav", 100000, 32700) =>
write_wav(sampled, filename_head + ".wav", target_samplerate, 32700)

comb...@gmail.com

unread,
Dec 4, 2015, 10:02:18 AM12/4/15
to
Regarding my last post:

ignore

gav...@gmail.com

unread,
Dec 20, 2016, 1:21:17 PM12/20/16
to
hi i need some help about the pretz code. how do the the csv file have to be ? like this:
0 , 3499
1, 500
2 , 550

i tried this and i cant hear anything in the wav file

thanks

Don Y

unread,
Dec 20, 2016, 1:44:30 PM12/20/16
to
Turn the volume up to 11...

bitrex

unread,
Dec 20, 2016, 2:19:46 PM12/20/16
to
Don't reinvent the wheel. There's a library for that:

http://aquila-dsp.org/

Jim Thompson

unread,
Dec 20, 2016, 3:08:05 PM12/20/16
to
[snort]:-}

...Jim Thompson
--
| James E.Thompson | mens |
| Analog Innovations | et |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| STV, Queen Creek, AZ 85142 Skype: skypeanalog | |
| Voice:(480)460-2350 Fax: Available upon request | Brass Rat |
| E-mail Icon at http://www.analog-innovations.com | 1962 |

There is no fool quite like an audiphool.

gav...@gmail.com

unread,
Dec 21, 2016, 10:46:12 AM12/21/16
to
i don't understand, what this library suppose to do.
convert csv to wav ? analyse wav file ?

bitrex

unread,
Dec 21, 2016, 10:59:53 AM12/21/16
to
On 12/21/2016 10:46 AM, gav...@gmail.com wrote:
> i don't understand, what this library suppose to do.
> convert csv to wav ? analyse wav file ?

Please look at the docs here, particularly the "PlainTextFile.h"

http://aquila-dsp.org/docs/

The API in that header will let you convert a plain text file of 8 bit
or 16 bit samples into the library's own SignalSource generic format.
Then the API exposed in WavFileHandler.h will take the SignalSource
vector, create the appropriate header, and write the data to a .wav file.

The PlainTextFile.h expects its input to be one sample per newline, not
CSV. I imagine temporarily converting the CSV to a file object of that
format during the conversion process should be straightforward enough...

gav...@gmail.com

unread,
Dec 21, 2016, 2:15:51 PM12/21/16
to
Great thanks a lot ! i am trying to get that aquila , i am on windows 10, programming on mbed.org for the disco F746 board. is it ok ? how i install this ?
0 new messages