Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过sin()生成的金属声音 我有以下代码,混合了,因为我从C和PHP背景学习C++: int main() { const unsigned int sampleRate = 48000; // prepare a 6 seconds buffer and write it const unsigned long int size = sampleRate*6; float sample[size]; unsigned long int i = 0; unsigned long insin, insinb, factor; // Multiply for 2*pi and divide by sampleRate to make the default period of 1s // So the freq can be stated in Hz factor = 2*3.141592; for (i; i<size; i++) { // Store the value of the sin wave insin = 440*float(i)*factor/float(sampleRate); insinb = 880*float(i)*factor/float(sampleRate); if (i > size/8) // Attempt to make it sound more instrument-like sample[i] = 0.7 * sin(insin) + 0.3 * sin(insinb); else sample[i] = 0.7 * sin(insinb) + 0.3 * sin(insin); // DEBUG if (i < 1000) printf("%f\n", sample[i]); } writeWAVData("sin.mp3", sample, size, sampleRate, 1); return 1; }_C++_Linux_Audio_Trigonometry - Fatal编程技术网

通过sin()生成的金属声音 我有以下代码,混合了,因为我从C和PHP背景学习C++: int main() { const unsigned int sampleRate = 48000; // prepare a 6 seconds buffer and write it const unsigned long int size = sampleRate*6; float sample[size]; unsigned long int i = 0; unsigned long insin, insinb, factor; // Multiply for 2*pi and divide by sampleRate to make the default period of 1s // So the freq can be stated in Hz factor = 2*3.141592; for (i; i<size; i++) { // Store the value of the sin wave insin = 440*float(i)*factor/float(sampleRate); insinb = 880*float(i)*factor/float(sampleRate); if (i > size/8) // Attempt to make it sound more instrument-like sample[i] = 0.7 * sin(insin) + 0.3 * sin(insinb); else sample[i] = 0.7 * sin(insinb) + 0.3 * sin(insin); // DEBUG if (i < 1000) printf("%f\n", sample[i]); } writeWAVData("sin.mp3", sample, size, sampleRate, 1); return 1; }

通过sin()生成的金属声音 我有以下代码,混合了,因为我从C和PHP背景学习C++: int main() { const unsigned int sampleRate = 48000; // prepare a 6 seconds buffer and write it const unsigned long int size = sampleRate*6; float sample[size]; unsigned long int i = 0; unsigned long insin, insinb, factor; // Multiply for 2*pi and divide by sampleRate to make the default period of 1s // So the freq can be stated in Hz factor = 2*3.141592; for (i; i<size; i++) { // Store the value of the sin wave insin = 440*float(i)*factor/float(sampleRate); insinb = 880*float(i)*factor/float(sampleRate); if (i > size/8) // Attempt to make it sound more instrument-like sample[i] = 0.7 * sin(insin) + 0.3 * sin(insinb); else sample[i] = 0.7 * sin(insinb) + 0.3 * sin(insin); // DEBUG if (i < 1000) printf("%f\n", sample[i]); } writeWAVData("sin.mp3", sample, size, sampleRate, 1); return 1; },c++,linux,audio,trigonometry,C++,Linux,Audio,Trigonometry,我认为金属声可能是因为sin()返回的值太像正方形。为什么会这样我能让sin()返回一个“质量更好”的函数吗?还是我做了其他固有的错误?如果您有兴趣,下面是代码的开头和其余部分: #include <fstream> #include <cmath> #include <sndfile.hh> template <typename T> void write(std::ofstream& stream, const T& t) {

我认为金属声可能是因为sin()返回的值太像正方形。为什么会这样我能让sin()返回一个“质量更好”的函数吗?还是我做了其他固有的错误?如果您有兴趣,下面是代码的开头和其余部分:

#include <fstream>
#include <cmath>
#include <sndfile.hh>

template <typename T>
void write(std::ofstream& stream, const T& t) {
  stream.write((const char*)&t, sizeof(T));
  }

template <typename SampleType>
void writeWAVData(const char* outFile, SampleType* buf, size_t bufSize,
                  int sampleRate, short channels)
  {
  std::ofstream stream(outFile, std::ios::binary);
  stream.write("RIFF", 4);
  write<int>(stream, 36 + bufSize);
  stream.write("WAVE", 4);
  stream.write("fmt ", 4);
  write<int>(stream, 16);
  write<short>(stream, 1);                                        // Format (1 = PCM)
  write<short>(stream, channels);                                 // Channels
  write<int>(stream, sampleRate);                                 // Sample Rate
  write<int>(stream, sampleRate * channels * sizeof(SampleType)); // Byterate
  write<short>(stream, channels * sizeof(SampleType));            // Frame size
  write<short>(stream, 8 * sizeof(SampleType));                   // Bits per sample
  stream.write("data", 4);
  stream.write((const char*)&bufSize, 4);
  stream.write((const char*)buf, bufSize);
  }

我认为问题可能是:

unsigned long insin, insinb, factor;
这些是整数类型

尝试将此更改为

float insin, insinb, factor;

确实如此。非常感谢你。
unsigned long insin, insinb, factor;
float insin, insinb, factor;