-1

Imagine having a recording of something saying "Hi there"

I'd like to chop/slice the WAV file between "Hi" and "there"

At this point, the "Hi" would be on one WAV file and the "there" on another wav file .e.g

hi.wav there.wav

From there, I'd like to change the volume/amplitude of the "there.wav" file.

Lastly, I'd like to merge the two wave files into:

hi_there.wav

Think of this as an audio editor, once finishing editing the audio, you would be able to save the edited audio as a brand new audio clip.

smoovy
  • 1
  • 3
  • I probably use a scale filter in ffmpeg to scale the whole audio. Then I'd use the same app to splice in the amplified version – g00se Dec 03 '22 at 22:45
  • By ffmpeg are you referring to a program or a library? I installed their GitHub library onto my IDE, however some packages were not found and I couldn't find any example code to build onto. – smoovy Dec 03 '22 at 23:23
  • There's a Java wrapper as far as I can remember but if you find that problematic you can always use the real thing via `ProcessBuilder` – g00se Dec 03 '22 at 23:40
  • @g00se Thanks, will look into that. Yesterday I've managed to get cutting a part of the WAV file. I've previously found out how to join the two. I just need to get the amplitude part right at this point. – smoovy Dec 04 '22 at 12:41
  • It's not inconceivable that you can configure the filter to just scale part of the file, such that you don't need to do any splicing – g00se Dec 04 '22 at 13:38
  • @g00se that's also true, I guess I can implement something like that as well. So far what I've tried to change the amplitude in half is two covert the bytes into a double array, half the value of each double and then cast it back to a byte array. It had an output, but it basically worked like an audio limiter and then add a bunch of noise as well. – smoovy Dec 04 '22 at 13:44
  • After a great deal of googling and experimentation, I came up with `ffmpeg -i a440.wav -af "volume=0.1:enable='between(t,2,8)'" a2.wav` Scale the volume down by a factor of 10 in the time region between 2 and 8 seconds – g00se Dec 04 '22 at 16:28
  • @g00se Do you do this in the command line or is it that in another programming language? I don't recognize that syntax – smoovy Dec 04 '22 at 19:21
  • Command line, yes – g00se Dec 04 '22 at 20:35
  • @g00se I took a look around it, it looks like it's quite an exhaustive library, just not sure how to use it. Hopefully I can figure it out, might take a bit of browsing through it. – smoovy Dec 05 '22 at 21:34

1 Answers1

0

Cut the wave file:

double [] music = read("music.wav");

int start = 792478*2;
int end = 1118153*2;

double [] cutMusic = new double [end-start];

for(int i = start; i < end; i++)
{
    cutMusic [i-start] = music[i];
}

save("cutMusic.wav", cutMusic); //from StdAudio library

Change the volume/amplitude:

int multiplier = 0.5; //how much you want to change the volume by

for(int i = 0; i < cutMusic.length;i++)
{
    cutMusic[i] = multiplier * cutMusic[i];
}

Save the audio to a new wav file:

save("cutmusic.wav"), cutMusic); //from the StdAudio library
//converts the double array to a byte array

Join the two files:

ArrayList <File> joined = new ArrayList<>();

joined.add(new File("music.wav"));
joined.add(new File("cutMusic.wav"));

AudioFormat format = new AudioFormat(

                /*AudioFormat.Encoding*/AudioFormat.Encoding.PCM_SIGNED
                /*float sampleRate*/, sampleRate
                /*int sampleSizeInBits*/, 16
                /*int channels*/, 2
                /*int frameSize*/, 4
                /*float frameRate*/, 44100,
                /*boolean bigEndian*/false

joinAudioFiles(format, joined, new File("joined.wav")); 
//method needed is below

public static void joinAudioFiles(AudioFormat audioFormat,
                                      java.util.List<File> audioFiles, File output) throws IOException,
            UnsupportedAudioFileException {
        output.getParentFile().mkdirs();
        output.delete();
        output.createNewFile();

        List<AudioInputStream> audioInputStreams = new ArrayList<AudioInputStream>();
        long totalFrameLength = 0;
        for (File audioFile : audioFiles) {
            AudioInputStream fileAudioInputStream = AudioSystem
                    .getAudioInputStream(audioFile);
            audioInputStreams.add(fileAudioInputStream);
            totalFrameLength += fileAudioInputStream.getFrameLength();
        }

        AudioInputStream sequenceInputStream = new AudioInputStream(
                new SequenceInputStream(
                        Collections.enumeration(audioInputStreams)),
                audioFormat, totalFrameLength);
        AudioSystem.write(sequenceInputStream, AudioFileFormat.Type.WAVE,
                output);
    }
smoovy
  • 1
  • 3