Audio Bit Depth problems

Hi guys,

I've been working on simple fourier transforms and outputting various waves as raw data. I've started outputting in other bit rates as a pose to just 16bit and am having some issues.
When importing into Audacity to test, 16 bit files still work as intended but 8 bit and 24 bit aren't as expected. Have I made a simple mistake or have i not grasped bit depth properly?

The below code asks the user for a filename, bit depth, then outputs a chord as a summation of square waves in 44.1kHz.

Thanks guys

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
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <limits>
#include <cmath>
using namespace std;

int main() {
    
    int frequency1 = 1000;                               // creates a variable for the frequency
    int frequency2 = 1250;
    int frequency3 = 1500;
    double amplitude = 0.8;                          // creates a variable for the amplitude
    double length = 4;                               // created a variable for the length of the wave
    int sampleRate = 44100;
    string filename;                                 // Created a variable to the name of the file
    int harmonicNum = 4;
    
    int chord [3] = {frequency1, frequency2, frequency3};         // created an array with 3 chord frequencies
    
    //Asking User for a File Name
    cout << "enter a filename" << endl;
    cin >> filename;
    
    
    // Asking user for bitrate.
    int switchcase = 1;
    cout << "What would you like the bitrate of your file to be?" << endl;
    cout << "Enter 1 for 8bit, Enter 2 for 16bit, Enter 3 for 24bit" << endl;
    cin >> switchcase;

    // Using a switch case to write the chosen bit rate to the variable
    int userBitrate = 16;
    float bitDepth = 32767;
    switch (switchcase) {
        case 1: {
            userBitrate = 8;
            bitDepth = 127;
            break;
        }
        case 2: {
            userBitrate = 16;
            bitDepth = 32767;
            break;
        }
        case 3: {
            userBitrate = 24;
            bitDepth = 8388607;
            break;
        }
    }
    cout << "The bitrate is: " << userBitrate << "bit." << endl << endl;
    
    
    int sampleCount = (int)(length * sampleRate);    // Worked out the total number of samples in the file
    ofstream ofs(filename, ios::binary);             // Creates and opens the file to send the data
    
    for (int x = 0; x < sampleCount; x++) {         // FOR loop to count each sample
        float t = x / (float)(sampleRate);
        float samplevalue = 0;
        
        for (int f = 0; f <= 2; f++) {       // FOR loop to cycle though 3 chord frequencies at every sample
            float chordFreq = 1;
            chordFreq = chord [f];          // unpacks frequencies from an array
            
            for (int k = 1; k <= harmonicNum; k++) {        // FOR loop to add harmonics
                samplevalue += (sin(t * chordFreq * 2 * M_PI * (2*k-1)) / (2*k-1)); // square wave formula
            }
            
        }
        int output = (samplevalue/3) * amplitude * bitDepth * (4 / M_PI);
        ofs.write((char*)(&output), sizeof(short));     // Writes the data to a rawdata file.
        
    }
    ofs.close();
    cout << "Your file has been saved as: " << filename << endl;
    
}
Last edited on
Line 71 ofs.write((char*)(&output), sizeof(short));
Why do you write only part out the output var ?
I've tried changing:

ofs.write((char*)(&output), sizeof(short));

to:

ofs.write((char*)(&output), sizeof(char));

Thinking that would work for 8bit output but still getting the same result?
Why are you only trying to write part of that int (output)? Shouldn't you be writing the complete value?



Topic archived. No new replies allowed.