我有一些用于创建wave文件的代码,我想将其转换为易于使用,可移植性和简单性的库。我目前有约150行代码,因此我想将该代码移到一个库中,然后仅使用几行来准备它,等等。我将以库形式一次删除一些行,但是我有点卡住。我是一个非常关心库的业余爱好者,但是我对普通的c ++编码了解很多。到目前为止,我只是使一个功能(文件大小())工作,但我一直在出错。
当我添加行时
#include "stdafx.h"
在我的代码中,出现错误:
1>------ Build started: Project: wavlib, Configuration: Debug Win32 ------
1>wavlib.cpp
1>c:\users\rowan\onedrive\documents\wavlib\wavlib\wavlib.cpp(5): error C2653: 'wavlib': is not a class or namespace name
1>c:\users\rowan\onedrive\documents\wavlib\wavlib\wavlib.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rowan\onedrive\documents\wavlib\wavlib\wavlib.cpp(5): error C2146: syntax error: missing ';' before identifier 'filesize'
1>c:\users\rowan\onedrive\documents\wavlib\wavlib\wavlib.cpp(6): error C2143: syntax error: missing ';' before '{'
1>c:\users\rowan\onedrive\documents\wavlib\wavlib\wavlib.cpp(6): error C2447: '{': missing function header (old-style formal list?)
1>Done building project "wavlib.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
如果我删除该#include语句,这些错误将消失,但我得到的错误是:
1>------ Build started: Project: wavlib, Configuration: Debug Win32 ------
1>wavlib.cpp
1>c:\users\rowan\onedrive\documents\wavlib\wavlib\wavlib.cpp(8): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Done building project "wavlib.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
当前代码(完整):
#include "stdafx.h"
using namespace std;
#include <iostream>
#include <fstream>
fstream wav("C:\\Users\\rowan\\OneDrive\\Desktop\\test.wav", ios::out | ios::binary | ios::trunc);
#include <chrono>
using namespace std::chrono_literals; // ns, us, ms, s, h, etc.
using std::chrono::system_clock;
#include <thread>
using namespace std::this_thread; // sleep_for, sleep_until
#include <random>
#define sampleRate 44100
#define bitDepth 16
#define channels 2
uint32_t kbps = (sampleRate * channels * bitDepth / 32) / 1024;
uint32_t bps = sampleRate * channels * bitDepth / 32;
uint32_t filesize();
void writeHeader(uint32_t rate, uint16_t depth, uint16_t chan);
void writeVal(uint32_t val);
bool state = 0;
int main()
{
//enlarge to 44 bytes, so there is room for the header
for (int i = 0; i < 44; i++) {
wav << '.';
}
for (uint64_t a = 0; a < bps; a++) {
writeVal(state * INT16_MAX);
state = !state;
}/**/
writeHeader(sampleRate, bitDepth, channels);
cout << "File size (kb): " << filesize() / 1024 << endl;
cout << "KBPS: " << kbps << endl;
cout << "Length: " << filesize() / bps / 4;
wav.close();
sleep_for(5s);
return 0;
}
uint32_t filesize()
{
streampos pos = wav.tellp();
wav.seekp(0);
streampos begin = wav.tellp();
wav.seekp(0, ios::end);
streampos size = wav.tellp() - begin;
wav.seekp(pos);
return uint32_t(size);
}
void writeHeader(uint32_t rate, uint16_t depth, uint16_t chan) {
uint16_t u16;
uint32_t u32;
char c2[2];
char c4[4];
//seek to start
wav.seekp(0);
// '----' is for file size, computed at end
wav << "RIFF----WAVEfmt ";
//size of format data
c4[0] = 16;
c4[1] = 0;
c4[2] = 0;
c4[3] = 0;
wav.write(c4, 4);
//type of format (1 = pcm)
c2[0] = 1;
c2[1] = 0;
wav.write(c2, 2);
//channels
c2[0] = char(chan);
c2[1] = char(chan >> 8);
wav.write(c2, 2);
//sample rate
c4[0] = char(rate);
c4[1] = char(rate >> 8);
c4[2] = char(rate >> 16);
c4[3] = char(rate >> 24);
wav.write(c4, 4);
//memory used per second
u32 = (rate * depth * chan) / 8;
c4[0] = char(u32);
c4[1] = char(u32 >> 8);
c4[2] = char(u32 >> 16);
c4[3] = char(u32 >> 24);
wav.write(c4, 4);
//bytes per frame
u16 = (depth * chan) / 8;
c2[0] = char(u16);
c2[1] = char(u16 >> 8);
wav.write(c2, 2);
//bit depth
c2[0] = char(depth);
c2[1] = char(depth >> 8);
wav.write(c2, 2);
//data chunk header
c4[0] = 'd';
c4[1] = 'a';
c4[2] = 't';
c4[3] = 'a';
wav.write(c4, 4);
//size of data section, file size - header size(44 bytes)
uint32_t datasize = filesize() - 44;
c4[0] = char(datasize);
c4[1] = char(datasize >> 8);
c4[2] = char(datasize >> 16);
c4[3] = char(datasize >> 24);
wav.write(c4, 4);
//write file size
wav.seekp(4);
u32 = filesize();
c4[0] = char(u32);
c4[1] = char(u32 >> 8);
c4[2] = char(u32 >> 16);
c4[3] = char(u32 >> 24);
wav.write(c4, 4);
}
void writeVal(uint32_t val) {
char c4[4];
c4[0] = char(val);
c4[1] = char(val >> 8);
c4[2] = char(val >> 16);
c4[3] = char(val >> 24);
wav.write(c4, 4);
}
void writeVal(uint16_t l, uint16_t r) {
char c4[4];
c4[0] = char(l);
c4[1] = char(l >> 8);
c4[2] = char(r >> 16);
c4[3] = char(r >> 24);
wav.write(c4, 4);
}
为了使其更有用,我将添加一个参数(fstream wav)
filesize():
uint32_t filesize()
{
streampos pos = wav.tellp();
wav.seekp(0);
streampos begin = wav.tellp();
wav.seekp(0, ios::end);
streampos size = wav.tellp() - begin;
wav.seekp(pos);
return uint32_t(size);
}
库代码:
wavlib.h:
#pragma once
#include <iostream>
#include <fstream>
//include random for random noise to test files
#include <random>
using namespace std;
class wavlib
{
public:
uint32_t filesize(string filename); //return the size of the file
};
wavlib.cpp:
#include "wavlib.h"
#include "stdafx.h"
using namespace std;
uint32_t wavlib::filesize(string filename)
{
fstream file(filename);
streampos pos = file.tellp();
file.seekp(0);
streampos begin = file.tellp();
file.seekp(0, ios::end);
streampos size = file.tellp() - begin;
file.seekp(pos);
return uint32_t(size);
}/**/