- #include <stdio.h>
- #include <stdarg.h>
- #include <stdint.h>
- #include <limits.h>
- #include <math.h>
- #include <stdlib.h>
- #include <string.h>
- #define C 0
- #define D 2
- #define E 4
- #define A 9
- struct _WaveHeader_
- {
- char chunk_id_[4];
- uint32_t chunk_size_;
- char riff_type_[4];
- char fmt_header_[4];
- uint32_t fmt_length_;
- uint16_t format_tag_;
- uint16_t channels_;
- uint32_t sample_rate_;
- uint32_t bytes_per_sec;
- uint16_t frame_size_;
- uint16_t bits_per_sample_;
- char data_header_[4];
- uint32_t data_length_;
- } __attribute__((packed));
- //main funkcija
- int main(int argc, char *argv[])
- {
- char msg1[] = "Usage: ./assa [-bpm <beats_per_min>] [-o <output_file>] <input_files>\n";
- char msg2[] = "Error: can not read input file.\n";
- char msg3[] = "Error: invalid input file.\n";
- char msg4[] = "Error: can not write output file.\n";
- char msg5[] = "Error: out of memory.\n";
- char *filename = "out.wav";
- int bpm = 120;
- char *p_bpm = NULL;
- char *p_o = NULL;
- int i,k;
- char** text = ".txt";
- printf("%s",text);
- for(i=1; i<argc; i++)
- {
- }
- for(i=1; i<argc; i++)
- {
- if (strcmp(argv[i],"-bpm")==0)// ||
- {
- if(i+1==argc)
- {
- printf(msg1);
- return 1;
- }
- else
- {bpm=atoi(argv[i+1]);}
- }
- if(strcmp(argv[i],"-o")==0)
- {
- if(i+1==argc)
- {
- printf(msg1);
- return 1;
- }
- else
- {filename = argv[i+1];}
- }
- }
- FILE *file = fopen(filename, "wb");
- if (file == NULL)
- return 2;
- uint32_t sample_rate = 44100;
- uint32_t sample_full_note = (sample_rate * 60 * 4)/bpm; //88200 na 120 bpm
- //float duratation = 0.5;
- uint32_t total_samples = 10 * sample_full_note;
- writeWaveHeader(file, total_samples);
- makeTone(file, frequency(D,2), sample_full_note/8);//D----EXAMPLE-----
- makeTone(file, frequency(-1,4), sample_full_note/8);//E
- makeTone(file, frequency(-1,5), sample_full_note/8);//F
- makeTone(file, frequency(-1,7), sample_full_note/8);//G
- makeTone(file, frequency(-1,9), sample_full_note/8);//A
- makeTone(file, frequency(-1,4), sample_full_note/8);//E
- makeTone(file, frequency(-1,9), sample_full_note/4);//A
- makeTone(file, frequency(-1,8), sample_full_note/8);//G#
- makeTone(file, frequency(-1,4), sample_full_note/8);//E
- makeTone(file, frequency(-1,8), sample_full_note/4);//G#
- makeTone(file, frequency(-1,7), sample_full_note/8);//G
- makeTone(file, frequency(-1,3), sample_full_note/8);//Eb
- //}
- fclose(file);
- return 0;
- }
- int open_txt(char* text_name)
- {
- char chars = 0;
- FILE *text_file;
- text_file = fopen("mountain1.txt","r");
- if(text_file==0)
- {
- printf("Error: can not read input file.\n");
- return 2;
- }
- }
- int writeWaveHeader(FILE *file, uint32_t total_samples)
- {
- struct _WaveHeader_ header;
- strcpy(header.chunk_id_, "RIFF");
- header.chunk_size_ = 2 * total_samples + 8 + 24 + 4;
- strcpy(header.riff_type_, "WAVE");
- strcpy(header.fmt_header_, "fmt ");
- header.fmt_length_ = 16;
- header.format_tag_ = 1; // PCM
- header.channels_ = 1; // mono channel
- header.sample_rate_ = 44100;
- header.bits_per_sample_ = 16; // short type
- header.frame_size_ = header.channels_ * ((header.bits_per_sample_ + 7) / 8);
- header.bytes_per_sec = header.sample_rate_ * header.frame_size_;
- strcpy(header.data_header_, "data");
- header.data_length_ = 2*total_samples;
- if (fwrite(&header, sizeof(header), 1, file) != 1)
- return 4;
- return 0;
- }
- int frequency(int octave,int offset)
- {
- int note_num;
- note_num = (5 + octave) * 12 + offset;
- printf(" midi je %d\n",note_num);
- float offset_num = 12;
- float exp = (note_num - 69) / offset_num;
- float f;
- f = pow(2,exp)*440;
- printf("frekvencija je %f",f);
- return f;
- }
- int makeTone(FILE *file, uint32_t freq, uint32_t ton_duratation)
- {
- printf("%d\n",freq);
- uint32_t sample_count;
- for (sample_count = 1; sample_count <= ton_duratation; sample_count++)
- {
- double time = ((sample_count) / (44100.00)) * freq;
- short int buff = (65535) * (0.5 * sin(time * 2.0 * M_PI) + 0.5);
- fwrite(&buff, sizeof(buff), 1, file);
- }
- return 0;
- }