Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to convert .WAV to .CSV and .CSV to .WAV

How to convert .WAV to .CSV and .CSV to .WAV

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Austin_Cpp
    wrote on last edited by
    #1

    I am looking for c++ code that can load a .wav audio file and output a .csv text file. Then convert it back again. That is wav-to-csv and csv-to-wav. This is an unusual conversion process so any help is welcome.

    _ C J 3 Replies Last reply
    0
    • A Austin_Cpp

      I am looking for c++ code that can load a .wav audio file and output a .csv text file. Then convert it back again. That is wav-to-csv and csv-to-wav. This is an unusual conversion process so any help is welcome.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      .wav is a binary file and .csv is a text file. So what is the conversion that you would like to do? You cannot normally convert audio data to text data and vice versa. So you need to be more specific on your question. The only conversion that I can think of is to extract the tag information from a .wav file to a .csv file. But then again the reverse would not be possible because there is no audio data in a .csv file. The next thing that you can do is a simple rename of the extension from .wav to .csv, but that doesn't make any sense either.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      1 Reply Last reply
      0
      • A Austin_Cpp

        I am looking for c++ code that can load a .wav audio file and output a .csv text file. Then convert it back again. That is wav-to-csv and csv-to-wav. This is an unusual conversion process so any help is welcome.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        The WAV file format is relatively simple to parse. You may have a look at the specifications (as starting point, you may check out its Wikipedia page[^]. Once you are able to parse it you have to decide how to generate a meaningful (for instance an 'easy for the human to read') format for putting all the information in the CSV file.

        Veni, vidi, vici.

        A 1 Reply Last reply
        0
        • C CPallini

          The WAV file format is relatively simple to parse. You may have a look at the specifications (as starting point, you may check out its Wikipedia page[^]. Once you are able to parse it you have to decide how to generate a meaningful (for instance an 'easy for the human to read') format for putting all the information in the CSV file.

          Veni, vidi, vici.

          A Offline
          A Offline
          Austin_Cpp
          wrote on last edited by
          #4

          Here is the only code I can find for writing a .Wav file. This is python which I know nothing about. I have .csv files of data that when plotted draw a mandala. I want to save the same file to a .wav format so I can play the mandala as sound. Is writing a wav file possible with C++ code or do I need a library? https://gist.github.com/Pretz/1773870 #!/usr/bin/python import wave import numpy import struct import sys import csv from scikits.samplerate import resample def write_wav(data, filename, framerate, amplitude): wavfile = wave.open(filename, "w") nchannels = 1 sampwidth = 2 framerate = framerate nframes = len(data) comptype = "NONE" compname = "not compressed" wavfile.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname)) print("Please be patient whilst the file is written") frames = [] for s in data: mul = int(s * amplitude) # print "s: %f mul: %d" % (s, mul) frames.append(struct.pack('h', mul)) # frames = (struct.pack('h', int(s*self.amp)) for s in sine_list) frames = ''.join(frames) for x in xrange(0, 7200): wavfile.writeframes(frames) wavfile.close() print("%s written" %(filename)) if __name__ == "__main__": if len(sys.argv) <= 1: print "You must supply a filename to generate" exit(-1) for fname in sys.argv[1:]: data = [] for time, value in csv.reader(open(fname, 'U'), delimiter=','): try: data.append(float(value)) except ValueError: pass # Just skip it print "Generating wave file from %d samples" % (len(data),) arr = numpy.array(data) # Normalize data arr /= numpy.max(numpy.abs(data)) filename_head, extension = fname.rsplit(".", 1) # Resample normalized data to 44.1 kHz target_samplerate = 44100 sampled = resample(arr, target_samplerate/100000.0, 'sinc_best') write_wav(sampled, filename_head + ".wav", 100000, 32700)

          C 1 Reply Last reply
          0
          • A Austin_Cpp

            Here is the only code I can find for writing a .Wav file. This is python which I know nothing about. I have .csv files of data that when plotted draw a mandala. I want to save the same file to a .wav format so I can play the mandala as sound. Is writing a wav file possible with C++ code or do I need a library? https://gist.github.com/Pretz/1773870 #!/usr/bin/python import wave import numpy import struct import sys import csv from scikits.samplerate import resample def write_wav(data, filename, framerate, amplitude): wavfile = wave.open(filename, "w") nchannels = 1 sampwidth = 2 framerate = framerate nframes = len(data) comptype = "NONE" compname = "not compressed" wavfile.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname)) print("Please be patient whilst the file is written") frames = [] for s in data: mul = int(s * amplitude) # print "s: %f mul: %d" % (s, mul) frames.append(struct.pack('h', mul)) # frames = (struct.pack('h', int(s*self.amp)) for s in sine_list) frames = ''.join(frames) for x in xrange(0, 7200): wavfile.writeframes(frames) wavfile.close() print("%s written" %(filename)) if __name__ == "__main__": if len(sys.argv) <= 1: print "You must supply a filename to generate" exit(-1) for fname in sys.argv[1:]: data = [] for time, value in csv.reader(open(fname, 'U'), delimiter=','): try: data.append(float(value)) except ValueError: pass # Just skip it print "Generating wave file from %d samples" % (len(data),) arr = numpy.array(data) # Normalize data arr /= numpy.max(numpy.abs(data)) filename_head, extension = fname.rsplit(".", 1) # Resample normalized data to 44.1 kHz target_samplerate = 44100 sampled = resample(arr, target_samplerate/100000.0, 'sinc_best') write_wav(sampled, filename_head + ".wav", 100000, 32700)

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            You know, this piece of code does use a libraries (import wave, etc..). With C++ you can either use a library or write the calsses and methods (for instance setparams) yourself.

            Veni, vidi, vici.

            1 Reply Last reply
            0
            • A Austin_Cpp

              I am looking for c++ code that can load a .wav audio file and output a .csv text file. Then convert it back again. That is wav-to-csv and csv-to-wav. This is an unusual conversion process so any help is welcome.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Other response suggests you want to go from csv to wav. Steps 1. Determine for all given values in csv how them map to a 'sound'. There are any number of ways to do this. As one example you might pick a small audible music scale and then discretely break up the entire range of csv numbers along this scale. This step does NOT involve code. 2. Write code to parse the csv 3. Find or write code that outputs a wav file 4. Write code that takes the csv data, maps it via what you determined in 1, and then using 3 write it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups