How to Programmatically Generate Music and Sound Effects?

I want to programmatically generate music and sound effects using SDL2 please help

What style did you have in mind? Old 8 bit computers used to have pretty simple systems with a few simple oscillators and filters. Think bleeps and bloops like the C64 or NES. Later systems had sample based synthesizers that play back little sound file snippets, Amiga “mod” files for example. Both of these options are fairly straightforward to implement with just vanilla SDL using an audio callback. There are of course much fancier types of synthesizers, but they get complicated quite quickly.

I’m not sure what you’re trying to do, but you’re almost certainly better off faking it. Otherwise, you’d have to use SDL’s native music API (which isn’t that bad but SDL_Mixer is easier), keep track of tempo yourself and generate a waveform on the fly, which beyond your basic square, triangle, sawtooth and maybe sine, is not an easy thing to do.

If you were to try to do it anyway, you’d probably want to build a tool that lets you convert sounds you’ve recorded into a sort of data structure that represents the waveforms you want for each instrument at each note (and each intonation, if you wanna go there) and play them back mixed together. But if you were going to do that, you might as well use a DAW, an existing tool which does exactly that, for you, and spits out either a music file or a mod file. SDL_Mixer supports mod music types. Using them, you can do things like dynamically switch to different parts of the tune at natural points in your track rather than abruptly.

Or, if you’re more looking for adding/removing layers and/or reorchestrating on the fly, that’s as easy as crossfading with SDL (not SDL_Mixer) to different versions of your track that have the arrangement you need. For example, you might have a level1.wav, and a level1withdrums.wav. Takes more disk space though.

I’m not necessarily trying to talk you out of doing it how you mentioned–in fact, that could be a fun challenge. But I did want to point out that there are much easier ways to accomplish some of the goals you might have in mind.

I, for example, would like to change the frequency of played sounds … This is a basic function … MIDI support would also be useful.

I found this code may be someone can explain it
it plays some sound

the keyword is bytebeats

#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#define PI2 6.28318530718

float duration = 0;
float freq = 440;

void callback(void *userdata, Uint8 *stream, int len)
{
	short *snd = (short*)stream;
	len /= sizeof(*snd);
	for (int i = 0; i < len; i++) //Fill array with frequencies, mathy-math stuff
	{
		snd[i] = 32000 * sin(duration);

		duration += freq * PI2 / 48000.0;
		if (duration >= PI2)
			duration -= PI2;
	}
}

int main(int argc, char **argv)
{

	srand(time(NULL));

	SDL_Init(SDL_INIT_AUDIO);
	SDL_AudioSpec spec, aspec; // the specs of our piece of "music"
	SDL_zero(spec);
	spec.freq = 48000; //declare specs
	spec.format = AUDIO_S16SYS;
	spec.channels = 1;
	spec.samples = 4096;
	spec.callback = callback;
	spec.userdata = NULL;

	//Open audio, if error, print
	int id;
	if ((id = SDL_OpenAudioDevice(NULL, 0, &spec, &aspec, SDL_AUDIO_ALLOW_ANY_CHANGE)) <= 0)
	{
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
		exit(-1);
	}

	/* Start playing, "unpause" */
	SDL_PauseAudioDevice(id, 0);

	while (true)
	{
		for (freq = 440; freq < 880; freq += 2)
			SDL_Delay(5);
		for (freq = 870; freq > 450; freq -= 2)
			SDL_Delay(5);
	}

}

And for the MIX_PlayChannel sound?

Why doesn’t it work for me in Windows 10? Silence… SDL_mixer works …