简体   繁体   中英

Change the volume of an audio file and save the file using java.sound.sampled

I want to change the volume of an audio file and save the new file using java.sound.sampled.

I tried to use the mixer to create a source line from the file given and a target line to the new file. So that I can change the mixer settings to change the volume. But the sound is being played to the system speaker. Am I thinking along correct way or not? Is there any other way to record a file from a line?

The code is available here A solution I got is www.jsresources.org/examples/AmplitudeConverter.html. But can the same be done within java.sound.sampled without using external libraries.

To change the volume, if you don't use a "Control" (see the Java Sound Tutorials), there is the option of directly modifying the samples themselves.

In your innermost loop, convert the bytes in the innermost buffer into a sample (if it is WAV 16-bit encoding, then you need to put the two bytes together to make the single SHORT value), then multiply that value by a float that ranges from 0 to 1, where 0 is the quietest and 1 leaves the sound at full volume. Then take the result and break it back down into two bytes and pass it along.

Do you need the code to do this? There are several other posts here where folks convert from bytes to INTs or Float and back.

Hmmm. This question is pretty old. Well maybe my answer will help someone new to the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM