Course Content
Sound manipulation
Sound looping
Ø soundeffectinstance
Compared with the cowbell application in the previous chapterProgramTrombone in this chapter is a more professional instrument application. We can define the corresponding scale by controlling the slide movement up and down (The position of the slide in the application is not from the f, which is different from the actual trombone Slide position ). This application supports two different slide modes. If we touch the screen on the left, we can freely move the slide. If we touch the screen on the right, it will align to the scale that has been marked. In addition to being easier to operate, this software can also be used as a phonograph.
Trombone can be pronounced within two octal intervals. If we want to increase the sound to an octal, we can place another finger anywhere on the screen. The most interesting thing about this application is that, like a real long number, we must blow our phone to make it sound.
The functions required for the last two features of the application are described in the following sections (multi-touch and microphone usage ).CodeWe will not introduce it here. On the contrary, this chapter focuses on the pitch and sound length operations of a single sound effect to meet the needs of this application.
The User Interface
Trombone has a home page, an overview page, and a setting page. The code for setting the page is not described in this chapter, because it is similar to the setting page of the "bubble blower" Application in Chapter 34th except the page title. The settings page allows you to adjust the microphone when the volume is too large or in an hour. The code of the Introduction page is not described here, because it has no special features.
The initialization status on the home page is 31.1, which includes removable slide, scaled scale, and link buttons pointing to the other two pages.
Figure 31.1 The home page imitates the appearance of a long number
Note:
The scale line marked in Figure 31.1 is implemented by the CS code behind the page.
The ghost application bar blocks the user interface of the application, so two rectangular buttons are used instead. We use a familiar non-transparent template to ensure that the display effects are consistent under different themes.
Figure 31.2 The slider of the length is implemented by adding a removable image to the static image.
The Code-behind
Note:
In this app, only one audio file is used for the pronunciation of the long number, that is, the audio file for the fcall. The voice of Other tones is implemented by dynamically changing the sound frequency of the ftone.
In the previous chapter of explain, we directly use the soundeffect object. This application calls the createinstance method in soundeffect to obtain the soundeffectinstance object. Compared with soundeffect, soundeffectinstance provides more features, because it is bound to a single sound instance, so even if the sound has started playing, it can still be operated. On the one hand, trombone applications require soundeffectinstance to complete periodic tasks; on the other hand, soundeffectinstance can dynamically change the scale of played sounds.
➔ Soundeffectinstance provides an islooped attribute (set to false by default), which allows users to play an audio file cyclically indefinitely until the stop method is called. Depending on the audio source file, it can be implemented in two ways:
1. For an ordinary audio file, this loop is applied to the entire audio range. Therefore, when the previous playback ends, it starts to play again seamlessly.
2. For an audio file with a circular area, the program starts playing from the beginning during the first playback, but in the subsequent loop, only the circular area will be played. Once the program calls the default stop method, the sound stops playing. However, if we override the stop method and input the false parameter, it will stop the current playback, then jump out of the loop and play the remaining part of the audio segment.
Figure 31.3 shows the two actions. The latter behavior is suitable for this application, because it uses a real length fcall audio and performs a smooth transition from the beginning to the end of the sound. For this reason, the f.wav file included in the project defines a circular area. Although the length of the audio file is less than 1/3 seconds, the application can play the audio file for any long time as the user can maintain the mobile phone blowing action in the circular area.
Figure 31.3 set the attribute value of soundeffectinstance. islooped to true
Pay attention to the length of the circular area we define!
If we do not want to stop playing the sound immediately, but stop it slowly after calling the stop (false) method, then we define the loop area (and the remaining part of the sound file) it must be as short as possible. Otherwise, it takes a long time to end the current playback loop.
Wavelur (www.wav osaur.com) is a free and very strong Audio Editor. With this feature, we can create a circular area inside a .wav file. Select some areas of a sound file, click the "loop" option in the "Tools" menu, and then click "CREATE" to create a circular area. In a normal environment, the expanded .wav file can still be played directly, but when the soundeffectinstance instance is used and its islooped attribute is set to true, it will be played Based on the set loop area.
Soundeffect vs. soundeffectinstance
Soundeffect can play audio files, while soundeffectinstance can use its pause, resume, and stop methods to pause, continue, and stop a specified audio file. Each time the play method of soundeffect is called, a new instance of playing sound starts. We cannot stop it (it may affect the playing sound ); when the play method of soundeffectinstance is called, if the instance of the sound is currently playing, it will not do any action. Because soundeffectinstance is bound to a sound instance, it also has the state attribute to indicate that the current sound is playing, paused, or stopped.
In addition to the islooped attribute, soundeffectinstance also has three attributes that control the sound effect. We can set it at any time, or even during the playback process:
Volume volume (default value: 1): The range is 0 ~ 1, where 0 indicates mute, 1 indicates the maximum volume.
Interval pitch (default value: 0): range:-1 ~ 1, where-1 indicates the lower alignment scale, 1 indicates the higher alignment scale, and 0 indicates playing according to the original tone.
➔ Pan (default value: 0): range:-1 ~ 1.-1 indicates that only the left speaker is used for pronunciation, 1 indicates that only the right speaker is used for pronunciation, and 0 indicates the Left and Right balanced pronunciation.
Soundeffect can also be controlled by accepting play method overloading of volume, pitch, and Pan parameters. However, these values often lead to prolonged sound playback time (in the previous chapter, the play method in soundeffect does not contain any parameters, and its volume attribute is 1, the pitch and Pan attribute values are 0 ).
Soundeffectinstance also has two overloaded apply3d methods, so that we can apply the three-dimensional position to the sound playing process. This feature is very interesting for Xbox and PC games. For mobile phones, 3D positioning (or even custom Pan attribute values) is not very useful.
Note:
In the compositiontarget. Rendering event processing, the volume of the microphone is constantly compared with a threshold value (which can be adjusted on the settings page. If the value is large enough and the sound is not played, the program calls the play method (it is not necessary to strictly check the state attribute, because it is different from soundeffect. different play methods, soundeffectinstance. the play method does not play together when the sound is being played ). If the sound is playing and the microphone volume is not large enough, the program will call the stop (false) method to exit the playing loop until the sound ends.
When one finger of a user is in contact with the screen of the mobile phone, the other finger also touches the screen, this triggers the touch_framereported event (for more information, see the "Touch and multi-touch" section in Part VII). The program adjusts the tone of the sound. The startingpitch variable tracks the tone F in which it is located (0 represents the original scale, and 1 represents the ascending scale). The distance between the finger and the bottom of the screen determines the degree of scale reduction. In the scale array at the beginning of the code, we can find that the D scale can be obtained by downgrading the F scale 25% (based on the original scale 0.25 or the original scale 0.75). Similarly, if you reduce the scale F by 50%, you can obtain the scale B (by 0.5 based on the original scale or by 0.5 of the original scale ).
Can I hear the voice when the main volume of the mobile phone is muted? Can I play a sound that is larger than the main volume?
The answer is no.Because the user must be authorized to select the maximum playback volume. The value range is 0 ~ The volume parameter value of 1 is relative to the main volume. Note that soundeffect has a static mastervolume attribute, which allows you to adjust the volume of all sounds synchronously (whether played through soundeffect or soundeffectinstance), but the volume does not exceed the selected volume value.