Hello!

I’ve started a project that allows users to edited wav files (flange, distortion) and but them in a sequence and make a loop.

I want to be able to visualize the actual sound wave of the file (like if you dropped a sound file in Audacity (and then in future adapt it to what is playing)) and this is where I’ve hit a brick wall!

I’m not sure what to really do in order to accomplish this! I’ve tried reading the bits (as used with the adding effects) and using draw line to paint a frequency. This has made something, but defiantly not what the sound wave looks like!! (i checked it using Audacity/Fruityloops)

does anyone have any ideas for me where to start?!

thanks alot! =)

Recommended Answers

All 10 Replies

epic bump?

ive still not got anywhere with this =(

Are you able to obtain an amplitude for every sample from beginning to end from the wav file? That's the information you need.
So if the wave is sampled at 44.1 KHz, you have an amplitude at every 1/44100 of a second. And if it's 16-bit PCM encoded, then the amplitude is maximum at 2^(16-1) and lowest at 0. If you're already aware of this, then you probably need to study the wav format.

im using the .wav header file to get the structure

struct TRiff { char RiffStr[4];     // RIFF
               DWORD ChunkSize;     // Size of file after this point
               char WaveStr[4];     // WAVE
               char FmtStr[4];      // fmt
               DWORD FmtChunkLen;   // Length of data to this point 00000010
               WORD NoIdea;         // Seems to always be 0001
               WORD NumChannels;    // 1=mono 2=stereo
               DWORD SampleRate;    // in Hz
               DWORD BytesPerSec;
               WORD BytesPerSample; // 1 for 8 bit 2 for 16 bit
               WORD BitsPerSample;  // 8 for 8 0x10 for 16
               char DataStr[4];     // data
               DWORD DataLen;       // Length of raw data block
             };

and then when opening the file use these bits of code

if (!OpenDialog1->Execute()) return;

  AnsiString FileName;

  SoundFileName=OpenDialog1->FileName;
  Caption = SoundFileName;

  FileName=ExtractFileName(SoundFileName); // Get name without path

  if (UpperCase(ExtractFileExt(FileName)) !=  ".WAV")
  {
    ShowMessage("File has to be a .WAV type");
    return;
  }

  // If sample block already allocated from before then free it first
  if (SoundFileData) delete []SoundFileData;

  if (!LoadFile())//mSourceFileName, mpWaveData, &mSourceHeader))
  {
    ShowMessage("Failed to load data from .wav file");
    return;
  }
bool TForm1::LoadFile()
{
  int FileHandle;
  int BytesRead;

  FileHandle = FileOpen(SoundFileName, fmOpenRead);

  if (FileHandle >= 0)  // File  opened
  {
    BytesRead = FileRead(FileHandle, (void*)&mWaveHeader, sizeof(TRiff));

    if (BytesRead == sizeof(TRiff))  // Read header ok
    {
      SoundFileData = new unsigned char[mWaveHeader.DataLen];
      BytesRead = FileRead(FileHandle, (void*)SoundFileData, mWaveHeader.DataLen);
      FileClose(FileHandle);
      return (BytesRead == mWaveHeader.DataLen); // Return true if data read ok else false
    } // if failed to read header returns false
  }

  FileClose(FileHandle);
  return false;
}

so ive got the sound data in SoundFileData.

im really confused about how to use this to draw the sound wave.
ive been reading alot about FFT but not sue how i would use that with the data i have.

thanks in advance

http://www.intmath.com/Fourier-series/7_Fast-fourier-transform-FFT.php

I believe that's only useful for describing
the wave mathematically, as a series of sines and cos waves added together (which is what the Fourier Series is) and perhaps more importantly generating an analog wave from the digital values in electronics.

I've never studied the wav format but I know about PCM, which is what the data in a wav is. So I'm failing to see why you couldn't just plot a wave using the quantized values you read from the .wav. You could try plotting them in Excel and comparing the graph to Audacity.

I've never studied the wav format but I know about PCM, which is what the data in a wav is. So I'm failing to see why you couldn't just plot a wave using the quantized values you read from the .wav. You could try plotting them in Excel and comparing the graph to Audacity.

hmm how would i do that?

If you have the actual sound data in some sort of array/list and the frequency, then just create a for loop to iterate each element - and draw pixel by pixel the data to the screen (with some sort of drawer lib). This will take a bit of math (to fit it to the required dimensions), but not likely any sort of infinite series calculations. If you want to generate a wav, however, fourier series is a good place to start.

Perhaps you could post your distortion/flange functions, as they will demonstrate the use of the wav data you loaded into the struct, with which I could sugguest a more concrete method to solve this problem.

hmm how would i do that?

An XY graph with amplitude on Y-axis and time on the X-axis. Excel or any software with good graphing capabitlites should do.

This is how 5 samples of 8-bit PCM with a sampling frequency of 1KHz would look.

well as i posted abvove i get the data of the wav file in array SoundFileData[].

this enables me to add effects like echo

for (int i=0; i < mWaveHeader.DataLen; i++)
   {
    
     if (i >= Echo) DelayedSample=mWaveData[i-Echo]; // delayed sample val
      else DelayedSample=128;  // Silence

     DelayedSample-=128; // Normalize to zero
     CurrentSample=(int)(SoundFileData[i]);
     CurrentSample-=128; // Normalize to zero
     CurrentSample=floor(((double)DelayedSample*0.7)+(double)CurrentSample); // Echo with fixed depth

     Clip(CurrentSample);
     CurrentSample+=128;  // Un-normalize

     soundbuffer[i]=(unsigned char)CurrentSample; // Copy modified sample to buffer


   }

ive tried drawing the raw values from the array to a canvas, and it comes out with a "wave" form but im in alot of doubt as to that being the actuall soundwave form, since comparing it with other aps its compleatly different.

Hmmmm well I would sugguest that you perhaps try with some simple waveforms to see exactly what is being graphed. Try using waveforms such as a constant 100Hz signal, a fading 5Hz signal, etc. If you are unsure of how to generate these I could email you some to work with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.