reverses wav file data

hello i have problem of reverse wav data for my assignment.

the assignment require me to write a C++ program that reads in a WAVE file, reverses the audio samples, and writes the result to a new WAVE file.

my result don't tally with expected given result as the image show

my result
https://imgur.com/MX0HWMt

expected result
https://imgur.com/OZnlzR3

here my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#include <iostream>
#include <fstream>
#include <cstring>

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#define EIGHT_BYTES 8


struct WaveHeader {
 char riff_label[4]; // (offset 0) = {‘R’, ‘I’, ‘F’, ‘F’}
 unsigned riff_size; // (offset 4) = 36 + data_size
 char file_tag[4]; // (offset 8) = {‘W’, ‘A’, ‘V’, ‘E’}
 char fmt_label[4]; // (offset 12) = {‘f’, ‘m’,’t’, ’ ‘}
 unsigned fmt_size; // (offset 16) = 16
 unsigned short audio_format; // (offset 20) = 1
 unsigned short channel_count; // (offset 22) = 1 or 2
 unsigned sampling_rate; // (offset 24) = <anything>
 unsigned bytes_per_second; // (offset 28) = <ignore>
 unsigned short bytes_per_sample; // (offset 32) = <ignore>
 unsigned short bits_per_sample; // (offset 34) = 16
 char data_label[4]; // (offset 36) = {‘d’, ‘a’, ‘t’, ‘a’}
 unsigned data_size; // (offset 40) = <# of bytes of data>
};

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

void RunReserveProgram(const char * input , const char * output)
{
  
}
void PrintUsage()
{
  std::cout << "Usage:" << std::endl;
  std::cout << "\treverse <wave in> <wave out> " << std::endl;
  std::cout << "where:" << std::endl;
  std::cout << "\t<wave in> -- input wave file " << std::endl;
  std::cout << "\t<wave out>-- output wave file" << std::endl;
  exit(EXIT_SUCCESS);
}

void ErrorMsg()
{
  std::cout << "incorrect number of arguments "<< std::endl;
  exit(EXIT_FAILURE);
}

int main(int argc, char** argv)
{
  if(argc == 4)
  {
    RunReserveProgram(argv[2],argv[3]);
  }
  else if(argc == 2)
  {
    PrintUsage();
  }
  else
  {
     ErrorMsg();
  }
 
}


Last edited on
At line 81, ptr2 is off by one. It points just past the end of the array rather than at the last element.

That should make the code work, but here are some other comments:

Your code will only work with 16 bit samples. That's probably okay, but I thought I'd point it out.

Line 74: you're allocating an array of 2 shorts, but you only use one. There's actually no need for an array at all.

Your loop at 83-92 is kind of crazy. You use the the pointers to create an index into Data which results in pointers again. This seems easier to me:

1
2
3
4
5
6
7
8
    while (ptr1 < ptr2)
    {
      short tmp = *ptr1;
      *ptr1 = *ptr2;
      *ptr2 = tmp;
      ++ ptr1;
      -- ptr2;
    }


Or better yet:
1
2
3
    for (; ptr1 < ptr2; ++ptr1, --ptr2) {  // keep all the loop logic together
        swap(*ptr1, *ptr2);
    }
Topic archived. No new replies allowed.