Converting a wav file to an array

Hello everyone. You need to perform a DFT conversion of the wav file. The function is available, graphs are plotted(based on numerical data), I do not understand how to pull data from a wav file to process them. All of the above is implemented in Qt. I don't really know Qt myself, I study it in a hurry. Help us implement a function that "pulls" data from a wav file to plot a graph / spectrum.

Author: Good Boi, 2020-03-09

2 answers

First, you need to determine the file format, frequency, bit rate, and number of channels.

If PCM - then everything is simple. For example, for 16-bit, 44kHz stereo:

You skip the file header, then move the pointer 32 bits, 16 from one channel, 16 from another. the second buffer is 44000 times so move the pointer. You throw it in mono. For example, you can ignore the second channel. I mixed 2 channels together according to some formula.

For mono sound with linear type conversion (int16 *) get the data for the DFT formula.

Https://www.cyberforum.ru/qt/thread1493544.html (https://www.cyberforum.ru/post7846511.html) read here

If there is adpcm, mp3, ac3 or something else, then you need to pass it through the codec. The phonon or vlc library will help.

 2
Author: eri, 2020-03-09 19:54:17

I found this joke: There is a WAVESTRUCT structure, which, as I understand it, describes all the" jokes "of a WAV file `'

char                chunkId[4];                 // Информация о формате файла (RIFF), Содержит символы “RIFF” в ASCII кодировке;
unsigned long       ChunkSize;                  // Размер без  chunkId[4];
char                format[4];                  // Формат потоковых данных (WAVE);
char                subchunk1Id[4];             // Описание параметров WAV-файла (fmt-chunk);
unsigned long       subchunk1Size;              // Размер подструктуры  subchunk1  (16 байт);
unsigned short      audioFormat;                // Аудио формат (PCM = 1);
unsigned short      nChannels;                  // Количество каналов (Моно = 1, Стерео = 2);
unsigned long       SamplesRate;                // Частота дискретизации в Гц;
unsigned long       ByteRate;                   // Кол-во передаваемых байт в секунду воспроизведения;
unsigned short      blockAlign;                 // Размер сэмпла в байтах 16 бит = 2 байта моно, 32 бита = 4 байта стерео (включая все каналы);
unsigned short      BitsPerSample;              // Количество бит в сэмпле. Так называемая “глубина” или точность звучания. 8 бит, 16 бит и т.д. /// битов на отсчет
char                Subchunk2ID[4];             // Символы "Data", начало чанка данных;
unsigned long       Subchunk2Size;              // Размер области данных в байтах;
int             dataSize;
unsigned char data[]; 
//vector<unsigned char> data;

Below is the main function:

setlocale(0, "");
FILE *file;
errno_t error;
error = fopen_s(&file, "C:/Filename.wav", "rb");
if (error)
{
    cout << "Не удалось открыть файл\n";
    system("pause");
    return 0;
}

WAVHEADER header;
fread_s(&header, sizeof(WAVHEADER), sizeof(WAVHEADER), 1, file);
// Выводим данные из сегмента data

int lenght = sizeof(&header.data) / sizeof(unsigned char);
for (int i = 0; i <  header.dataSize; i++)
{
    cout << i << " " << static_cast<int>(header.data[i]) << endl;
}

fclose(file);

system("pause");
return 0;

` If you output all the data from the structure, then everything seems to be fine there.The dataSize field was added so that the size of the array is not fixed and depends on the file itself.The scary thing is that the dataSize value for a file with a length of 5 seconds is 1330007625. Is this how it should be or did I mix something up?

 0
Author: Good Boi, 2020-03-11 23:38:04