Jump to content

Tricky college assignment

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
sandbagger

sandbagger

    Newbie

  • Members
  • Pip
  • 1 posts
Hey guys this is my fist post here...

I have a college assgnment to do but im am not quite sure what my lecturer wants me to do?

This is the assignment itself:


Assignment 4 Sine wave sounds



<!--[if !supportLists]-->1)<!--[endif]-->Create a 32 element array containing one period of a sine wave by calling the sine function 32 times incrementing the angle by 32/360 degrees which will result is a full period in the array. The frequency of this signal will depend on the sampling frequency at which it is played back and the number of samples in one period.

<!--[if !supportLists]-->2)<!--[endif]-->Convert the data array to a 16 bit signed integer array.

<!--[if !supportLists]-->3)<!--[endif]-->Write code so that the user can enter the frequency required and change the data in the array assuming the sampling is fixed at 8Khz

Sampling is at 8KHz one sample = 1/8000 Sec.

with an increment angle of 32/360 degrees one period = 32 samples, multiply by the time for 1 sample to get the period in seconds = 32*1/8000 = 4ms so the frequency of the sine wave is 250Hz. The angle in degrees is freq*360/sampling frequency.To convert the angle to radians use degrees*2*pi/360

Keep the array size the same but it now will contain more than one period.


<!--[if !supportLists]-->4)<!--[endif]-->Refine the code so that an error message is generated if the entered frequency is negative or greater than 4KHz

<!--[if !supportLists]-->5)<!--[endif]-->Increase the array size so that it will be long enough to hear say 4000 to 8000 samples and create a wav file using the function given. Add the function source file to your project so that you have two c file in your project and include the header.

<!--[if !supportLists]-->6)<!--[endif]-->Modify the code so that you can enter the note name instead of the frequency A,Bflat,B,C,C#,D,E,EflatF,F#,G,G#. To convert from note name to freq:freq= 440*2^(n/12). n =0 is A, n=1 is B flat, n=2 is B etc

<!--[if !supportLists]-->7)<!--[endif]-->Allow the user to put in say 2 extra harmonics to make up a chord. The A major chord is 440Hz, 550Hz and 660Hz, (A, C# and E).

<!--[if !supportLists]-->8)<!--[endif]-->Use the playsound function to listen to the sound file. PlaySound("mysound.wav",NULL,SND_FILENAME);

needs the winmm.lib to add the lib to your project:

project ->settings -> link->type in winmm.lib

and include windows.h in your source code.
<!--[if !supportLists]-->9)<!--[endif]-->Allow the user to control the length of the file by setting the array size to 800 and loading it into the file multiple times which will allow you to adjust the length in 0.1 sec increments. You will need to modify the make _wav function and the header as follows write a mak_header function to just write the wav header and open the file, close the file and load the data in main.



Mak_wav.h 

// header file to use the make_wav function

// sound is set for 8000 HZ sampling frequency and length is fixed at 1 sec i.e. 8000 samples, you must call the function with a pointer to a 8000 element array of 16 bit data i.e shorts

//the file created is called mysound.wav, any file existing of the same name in your folder will be overwritten

//returns 1 if unsucessful

// call like this fail=make_wav(signal)

// Mike Fahy 2008

// if you want the change the length you just change N here 

//other changes like sampling frequency or to make it stereo will have to be don in the function


#define N 8000//no of data samples

int make_wav(short*signal);



mak_wav_function.c
#include <stdio.h>


#include "make_wav.h"


int make_wav(short*signal)

{

FILE *fp;

char riff[4]={'R','I','F','F'};

char wave[4]={'W','A','V','E'};

char fmt[4]={'f','m','t',' '};

char data[4]={'d','a','t','a'};

short tag=1, channels=1,align=2, bits=16; // 16 bit values

int size =16036,format =16, sample_rate=8000, avg_bytes_sec=16000, data_size=16000; // 32 bit values


// set the file size and data size based on N defined in make_wav.h

size = 2*N +36;

data_size= 2*N;


fp = fopen("mysound.wav","wb");
if(fp==NULL)

{

printf("cannot open or create file");

return 1;

}


// write the header


fwrite(riff,sizeof(char),4,fp);

fwrite(&size,sizeof(int),1,fp);

fwrite(wave,sizeof(char),4,fp);

fwrite(fmt,sizeof(char),4,fp); 

fwrite(&format, sizeof(int),1,fp);

fwrite(&tag, sizeof(short), 1, fp);

fwrite(&channels, sizeof(short),1,fp);

fwrite(&sample_rate, sizeof(int), 1, fp);

fwrite(&avg_bytes_sec, sizeof(int), 1, fp);

fwrite(&align, sizeof(short), 1, fp);

fwrite(&bits, sizeof(short), 1, fp);

fwrite(data, sizeof(char),4,fp); 

fwrite(&data_size, sizeof(int), 1, fp); 


// write the data

fwrite(signal,sizeof(short), N, fp);


fclose(fp);


return 0;

}


Its a hell of a lot of code, if any of you guys could point me in the right direction it would be really appreciated!
Thanks alot:thumbup1:

Edited by WingedPanther, 06 December 2009 - 08:20 PM.
add code tags (the # button)


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Please use code tags. (Highlight your code and click the # button.) Is there something specific you're having a problem with? We don't do your homework for you here, but we will help you to the best of our ability.
sudo rm -rf /