Reply by Martin Eisenberg November 12, 20062006-11-12
m.topczewski@gmail.com wrote:

> sound_buffer = (short *) malloc (sizeof(short) * > data_size/size_per_sample); > > This looks ok?
No ;) That will work *if* size_per_sample == sizeof(short) but is then unnecessarily circuitous. If you ultimately want your program to handle arbitrary frame widths or at least a range of common values, you will need to piece the samples together from the raw bytes with bitwise operations or else duplicate the affected code. Actually, I recommend you stop mucking about with that stuff and use libsndfile: http://www.mega-nerd.com/libsndfile/ Martin -- We don't often think of the lavish jeweled scarabs that decorated the mummies as representations of dung beetles who nourished their /pupae/ in excrement. We have, however, appropriated this term into English as /pupil/. -- B. Hagens, Timbre of the Spheres
Reply by November 12, 20062006-11-12
You are right:)
I was thinking aobut such a formula - but when posting did a stupid
copy/paste without thinking.

sound_buffer = (short *) malloc (sizeof(short) *
data_size/size_per_sample);

This looks ok?

Martin Eisenberg napisal(a):
> m.topczewski@gmail.com wrote: > > > Martin - sound_buffer = (short *) malloc (sizeof(BYTE) * > > data_size); or maybe > > sound_buffer = (short *) malloc (sizeof(short) * data_size); ? > > No; the data_size field is in units of bytes, not frames. > > > Martin > > -- > Quidquid latine scriptum sit, altum viditur.
Reply by Martin Eisenberg November 12, 20062006-11-12
m.topczewski@gmail.com wrote:

> Martin - sound_buffer = (short *) malloc (sizeof(BYTE) * > data_size); or maybe > sound_buffer = (short *) malloc (sizeof(short) * data_size); ?
No; the data_size field is in units of bytes, not frames. Martin -- Quidquid latine scriptum sit, altum viditur.
Reply by November 12, 20062006-11-12
Martin - sound_buffer = (short *) malloc (sizeof(BYTE) * data_size); or
maybe
sound_buffer = (short *) malloc (sizeof(short) * data_size); ?

Martin Eisenberg napisal(a):
> m.topczewski@gmail.com wrote: > > > sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); > > As you already suspected, that line should read: > > sound_buffer = (short *) malloc (sizeof(BYTE) * data_size); > > > Martin > > -- > Quidquid latine scriptum sit, altum viditur.
Reply by Martin Eisenberg November 11, 20062006-11-11
m.topczewski@gmail.com wrote:

> sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size);
As you already suspected, that line should read: sound_buffer = (short *) malloc (sizeof(BYTE) * data_size); Martin -- Quidquid latine scriptum sit, altum viditur.
Reply by Rune Allnor November 11, 20062006-11-11
m.topczewski@gmail.com skrev:
> Concerning the formula, You are absolutley right. > > The code is suppose to apply hamming window to a signal, in order to > allow smooth assembly of audio data.
Hmmm.... I'm not sure the Hamming window is the best way to achieve what you want. The window functions are used for side lobe supression, a task that is needed only in filtering and spectrum analysis. With a little bit of luck, there might be some some audio guy around, who can explain how editing of audio data is usually done. Rune
Reply by November 11, 20062006-11-11
Concerning the formula, You are absolutley right.

The code is suppose to apply hamming window to a signal, in order to
allow smooth assembly of audio data.

This part of code is supposed to only apply window function.

Do you know any way to get into the sound_buffer data in such a way,
that one element of an array will represent one sample? For now, the
data is being represented by two elements.

My audio source is a mono 16KHz, 16-bit audio file.

Rune Allnor napisal(a):
> > There is one implementation detail that you need to check: I suspect > the > correct expression for the window function involves a term like > > cos(2*M_PI*z/(N-1)); > > (pi is represented to working precision, N-1 instead of N in the cosine > > term). > > Apart from that, I can't quite figure out what this code supposed to > do. Merely > windowing the data makes little sense to me, except as part of some FIR > filter. > But that would require a convolution somewhere, which I can't find. > > What do you expect your code to do? > > Rune
Reply by Rune Allnor November 10, 20062006-11-10
m.topczewski@gmail.com skrev:
> Hello, > I'm new to dsp, still much to learn. > I'm working on something that will apply window function to signal read > from a .wav file. > Until now I've developed a code that reads the data and loads it into > memory buffer. > The code looks like
As far as I can tell, this is the "business part" of the code:
>for (z=0;z<N;z++) > { > //windowing > window=0.54-0.46*cos(2*3.14*z/N);//hamming window function > sound_buffer[z]=window*sound_buffer[z]; > printf("window = %f",window); > printf("buffer = %d \n",sound_buffer[z]); > > } > //write data after windowing > fwrite(sound_buffer,sizeof(BYTE),data_size,fout);
> Assuming that the file is small enough it will be possible to process > it in the window function. > I cant get it to work - i get many distorions of the signal, sound > becomes unreadable - can someone help me with, becouse i went through > it many times, and still no clue about what is wrong. > > Any help would appreciated.
There is one implementation detail that you need to check: I suspect the correct expression for the window function involves a term like cos(2*M_PI*z/(N-1)); (pi is represented to working precision, N-1 instead of N in the cosine term). Apart from that, I can't quite figure out what this code supposed to do. Merely windowing the data makes little sense to me, except as part of some FIR filter. But that would require a convolution somewhere, which I can't find. What do you expect your code to do? Rune
Reply by November 10, 20062006-11-10
John Sampson napisal(a):
> m.topczewski@gmail.com wrote: > > Hello, > > I'm new to dsp, still much to learn. > > I'm working on something that will apply window function to signal read > > from a .wav file. > > Until now I've developed a code that reads the data and loads it into > > memory buffer. > > The code looks like: > > "int main(int argc, char *argv[]) > > { > > FILE *fwyj,*fout; > > double ts,tk,time; > > float f0,pr,window; > > int z,N; > > > > long start_byte,end_byte; > > long new_data_size=0; > > > > fwyj = fopen("c:\\input.wav","rb"); > > if (fwyj==NULL) > > { > > printf("ERROR: File is missing !!\n"); > > system("PAUSE"); > > exit(1); > > } > > > > fout = fopen("output.wav","wb"); > > > > if (fwyj) > > { > > BYTE id[5], *sound_buffer; > > id[4]='\0'; > > DWORD size; > > short format_tag, channels, block_align, bits_per_sample; > > DWORD format_length, sample_rate, avg_bytes_sec, data_size; > > > > fread(id, sizeof(BYTE), 4, fwyj); > > fwrite(id,sizeof(BYTE), 4,fout); > > > > if (!strcmp((char *)id, "RIFF")) > > { fread(&size, sizeof(DWORD), 1, fwyj); > > fwrite(&size, sizeof(DWORD), 1, fout); > > fread(id, sizeof(BYTE), 4, fwyj); > > fwrite(id, sizeof(BYTE), 4, fout); > > if (!strcmp((char *)id,"WAVE")) > > { > > fread(id, sizeof(BYTE), 4, fwyj); > > fwrite(id, sizeof(BYTE), 4, fout); > > fread(&format_length, sizeof(DWORD),1,fwyj); > > fwrite(&format_length, sizeof(DWORD),1,fout); > > fread(&format_tag, sizeof(short), 1, fwyj); > > fread(&channels, sizeof(short),1,fwyj); > > fread(&sample_rate, sizeof(DWORD), 1, fwyj); > > fread(&avg_bytes_sec, sizeof(DWORD), 1, fwyj); > > fread(&block_align, sizeof(short), 1, fwyj); > > fread(&bits_per_sample, sizeof(short), 1, fwyj); > > fread(id, sizeof(BYTE), 4, fwyj); > > > > fwrite(&format_tag, sizeof(short), 1, fout); > > fwrite(&channels, sizeof(short),1,fout); > > fwrite(&sample_rate, sizeof(DWORD), 1, fout); > > fwrite(&avg_bytes_sec, sizeof(DWORD), 1, fout); > > fwrite(&block_align, sizeof(short), 1, fout); > > fwrite(&bits_per_sample, sizeof(short), 1, fout); > > fwrite(id, sizeof(BYTE), 4, fout); > > > > fread(&data_size, sizeof(DWORD), 1, fwyj); > > fwrite(&data_size, sizeof(DWORD), 1, fout); > > > > sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); > > fread(sound_buffer, sizeof(BYTE), data_size, fwyj); > > N=4096; //may be different > > for (z=0;z<N;z++) > > { > > //windowing > > window=0.54-0.46*cos(2*3.14*z/N);//hamming window function > > sound_buffer[z]=window*sound_buffer[z]; > > printf("window = %f",window); > > printf("buffer = %d \n",sound_buffer[z]); > > > > } > > //write data after windowing > > fwrite(sound_buffer,sizeof(BYTE),data_size,fout); > > } > > > > } > > > > } > > system("PAUSE"); > > return EXIT_SUCCESS; > > } > > > > Assuming that the file is small enough it will be possible to process > > it in the window function. > > I cant get it to work - i get many distorions of the signal, sound > > becomes unreadable - can someone help me with, becouse i went through > > it many times, and still no clue about what is wrong. > > > > Any help would appreciated. > > > > It looks like it will only work if the file contains exactly 4096 > samples. There is also the possibility that the wave header isn't being > read correctly. Have you confirmed that sound_buffer[0] contains the > very first audio sample in the file, and that data_size is correct? > > John
I'm sure, that the data_size is correct, and that the correct sample is in the buffer. Doesn't matter what the size of N is, it doesn't work still for numbers like 1024 or 2048. I guess that it is not the case of size of window. Assume that input file is 0.2 sec long. Problem may be related (but i'm not sure) with the fact that each sample is divided into two elements of array.
Reply by John Sampson November 10, 20062006-11-10
m.topczewski@gmail.com wrote:
> Hello, > I'm new to dsp, still much to learn. > I'm working on something that will apply window function to signal read > from a .wav file. > Until now I've developed a code that reads the data and loads it into > memory buffer. > The code looks like: > "int main(int argc, char *argv[]) > { > FILE *fwyj,*fout; > double ts,tk,time; > float f0,pr,window; > int z,N; > > long start_byte,end_byte; > long new_data_size=0; > > fwyj = fopen("c:\\input.wav","rb"); > if (fwyj==NULL) > { > printf("ERROR: File is missing !!\n"); > system("PAUSE"); > exit(1); > } > > fout = fopen("output.wav","wb"); > > if (fwyj) > { > BYTE id[5], *sound_buffer; > id[4]='\0'; > DWORD size; > short format_tag, channels, block_align, bits_per_sample; > DWORD format_length, sample_rate, avg_bytes_sec, data_size; > > fread(id, sizeof(BYTE), 4, fwyj); > fwrite(id,sizeof(BYTE), 4,fout); > > if (!strcmp((char *)id, "RIFF")) > { fread(&size, sizeof(DWORD), 1, fwyj); > fwrite(&size, sizeof(DWORD), 1, fout); > fread(id, sizeof(BYTE), 4, fwyj); > fwrite(id, sizeof(BYTE), 4, fout); > if (!strcmp((char *)id,"WAVE")) > { > fread(id, sizeof(BYTE), 4, fwyj); > fwrite(id, sizeof(BYTE), 4, fout); > fread(&format_length, sizeof(DWORD),1,fwyj); > fwrite(&format_length, sizeof(DWORD),1,fout); > fread(&format_tag, sizeof(short), 1, fwyj); > fread(&channels, sizeof(short),1,fwyj); > fread(&sample_rate, sizeof(DWORD), 1, fwyj); > fread(&avg_bytes_sec, sizeof(DWORD), 1, fwyj); > fread(&block_align, sizeof(short), 1, fwyj); > fread(&bits_per_sample, sizeof(short), 1, fwyj); > fread(id, sizeof(BYTE), 4, fwyj); > > fwrite(&format_tag, sizeof(short), 1, fout); > fwrite(&channels, sizeof(short),1,fout); > fwrite(&sample_rate, sizeof(DWORD), 1, fout); > fwrite(&avg_bytes_sec, sizeof(DWORD), 1, fout); > fwrite(&block_align, sizeof(short), 1, fout); > fwrite(&bits_per_sample, sizeof(short), 1, fout); > fwrite(id, sizeof(BYTE), 4, fout); > > fread(&data_size, sizeof(DWORD), 1, fwyj); > fwrite(&data_size, sizeof(DWORD), 1, fout); > > sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); > fread(sound_buffer, sizeof(BYTE), data_size, fwyj); > N=4096; //may be different > for (z=0;z<N;z++) > { > //windowing > window=0.54-0.46*cos(2*3.14*z/N);//hamming window function > sound_buffer[z]=window*sound_buffer[z]; > printf("window = %f",window); > printf("buffer = %d \n",sound_buffer[z]); > > } > //write data after windowing > fwrite(sound_buffer,sizeof(BYTE),data_size,fout); > } > > } > > } > system("PAUSE"); > return EXIT_SUCCESS; > } > > Assuming that the file is small enough it will be possible to process > it in the window function. > I cant get it to work - i get many distorions of the signal, sound > becomes unreadable - can someone help me with, becouse i went through > it many times, and still no clue about what is wrong. > > Any help would appreciated. >
It looks like it will only work if the file contains exactly 4096 samples. There is also the possibility that the wave header isn't being read correctly. Have you confirmed that sound_buffer[0] contains the very first audio sample in the file, and that data_size is correct? John