Author Topic: Audio Reverse (play audio file in reverse)  (Read 2021 times)

0 Members and 1 Guest are viewing this topic.

Offline mynameispaul

  • Newbie
  • Posts: 49
Audio Reverse (play audio file in reverse)
« on: September 13, 2020, 02:30:57 pm »
' This program does not play backwards perfectly. Basically the program is
' still playing the audio file in the forwards direction using QB64 command
' _SNDPLAY, but the program constantly is using the _SNDSETPOS command to
' "back up" to a previous location in the audio playback. By playing only
' very small "blocks" of audio, working backwards, it makes it sound like
' the audio file is being played backwards.
' I recomend running the "Update Count Per Second" option on the main menu
' when you first run the program. (but you don't have to) This option will
' see how much the program can count in 1 second (10x). Hard to explain but
' helps the program work better.
' Play around with the other variables in its config file
' - SecondsPositionAdjust
' - DelayCounterAdjust
' Maybe you can come up with a better settings than what i used.
* AudioReverse.zip (Filesize: 12.83 KB, Downloads: 147)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Audio Reverse (play audio file in reverse)
« Reply #1 on: September 15, 2020, 03:06:13 am »
Hello. To actually play an audio file from back to front, you must have RAW audio decoders. In WAV format, this is easiest to do if WAV does not contain ID3 information. The first 45 bytes in the file are the header. You leave the same here and turn over the rest of the audio data.


For reverse and play WAV files: (8 bit mono/stereo - BYTE swap, for 16 bit is need INTEGERs swap)
Code: QB64: [Select]
  1. DEFLNG A-Z
  2. rev$ = "2.wav" '                                                    original wav audio file
  3.     DIM Head AS STRING * 45
  4.     ff = FREEFILE
  5.     OPEN rev$ FOR BINARY AS ff
  6.     GET ff, 1, Head$
  7.     OriginalAudio = LOF(ff) - 44
  8.     DIM OA(OriginalAudio) AS _UNSIGNED _BYTE
  9.     DIM NA(OriginalAudio) AS _UNSIGNED _BYTE
  10.     GET ff, , OA()
  11.     DIM m AS _MEM, n AS _MEM, V AS _UNSIGNED _BYTE
  12.     m = _MEM(OA()) '                                                 old audio data structure (original)
  13.     n = _MEM(NA()) '                                                 new (reverse) audiodata structure
  14.     DO UNTIL R = m.SIZE '                                            copy original audiodata so as is in file to memory
  15.         V = _MEMGET(m, m.OFFSET + m.SIZE - R - 1, _UNSIGNED _BYTE)
  16.         _MEMPUT n, n.OFFSET + R, V '                                 write it in reverse order to new memory block
  17.         R = R + 1
  18.     LOOP
  19.     CLOSE ff
  20.     PRINT "creating reverse WAV file..."
  21.     rev2$ = "REV" + rev$ '
  22.     OPEN rev2$ FOR BINARY AS ff
  23.     PUT ff, 1, Head$ '                                               write original audio header to new wav file
  24.     PUT ff, , NA() '                                                 write reversed audiodata to new audiofile
  25.     CLOSE ff
  26.     _MEMFREE m
  27.     _MEMFREE n
  28.     _SNDPLAYFILE rev2$ '                                             play new audio file
  29.  
« Last Edit: September 15, 2020, 10:28:02 am by Petr »