Soundtouch audio processing database source code analysis and algorithm extraction (1)

Source: Internet
Author: User

Soundtouch audio processing library is easy to use. After simple compilation, you can set the compiling environment. Take VC as an example.

Directly include the include path under the soundtouch directory, and then add the soundtouch directory in Lib.

And then add the header file and referenced Library to the header file of the Code. According to the macro _ debug,

We can perform some compilation preprocessing. If we use the debug library for debugging compilation, we will use

The release library. The difference is whether there is a "d" after the file name ".
# Include <soundtouch. h>
# Ifdef _ debug
# Pragma comment (Lib, "soundtouchd. lib ")
# Else
# Pragma comment (Lib, "soundtouch. lib ")
# Endif
Of course, you can also add them directly in the VC project. Some people prefer this.
The most important thing is to declare a namespace. The reason is related to the declaration definition of the soundtouch library,

As mentioned in the analysis.
Using namespace soundtouch
Then, you can define a class variable soundtouch m_soundtouch in your code;
The Declaration of the soundtouch class is included in soundtouch. h and soundtouch. cpp.

The fifoprocessor class is derived directly from the base class fifosamplepipe. Declare soundtouch

The class is included in the namespace soundtouch, which is why we need to declare the namespace when using this library.

The main reason is. It seems a little redundant. Only some constants, such as the version number and version ID number, are defined.

The parent class is included in the export osamplepipe. h and export osamplepipe. cpp files.

No matter what database, if you want to use it, the general process is to first define and then perform some necessary initialization,

Soundtouch (hereinafter referred to as St) is no exception. The initialization of St is just as simple as its compilation.

Refer to his example soundstretch for implementation. You can also refer to the Declaration of the soundtouch class in the source code,

Now we only care about what we will use. We can see that two other class pointers are defined in private.

Ratetransposer *, tdstretch *;
Ratetransposer is derived from kerberoprocessor, and kerberoprocessor is directly from the base class paiosamplepipe

Generate, tdstretch and ratetransposer are similar. It can be seen from the names of two classes: lengthen? Transmission

Speed? It is hard to imagine that the processing of sound signals in this database may be "stretching" and "variable speed ". Is it a legend

Do not change the transmission speed? This is exactly the case. This is not a topic we are currently concerned about.
......
PRIVATE:
/// Rate transposer class instance
Class ratetransposer * pratetransposer;
/// Time-stretch class instance
Class tdstretch * ptdstretch;
/// Virtual pitch parameter. Valid tive rate & tempo are calculated from

These parameters.
Float virtualrate;
/// Virtual pitch parameter. Valid tive rate & tempo are calculated from

These parameters.
Float virtualtempo;
/// Virtual pitch parameter. Valid tive rate & tempo are calculated from

These parameters.
Float virtualpitch;
/// Flag: Has sample rate been set?
Bool bsrateset;
/// Calculates valid tive rate & tempo valuescfrom 'virtualrate ',

'Virtualtempo' and
/// 'Virtualproject' parameters.
Void calceffectiverateandtempo ();
Protected:
/// Number of channels
Uint channels;
// Valid tive 'rate' value calculated from 'virtualrate', 'virtualtempo'

And 'virtualupload'
Float rate;
// Valid tive 'tempo' value calculated from 'virtualrate', 'virtualtempo'

And 'virtualupload'
Float tempo;
/// Sets new rate control value. Normal Rate = 1.0, smaller values
/// Represent slower rate, larger faster rates.
Void setrate (float newrate );
/// Sets new tempo control value. normal tempo = 1.0, smaller values
/// Represent slower tempo, larger faster tempo.
Void settempo (float newtempo );
/// Sets new rate control value as a difference in percents compared
/// To the original rate (-50... + 100%)
Void setratechange (float newrate );
/// Sets new tempo control value as a difference in percents compared
/// To the original tempo (-50... + 100%)
Void settempochange (float newtempo );
/// Sets new pitch control value. Original pitch = 1.0, smaller values
/// Represent lower pitches, larger values higher pitch.
Void setpitch (float newpitch );
/// Sets pitch change in aves AVEs compared to the original pitch
/// (-1.00 .. + 1.00)
Void setpitchoctaves (float newpitch );
/// Sets pitch change in semi-Tones compared to the original pitch
/// (-12... + 12)
Void setpitchsemitones (INT newpitch );
Void setpitchsemitones (float newpitch );
/// Sets the number of channels, 1 = mono, 2 = stereo
Void setchannels (uint numchannels );
/// Sets sample rate.
Void setsamplerate (uint srate );
/// Changes a setting controlling the processing system behaviour. See
/// 'Setting _... 'defines for available setting ID's.
/// Return 'true' if the setting was succesfully changed
Bool setsetting (INT settingid, // <setting ID number. See setting _...

Defines.
Int value // <new setting value.
);
......
Refer to the example soundstretch provided by St to initialize the soundtouch class:
M_soundtouch.setsamplerate (samplerate); // sets the sound sampling frequency.
M_soundtouch.setchannels (channels); // set the sound Channel
M_soundtouch.settempochange (tempodelta); // This is the legendary variable speed.
M_soundtouch.setpitchsemitones (pitchdelta); // set the pitch of the sound.
M_soundtouch.setratechange (ratedelta); // sets the sound rate.
// Quick is a bool variable. I am not sure what use_quickseek is.
M_soundtouch.setsetting (setting_use_quickseek, quick );
// Noantialias is a bool variable. I am not quite sure what use_aa_filter is.
M_soundtouch.setsetting (setting_use_aa_filter ,! (Noantialias ));
// Speech is also a bool variable. You need to set it when there may be no music but human voices.
If (speech)
{
// Use settings for Speech Processing
M_soundtouch.setsetting (setting_sequence_ms, 40 );
M_soundtouch.setsetting (setting_seekwindow_ms, 15 );
M_soundtouch.setsetting (setting_overlap_ms, 8 );
Fprintf (stderr, "tune processing parameters for speech processing./N ");
}
Through a few simple function calls, we can now feel the power of St. Using the soundtouch class

Function call method:
Putsamples (samplebuffer, nsamples );
The first parameter is a pointer to a piece of audio data encoded by PCM, and the second parameter is the number of audio data to be processed.

Sample can also be understood as the number of frames.
It should be noted that generally, data streams are byte streams, that is, the sample size, sound path, and bit sound parameters.

If the samplebuffer Pointer Points to a PCM data buffer with a length of 64 bytes

In fact, only (16*2)/8 = 4 bytes, 64/4 = 16; 16 samples are stored here.

Location. M_soundtouch.putsamples (samplebuffer, nsamples); where does the data pass in?

What about the processed audio data? In this case, we need to use the receivesamples function provided by soundtouch.

Call method.
Uint ipvesamples (sampletype * outbuffer, // <buffer where to copy output

Samples.
Uint maxsamples // <How many samples to receive at max.
); It is also two parameters. The first is the parameter for receiving data, and the second is the maximum number of samples that can be received.
Through this annotation, it is clear that the receivesamples function will not return data immediately after putsamples,

On the other hand, it is possible to return more data than maxsamples, so you need to put it in a do... While (...) In the loop

Squeeze them all.
// Read ready samples from soundtouch processor & write them output file.
// Notes:
//-'Your esamples 'doesn' t necessarily return any samples at all
// During some rounds!
//-On the other hand, during some round 'into esamples' may have more
// Ready samples than wocould fit into 'samplebuffer', and for this reason
// The 'others' call is iterated for as many times as it
// Outputs samples.
Do
{
Nsamples = m_soundtouch.receivesamples (samplebuffer, buffsizesamples );
// Write samplebuffer into a file, or fill it in the buffer of the sound card to play the sound.
} While (nsamples! = 0 );
I will write it here today, which is exhausting.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/suhetao/archive/2010/08/27/5843480.aspx

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.