Centar za edukaciju-BiH


Stranice (1):1

#1 06.02.2014 12:29
Amelasar Van mreze
Clan
Registrovan od:07.04.2011
Postovi:262


Predmet:Zadatak za ispit
Zadatak: https://palme.iicm.tugraz.at/wiki/EP/AssA_WS13

Zapoceto rjesenje:

PreuzmiIzvorni kôd (Text):
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdint.h>
  4. #include <limits.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define C 0
  9. #define D 2
  10. #define E 4
  11. #define A 9
  12. struct _WaveHeader_
  13. {
  14.   char     chunk_id_[4];
  15.   uint32_t chunk_size_;
  16.   char     riff_type_[4];
  17.  
  18.   char     fmt_header_[4];
  19.   uint32_t fmt_length_;
  20.   uint16_t format_tag_;
  21.   uint16_t channels_;
  22.   uint32_t sample_rate_;
  23.   uint32_t bytes_per_sec;
  24.   uint16_t frame_size_;
  25.   uint16_t bits_per_sample_;
  26.  
  27.   char     data_header_[4];
  28.   uint32_t data_length_;
  29. } __attribute__((packed));
  30.  
  31.  
  32. //main funkcija
  33. int main(int argc, char *argv[])
  34. {
  35.   char msg1[] = "Usage: ./assa [-bpm <beats_per_min>] [-o <output_file>] <input_files>\n";
  36.   char msg2[] = "Error: can not read input file.\n";
  37.   char msg3[] = "Error: invalid input file.\n";
  38.   char msg4[] = "Error: can not write output file.\n";
  39.   char msg5[] = "Error: out of memory.\n";
  40.  
  41.   char *filename = "out.wav";
  42.   int bpm = 120;
  43.   char *p_bpm = NULL;
  44.   char *p_o = NULL;
  45.   int i,k;
  46.   char** text = ".txt";
  47.   printf("%s",text);
  48.   for(i=1; i<argc; i++)
  49.   {
  50.  
  51.   }
  52.  
  53.  for(i=1; i<argc; i++)
  54.   {
  55.    if (strcmp(argv[i],"-bpm")==0)//          ||
  56.    {
  57.     if(i+1==argc)
  58.      {
  59.       printf(msg1);
  60.       return 1;
  61.      }
  62.      else
  63.       {bpm=atoi(argv[i+1]);}
  64.    }
  65.  
  66.   if(strcmp(argv[i],"-o")==0)
  67.   {
  68.    if(i+1==argc)
  69.    {
  70.     printf(msg1);
  71.     return 1;
  72.    }
  73.    else
  74.    {filename = argv[i+1];}
  75.   }
  76.   }
  77.  
  78.  
  79.   FILE *file = fopen(filename, "wb");
  80.   if (file == NULL)
  81.     return 2;
  82.  
  83.    uint32_t sample_rate = 44100;
  84.    uint32_t sample_full_note = (sample_rate * 60 * 4)/bpm; //88200 na 120 bpm
  85.    //float duratation = 0.5;
  86.    uint32_t total_samples = 10 * sample_full_note;
  87.  
  88.    writeWaveHeader(file, total_samples);
  89.  
  90.  
  91.     makeTone(file, frequency(D,2), sample_full_note/8);//D----EXAMPLE-----
  92.     makeTone(file, frequency(-1,4), sample_full_note/8);//E
  93.     makeTone(file, frequency(-1,5), sample_full_note/8);//F
  94.     makeTone(file, frequency(-1,7), sample_full_note/8);//G
  95.     makeTone(file, frequency(-1,9), sample_full_note/8);//A
  96.     makeTone(file, frequency(-1,4), sample_full_note/8);//E
  97.     makeTone(file, frequency(-1,9), sample_full_note/4);//A
  98.     makeTone(file, frequency(-1,8), sample_full_note/8);//G#
  99.     makeTone(file, frequency(-1,4), sample_full_note/8);//E
  100.     makeTone(file, frequency(-1,8), sample_full_note/4);//G#
  101.     makeTone(file, frequency(-1,7), sample_full_note/8);//G
  102.     makeTone(file, frequency(-1,3), sample_full_note/8);//Eb
  103.  
  104.  
  105.  
  106.  
  107.   //}
  108.   fclose(file);
  109.  
  110.   return 0;
  111. }
  112.  
  113. int open_txt(char* text_name)
  114. {
  115.  char chars = 0;
  116.  FILE *text_file;
  117.  text_file = fopen("mountain1.txt","r");
  118.  if(text_file==0)
  119.  {
  120.   printf("Error: can not read input file.\n");
  121.   return 2;
  122.  }
  123. }
  124.  
  125.  
  126. int writeWaveHeader(FILE *file, uint32_t total_samples)
  127. {
  128.   struct _WaveHeader_ header;
  129.   strcpy(header.chunk_id_, "RIFF");
  130.   header.chunk_size_ = 2 * total_samples + 8 + 24 + 4;
  131.   strcpy(header.riff_type_, "WAVE");
  132.   strcpy(header.fmt_header_, "fmt ");
  133.   header.fmt_length_ = 16;
  134.   header.format_tag_ = 1; // PCM
  135.   header.channels_ = 1; // mono channel
  136.   header.sample_rate_ = 44100;
  137.   header.bits_per_sample_ = 16; // short type
  138.   header.frame_size_ = header.channels_ * ((header.bits_per_sample_ + 7) / 8);
  139.   header.bytes_per_sec = header.sample_rate_ * header.frame_size_;
  140.   strcpy(header.data_header_, "data");
  141.   header.data_length_ = 2*total_samples;
  142.  
  143.   if (fwrite(&header, sizeof(header), 1, file) != 1)
  144.     return 4;
  145.  
  146.   return 0;
  147. }
  148.  
  149.  
  150.  
  151. int frequency(int octave,int offset)
  152. {
  153.  int note_num;
  154.  note_num = (5 + octave) * 12 + offset;
  155.  printf(" midi je %d\n",note_num);
  156.  
  157.  float offset_num = 12;
  158.  float exp = (note_num - 69) / offset_num;
  159.  float f;
  160.  f = pow(2,exp)*440;
  161.  printf("frekvencija je %f",f);
  162.  return f;
  163. }
  164.  
  165.  
  166. int makeTone(FILE *file, uint32_t freq, uint32_t ton_duratation)
  167. {
  168.  printf("%d\n",freq);
  169.   uint32_t sample_count;
  170.   for (sample_count = 1; sample_count <= ton_duratation; sample_count++)
  171.   {
  172.     double time = ((sample_count) / (44100.00)) * freq;
  173.     short int buff = (65535) * (0.5 * sin(time * 2.0 * M_PI) + 0.5);
  174.     fwrite(&buff, sizeof(buff), 1, file);
  175.  
  176.   }
  177.   return 0;
  178. }

Zadatak je započet. Trebalo bi ga dovrÅ¡iti. Vrijeme je ograničeno, mislim do 09.02. Može li neko zavrÅ¡iti, naravno uz naknadu. Može PP.

Ako tema nije uredu molila bi administratora da je obriše.

Hvala.
Pozdrav, Amela
Ovaj post je ureden 1 puta. Posljednja izmjena 06.02.2014 12:31 od strane Amelasar. ↑  ↓

Stranice (1):1


Sva vremena su GMT +01:00. Trenutno vrijeme: 5: 55 am.