Author Topic: Smooth sound speed adjustment  (Read 1973 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Smooth sound speed adjustment
« on: October 09, 2021, 04:55:04 am »
Hi. This is another of many experiments with _MEMSOUND. It's a piece of code from another program of mine that I'm writing now. I thought someone might be interested. If someone could change the speed logarithmically, the effect could be achieved as if the DJ on the turntable changes the speed and direction of rotation of the turntable by hand. Now it is set linearly, to demonstrate the functionality of the smooth speed change. Note that it is allowed to play from the end to the beginning, just like when a DJ spins a playback to the other side for a while. Unfortunately, it is still very far from perfect.

It uses _MOUSEMOVEMENTY, so drag the mouse in the Y axis to change the playback speed (and direction).

On row 1 rewrite your audio file name.

Code: QB64: [Select]
  1. SndFile$ = "01 Mydlovar.mp3"
  2. DIM AS _MEM SndL, SndR
  3. DIM AS _OFFSET CurPos, R
  4. DIM AS DOUBLE STP, S
  5.  
  6. Snd = _SNDOPEN(SndFile$)
  7. SndL = _MEMSOUND(Snd, 1)
  8. SndR = _MEMSOUND(Snd, 2)
  9.  
  10. TYPE sam
  11.     Left AS INTEGER
  12.     Right AS INTEGER
  13.  
  14. size& = OFFSET_to_I64(SndL.SIZE) \ 2
  15. DIM Sam(size&) AS sam
  16. STP = 1
  17.  
  18.  
  19. DO UNTIL i = UBOUND(sam)
  20.     Sam(i).Left = _MEMGET(SndL, SndL.OFFSET + ap&, INTEGER)
  21.     Sam(i).Right = _MEMGET(SndR, SndR.OFFSET + ap&, INTEGER)
  22.     i = i + 1
  23.     ap& = ap& + 2
  24.  
  25.  
  26. PRINT "Drag the mouse in the y-axis to correct the speed of sound, ESC for quit!"
  27.  
  28. DO WHILE K& <> 27
  29.     K& = _KEYHIT
  30.  
  31.         MMY = _MOUSEMOVEMENTY
  32.         IF MMY THEN
  33.             STP = STP + (MMY / 100)
  34.         END IF
  35.     WEND
  36.  
  37.     S = S + STP
  38.     IF S > UBOUND(sam) THEN S = UBOUND(sam)
  39.     IF S < LBOUND(sam) THEN S = LBOUND(sam)
  40.  
  41.     LeftValue = Sam(S).Left
  42.     RightValue = Sam(S).Right
  43.  
  44.     _MOUSEMOVE _WIDTH / 2, _HEIGHT / 2
  45.  
  46.     _SNDRAW LeftValue / 32767, RightValue / 32767
  47.  
  48.     DO WHILE _SNDRAWLEN > 0
  49.     LOOP
  50.  
  51. FUNCTION OFFSET_to_I64 (value AS _OFFSET)
  52.     DIM m AS _MEM
  53.     $IF 32BIT THEN
  54.         DIM num AS LONG
  55.         m = _MEM(num)
  56.         _MEMPUT m,m.offset, value
  57.         Offset_to_i64 = num
  58.         _MEMFREE m
  59.     $ELSE
  60.         DIM num AS _INTEGER64
  61.         m = _MEM(num)
  62.         _MEMPUT m, m.OFFSET, value
  63.         OFFSET_to_I64 = num
  64.         _MEMFREE m
  65.     $END IF
  66.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Smooth sound speed adjustment
« Reply #1 on: October 09, 2021, 05:21:49 am »
Small modification of the previous program - now press the left mouse button to change the speed while moving the mouse in the y-axis. When you release the mouse button, the music smoothly returns to normal speed and direction of playback (simulation of the turntable motor, which gives the board the correct speed and direction at the moment when the DJ stops influencing the speed by hand)

Code: QB64: [Select]
  1. SndFile$ = "01 Mydlovar.mp3"
  2. DIM AS _MEM SndL, SndR
  3. DIM AS _OFFSET CurPos, R
  4. DIM AS DOUBLE STP, S
  5.  
  6. Snd = _SNDOPEN(SndFile$)
  7. SndL = _MEMSOUND(Snd, 1)
  8. SndR = _MEMSOUND(Snd, 2)
  9.  
  10. TYPE sam
  11.     Left AS INTEGER
  12.     Right AS INTEGER
  13.  
  14. size& = OFFSET_to_I64(SndL.SIZE) \ 2
  15. DIM Sam(size&) AS sam
  16. STP = 1
  17.  
  18.  
  19. DO UNTIL i = UBOUND(sam)
  20.     Sam(i).Left = _MEMGET(SndL, SndL.OFFSET + ap&, INTEGER)
  21.     Sam(i).Right = _MEMGET(SndR, SndR.OFFSET + ap&, INTEGER)
  22.     i = i + 1
  23.     ap& = ap& + 2
  24.  
  25.  
  26. PRINT "Drag the mouse in the y-axis to correct the speed of sound, ESC for quit!"
  27.  
  28. 'S = i / 2 'play track from middle
  29.  
  30. DO WHILE K& <> 27
  31.     K& = _KEYHIT
  32.  
  33.         MMY = _MOUSEMOVEMENTY
  34.         IF MMY THEN
  35.             IF _MOUSEBUTTON(1) THEN
  36.                 STP = STP + (MMY / 100)
  37.             END IF
  38.         END IF
  39.     WEND
  40.  
  41.     IF _MOUSEBUTTON(1) = 0 THEN
  42.         IF STP < 1 THEN STP = STP + .0001
  43.         IF STP > 1 THEN STP = STP - .0001
  44.     END IF
  45.  
  46.  
  47.  
  48.     S = S + STP
  49.     IF S > UBOUND(sam) THEN S = UBOUND(sam)
  50.     IF S < LBOUND(sam) THEN S = LBOUND(sam)
  51.  
  52.     LeftValue = Sam(S).Left
  53.     RightValue = Sam(S).Right
  54.  
  55.     _MOUSEMOVE _WIDTH / 2, _HEIGHT / 2
  56.  
  57.     _SNDRAW LeftValue / 32767, RightValue / 32767
  58.  
  59.     DO WHILE _SNDRAWLEN > 0
  60.     LOOP
  61.  
  62. FUNCTION OFFSET_to_I64 (value AS _OFFSET)
  63.     DIM m AS _MEM
  64.     $IF 32BIT THEN
  65.         DIM num AS LONG
  66.         m = _MEM(num)
  67.         _MEMPUT m,m.offset, value
  68.         Offset_to_i64 = num
  69.         _MEMFREE m
  70.     $ELSE
  71.         DIM num AS _INTEGER64
  72.         m = _MEM(num)
  73.         _MEMPUT m, m.OFFSET, value
  74.         OFFSET_to_I64 = num
  75.         _MEMFREE m
  76.     $END IF
  77.