Answer the question
In order to leave comments, you need to log in
Play sound from buffer without saving?
I found this code (comments are not mine):
#include <windows.h>
#include <fstream>
#include <iostream>
const int len = 6000 * 10; // 10 секунд
// Вот тут самая "умная" часть, это параметры, включая длительность битрейт и битность.
// Во всё это я не вникал, это нужно только для создания файла.
byte buffer[len] = {0x52, 0x49, 0x46, 0x46, 0xfa, 0x3c, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x70, 0x17, 0x00, 0x00, 0x70, 0x17, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x64, 0x61, 0x74, 0x61, 0xd5, 0x3c, 0x00, 0x00};
int main()
{
int i = 42, l = len - 1, r;
while(++i < l) // Простой разброс семплов, вниз-вверх, амплитуда сигнала не будет больше 42х..
{
r = rand() % 3;
if(r == 0)
buffer[i] = 127;
else if(r == 1)
buffer[i] = 128;
else
buffer[i] = 129;
}
buffer[l] = 0;
std::ofstream file;
file.open("111.wav");
file.write((char*)&buffer[0], 5000*10);
file.close();
return 0;
}
Answer the question
In order to leave comments, you need to log in
If you are not a programmer, no way.
Playing sound programmatically is somewhat more complicated than writing data to a file. But the principle is about the same - open a playback device and write data to it that needs to be played.
But due to the fact that playback must be without delay, playback itself takes time and the OS API and hardware can only work with certain types of data, there are features that complicate the process.
The easiest way is to use some library that will do most of the work. You can use PortAudio, for example, but it's in C and quite low-level. It is possible to use the OS API, but this is quite low-level and can be quite difficult for a beginner.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question