C++ music player system

insert image description here

Students who study c++ know that c++ is a serious programming language, so no one should use it to make games, viruses, and...do...music playback systems! !

benefits of music

  • Elevates Mood : Music can affect our mood. Faster music can increase excitement and energy, while slower music can help relax and reduce stress. Choosing music that suits our mood can help us adjust our emotional state.

  • Reduce stress : Listening to music can help release stress. Studies have found that pleasant music can reduce our stress response and reduce tension and anxiety.

  • Improves Cognitive Skills : Learning music can promote brain development and improve cognitive skills. The learning process of music requires us to use multiple sensory and cognitive skills such as memory, attention and spatial perception.

  • Enhance focus : Music can help us focus. Several studies have shown that background music can improve productivity and concentration when performing tasks that require concentration.

  • Promotes socializing : Music helps build social connections. Participating in music activities, such as choir, band or music lessons, can share common interests with others and strengthen social connections.

  • Enhances Memory : Music has a strong connection with memory. By associating information with music, it can aid memory and improve retention.

  • Boosts creativity : Music can spark creativity. In the process of creating and playing music, we can use our imagination and creativity to express our emotions and thoughts.

Since music is so useful, I can't help but use c++ to make a music playback system

c++ make sound

C++ is a powerful, flexible and efficient programming language suitable for various application scenarios. It has a wide range of applications in computer science and software engineering, and is a programming language worth learning and mastering. It provides a rich set of features and tools that enable developers to write high-quality, maintainable, and performant code. However, due to its complexity and flexibility, learning and mastering C++ may take some time and experience.

How can c++ make sound without adding any special libraries?
The method is very simple:

#include <Windows.h>
int main() {
    
    
    Beep(440, 1000); // 发出440Hz的声音,持续1秒钟
    return 0;
}

Beep function! !

The function of the Beep function is very simple, it is to emit buzzing sounds of different heights and lengths. But if you want to make this thing into music, there seems to be something missing. . .

Sheet Music vs. Hertz

After 20 minutes of searching on the Internet, I found a very reliable comparison table between Hertz and C key:
insert image description here
So we can make three arrays, representing bass, midrange and treble, and then type these data, and get the following is an array of three

int s1[2][10]={
    
    {
    
    0,262,294,330,349,392,440,494},
			   {
    
    0,277,311,-99,370,415,466,-99}};

int s2[2][10]={
    
    {
    
    0,523,587,659,698,784,880,988},
			   {
    
    0,554,622,-99,740,831,932,-99}};

int s3[2][10]={
    
    {
    
    0,1046,1175,1318,1397,1568,1760,1976},
			   {
    
    0,1109,1245,-999,1480,1661,1865,-999}};

In this way, the Beep operation is much simpler~~

Put the song on c++

This is a violent code, so I put the code below~~

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
int s1[2][10]={
    
    {
    
    0,262,294,330,349,392,440,494},
			   {
    
    0,277,311,-99,370,415,466,-99}};
			   
int s2[2][10]={
    
    {
    
    0,523,587,659,698,784,880,988},
			   {
    
    0,554,622,-99,740,831,932,-99}};
			   
int s3[2][10]={
    
    {
    
    0,1046,1175,1318,1397,1568,1760,1976},
			   {
    
    0,1109,1245,-999,1480,1661,1865,-999}};
 
void cppmusic()
{
    
    
	Beep(s2[0][3],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],800);
	
	Beep(s2[0][3],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],800);
	
	Beep(s2[0][3],400);
	Beep(s2[0][5],400);
	Beep(s2[0][1],600);
	Beep(s2[0][2],200);
	Beep(s2[0][3],1600);
	
	Beep(s2[0][4],400);
	Beep(s2[0][4],400);
	Beep(s2[0][4],600);
	Beep(s2[0][4],200);
	Beep(s2[0][4],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],200);
	Beep(s2[0][3],200);
	
	Beep(s2[0][3],400);
	Beep(s2[0][2],400);
	Beep(s2[0][2],400);
	Beep(s2[0][1],400);
	Beep(s2[0][2],800);
	Beep(s2[0][5],800);
	
	Beep(s2[0][3],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],800);
	
	Beep(s2[0][3],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],800);
	
	Beep(s2[0][3],400);
	Beep(s2[0][5],400);
	Beep(s2[0][1],600);
	Beep(s2[0][2],200);
	Beep(s2[0][3],1600);
	
	Beep(s2[0][4],400);
	Beep(s2[0][4],400);
	Beep(s2[0][4],600);
	Beep(s2[0][4],200);
	Beep(s2[0][4],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],400);
	Beep(s2[0][3],200);
	Beep(s2[0][3],200);
	
	Beep(s2[0][5],400);
	Beep(s2[0][5],400);
	Beep(s2[0][4],400);
	Beep(s2[0][2],400);
	Beep(s2[0][1],1200);
}
 
int main()
{
    
    
	cppmusic();
	return 0;
}

In addition, it is very simple to play in a loop, just add a loop~~

Guess you like

Origin blog.csdn.net/DUXS11/article/details/132381102