Do any of you know C++?

  • 64 Replies
  • 4575 Views
*

Yes

  • 604
Re: Do any of you know C++?
« Reply #60 on: January 07, 2020, 03:09:56 AM »
Anyways, I think the number one thing in these cases is finding a way to do things that will help you do things faster before you ever try to implement it.
Your insight is wisdom.
Signatures are displayed at the bottom of each post or personal message. BBCode and smileys may be used in your signature.

*

FlatAssembler

  • 478
  • Not a FE-er
Re: Do any of you know C++?
« Reply #61 on: May 25, 2020, 09:54:36 AM »
Just made my first useful program in C++: a program that converts musical notes stored in a text file to WAV. It's written entirely in C++11 and doesn't use any external library.
Fan of Stephen Wolfram.
This is my parody of the conspiracy theorists:
https://www.theflatearthsociety.org/forum/index.php?topic=71184.0

*

John Davis

  • Secretary Of The Society
  • Administrator
  • 16669
  • Most Prolific Scientist, 2019
Re: Do any of you know C++?
« Reply #62 on: May 26, 2020, 09:27:32 AM »
What is one practical use for that?

Also looks like trash:

https://codereview.stackexchange.com/questions/241705/converting-musical-notes-to-wav

It's hard to believe anyone that wrote this code wrote any sort of language at all.
« Last Edit: May 26, 2020, 12:03:04 PM by John Davis »
Quantum Ab Hoc

*

John Davis

  • Secretary Of The Society
  • Administrator
  • 16669
  • Most Prolific Scientist, 2019
Re: Do any of you know C++?
« Reply #63 on: May 26, 2020, 11:28:28 AM »
Yeah okay, I was being a dick (though you did call me a liar for stating my profession of which I have 20+ years of experience). You are obviously still learning. If you'd like constructive criticism post what you have so far and I'd be happy to review it.

Quantum Ab Hoc

*

FlatAssembler

  • 478
  • Not a FE-er
Re: Do any of you know C++?
« Reply #64 on: May 28, 2020, 05:13:51 AM »
What is one practical use for that?

Also looks like trash:

https://codereview.stackexchange.com/questions/241705/converting-musical-notes-to-wav

It's hard to believe anyone that wrote this code wrote any sort of language at all.
Well, I don't know if you consider that practical, but it can be used for music composition. I am not really an expert in C++, I've written the compiler for my programming language in JavaScript. I don't know about you, but my perception is that C++ sucks as a language. It's enough high-level to make it inconvenient for kernel development, but getting basic things done in it, such as opening a graphic window, or string manipulation, is a nightmare. It got slightly better in C++11 (native support for regular expressions, the "stof" command...), but it's still very bad. Only writing binary files is slightly better done than in JavaScript, everything else is worse. Anyway, here is the code I've written thus far:
Code: [Select]
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <map>

std::map<std::string, float> notes;
int sampleRate = 8192; //I don't think it's at all my fault that some players claim to support WAV format, but fail to play a WAV file that has lower sample rate than "standard".

int main(int argc, char **argv) {
notes["a"] = 220;
notes["as"] = 233;
notes["b"] = 247;
notes["C"] = 262;
notes["Cs"] = 277;
notes["D"] = 293;
notes["Ds"] = 311;
notes["E"] = 329;
notes["F"] = 349;
notes["Fs"] = 370;
notes["G"] = 391;
notes["Gs"] = 415;
notes["A"] = 440;
notes["As"] = 466;
notes["H"] = 493;
notes["C5"] = 523;
notes["Cs5"] = 554;
notes["D5"] = 587;
notes["Ds5"] = 622;
notes["E5"] = 659;
if (argc < 2) {
std::cerr << "Please supply the text file with notes as an argument."
<< std::endl;
return 1;
}
std::ifstream input(argv[1]);
if (argc > 2)
sampleRate = atoi(argv[2]);
if (!input) {
std::cerr << "Can't open \"" << argv[1] << "\" for reading!"
<< std::endl;
return 1;
}
FILE *wav = std::fopen("output.wav", "wb");
if (!wav) {
std::cerr << "Can't open \"output.wav\" for output!" << std::endl;
return 1;
}
bool isLittleEndian;
int testNumber = 0x10;
std::fwrite(&testNumber, sizeof(int), 1, wav);
std::fclose(wav);
wav = std::fopen("output.wav", "rb");
char testCharacter = 0;
std::fread(&testCharacter, 1, 1, wav);
std::fclose(wav);
if (testCharacter == 0x10) //The logic is: if the C library uses big endian for writing binary files, now "testCharacter" will still contain 0.
isLittleEndian = true;
else
isLittleEndian = false;
wav = std::fopen("output.wav", "wb");
if (isLittleEndian)
std::fprintf(wav, "RIFF"); //ASCII for 0x52494646, the magic number that WAV files start with.
else
std::fprintf(wav, "RIFX"); //Big endian WAV file starts with magic number 0x52494658, or, in ASCII, "RIFX".
int32_t ChunkSize = 36 + 8 * sampleRate * 2;
std::fwrite(&ChunkSize, 4, 1, wav);
std::fprintf(wav, "WAVEfmt "); //The beginning of the header.
int32_t Subchunk1Size = 16; //PCM header is always 16 bytes.
std::fwrite(&Subchunk1Size, 4, 1, wav);
int16_t AudioFormat = 1; //PCM format.
std::fwrite(&AudioFormat, 2, 1, wav);
int16_t NumChannels = 1; //MONO audio.
std::fwrite(&NumChannels, 2, 1, wav);
int32_t SampleRate = sampleRate;
std::fwrite(&SampleRate, 4, 1, wav);
int32_t ByteRate = 2 * sampleRate; //Since we are using 16 bits per sample, and "sampleRate" samples per second.
std::fwrite(&ByteRate, 4, 1, wav);
int16_t BlockAlign = 2; //Each block is two bytes.
std::fwrite(&BlockAlign, 2, 1, wav);
int16_t BitsPerSample = 16;
std::fwrite(&BitsPerSample, 2, 1, wav);
std::fprintf(wav, "data");
while (!input.eof()) {
std::string currentNote;
input >> currentNote;
if (currentNote.substr(0, 1) == "#") //Comment
{
while (!input.eof() && input.get() != '\n')
continue;
continue;
}
if (currentNote.length() == 0)
break;
std::string durationString = "";
int i = 0;
while ((currentNote[i] >= '0' && currentNote[i] <= '9')
|| currentNote[i] == '.') {
durationString += currentNote.substr(i, 1);
i++;
}
std::cerr << "Read note name \"" << currentNote
<< "\", the duration string is: " << durationString
<< std::endl;
int noteDuration = 3 * sampleRate / std::stof(durationString);
std::string fullNoteName = currentNote.substr(i);
if (std::stof(durationString) == 0
|| std::isnan(std::stof(durationString))
|| (notes[fullNoteName] == 0 && fullNoteName != "P"
&& fullNoteName.substr(0, 3) != "Hz(")) {
std::cerr << "Can't interpret the note name \"" << currentNote
<< "\" or the duration number " << durationString
<< ", aborting!" << std::endl;
std::fclose(wav);
input.close();
return 1;
}
std::cerr << "Playing note \"" << fullNoteName << "\" for "
<< noteDuration << " samples." << std::endl;
float currentFrequency = notes[fullNoteName];
if (fullNoteName.substr(0, 3) == "Hz(")
currentFrequency = std::stof(fullNoteName.substr(3));
std::cerr << "Frequency is " << currentFrequency << "Hz." << std::endl;
for (int i = 0; i < noteDuration; i++) {
float baseFrequency = std::sin(
2 * M_PI * currentFrequency * i / sampleRate)
* std::pow(2, 14);
float secondHarmony = std::sin(
2 * M_PI * 2 * currentFrequency * i / sampleRate + M_PI / 4)
* std::pow(2, 12);
float thirdHarmony = std::sin(
2 * M_PI * 3 * currentFrequency * i / sampleRate + M_PI / 2)
* std::pow(2, 10);
float fourthHarmony = std::sin(
2 * M_PI * 4 * currentFrequency * i / sampleRate - M_PI / 4)
* std::pow(2, 9);
float currentAmplitude = (baseFrequency + secondHarmony
+ thirdHarmony + fourthHarmony)
* std::exp(-(float) (2 * i) / (sampleRate)); //Attenuation.
int16_t numberToBeWritten =
(fullNoteName == "P") ? (0) : (currentAmplitude);
numberToBeWritten += std::rand() % (1 << 8) - (1 << 7); //A bit of noise makes it sound better.
std::fwrite(&numberToBeWritten, 2, 1, wav);
}
}
input.close();
std::fclose(wav);
}
Fan of Stephen Wolfram.
This is my parody of the conspiracy theorists:
https://www.theflatearthsociety.org/forum/index.php?topic=71184.0