zoglmannk / Morse-Code-Ninja

Software used to generate Morse Code Ninja practice sets as found on https://morsecode.ninja/practice/index.html
https://morsecode.ninja
GNU General Public License v3.0
32 stars 14 forks source link

Cutting out silence from the soundfiles after each character to make "--sm, --silencemorse" work for 0.1-1s #49

Open heppstar opened 8 months ago

heppstar commented 8 months ago

Hi,

I learned to play a game called "beat the computer". The computer plays a character and I try to say the character before the computer does. I narrow the time down to about 250ms between the computer plays the characters and I speaks the character .

However, It is not possible to set the time to 250ms with the original render.pl because the soundfiles from AWS include a silence. I have solved it by cutting the end of the soundfiles in render.pl like this:

             unlink "$output_directory/sentence-lower-volume-$speed.mp3" if (-f "$output_directory/sentence-lower-volume-$speed.mp3");
              print "---- move($output_directory/sentence-lower-volume-$speed-resampled.mp3, $cached_filename)\n";
              #move("$output_directory/sentence-lower-volume-$speed-resampled.mp3", $cached_filename);
      my $temp_filename = "$output_directory/sentence-lower-volume-$speed-resampled-temp.mp3";
      move("$output_directory/sentence-lower-volume-$speed-resampled.mp3", $temp_filename);

      #STARTS HERE

          #Get the total length of the soundfiles with ffprobe
      my $ffprobe_cmd = "ffprobe -i $temp_filename -show_entries format=duration -v quiet -of csv=\"p=0\"";
      my $duration = `$ffprobe_cmd`;

      # Trim the last 350ms off the end of the file with ffmpeg
      my $trim_cmd = "ffmpeg -i $temp_filename -ss 0 -t " . ($duration - 0.35) . " -c copy $cached_filename";
      system($trim_cmd) == 0 or die "ERROR: $trim_cmd failed, $!\n";

          #ENDS HERE

      unlink $temp_filename; # Radera temporärfilen
              unlink "$output_directory/sentence-${speed}0000.mp3";
            }

            # generate repeat section if it is different than the sentence
            if (!$no_repeat_morse && $word_limit == -1 && ($repeat_part ne $sentence_part || ($character_multiplier ne "1" && $standard_speed_repeat != 0))
                && (!-f $cached_repeat_filename)) {
              open(my $fh_repeat, '>', "$output_directory/sentence-repeat.txt");
              print $fh_repeat "$repeat_part\n";
              close $fh_repeat;

              my $ebookCmdBase_for_repeat = $ebookCmdBase;
              if($character_multiplier ne "1" && $standard_speed_repeat != 0) {
                $ebookCmdBase_for_repeat =~ s/-m \d+//;

This it not a good solution because I set a fixed time of 350ms to cut and I noticed the the silence length after the soundfiles differ depending on what WPM is used. I am not a programmer, but could it be fixed in a better way? That would be very nice! :)

zoglmannk commented 7 months ago

Let me think about this. Probably... A better way of doing this is by analyzing the audio file for silence and using that info to truncate the file.

zoglmannk commented 7 months ago

@heppstar Take a look at this pull request — https://github.com/zoglmannk/Morse-Code-Ninja/pull/52 .