How to play a sound on a computer

Source: Internet
Author: User

My goal is to simulate a variety of musical instruments on the computer and use the speaker to play the sound of the Instrument in a realistic manner. Everything starts hard. The first step is to let the computer play a simple syllable, which is the same as helloworld when learning C language.

(1) first download the rtmidi library, which is an open source audio and audio library. Rtmidi is
A set of C ++ classes (rtmidiin, rtmidiout and
API-specific classes) that provides a common API (Application Programming Interface) for realtime MIDI input/output audio SS Linux (ALSA & Jack), Macintosh OS X (coremidi & Jack ), and windows (Multimedia Library & kernel streaming) operating systems. rtmidi significantly
Simplifies the process of interacting with computer MIDI hardware and software. it was designed with the following goals: rtmidi is an open-source Real-Time MIDI Player Library written in C ++. It can run on MacOSX, Linux, and windows.

: Http://www.music.mcgill.ca /~ Gary/rtmidi/release/rtmidi-2.0.1.tar.gz; the latest version is 2.0.1.

(2) Open the downloaded file, decompress it to a folder, open the test folder, and use vs to open the *. sln file in it. Then click "run". A sound is played in the audio.

(3) However, the example provided by the database itself is too complex to be suitable for beginners. So I wrote a new helloworld (you can also find the following source code on the official website). The program is as follows:

(4) If you are not familiar with the terms, read several other blog posts. Especially: http://blog.csdn.net/jia_zhengshen/article/details/8777071

# Include <iostream> # include <cstdlib> # include "rtmidi. H "int main () {rtmidiout * midiout = new rtmidiout (); STD: vector <unsigned char> message; // check available ports. unsigned int nports = midiout-> getportcount (); If (nports = 0) {// you can check the number of available MIDI interfaces. What does 0 mean? Not explained. STD: cout <"no ports available! \ N "; goto cleanup; // although the GOTO statement is rarely used, there is so little code here, You can always score the two or three bad noodles} // open first available port. midiout-> openport (0); // open the first MIDI port. // Send out a series of MIDI messages. // program change: 192, 5 message. push_back (192); // 192 = 0xc0, what does this mean? Let's look at the http://blog.csdn.net/jia_zhengshen/article/details/8777071. Here we use an article to explain the message. push_back (5); midiout-> sendmessage (& message); // control change: 176, 7,100 (volume) Message [0] = 176; // adjust the volume. // 176 = b0; view the preceding URL description. It is too complicated. Although the code is simple, it is not easy to explain it clearly. Message [1] = 7; message. push_back (100); // push_back. Message [2] may cause memory overflow. Midiout-> sendmessage (& message); // note on: 144, 64, 90 message [0] = 144; // start playing message [1] = 64; // The message of the 64-bit PLAYING code [2] = 90; // The speed is 90, and the highest position is 7fh midiout-> sendmessage (& message); sleep (500 ); // platform-dependent... see example in tests directory. // note off: 128, 64, 40 message [0] = 128; // stop playing message [1] = 64; // stop the 64-bit note message [2] = 40; // The speed is 40 midiout-> sendmessage (& message); // clean up cleanup: delete midiout; getchar (); return 0 ;}

These codes are relatively simple. Suitable for beginners. Comment out all the code in the sysextest. cpp file in the test project and copy it into the code. A note will be output through audio.

The explanation is complete. This article is based on http://my.csdn.net/jia_zhengshensource.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.