Author Topic: Mix like on a turntable  (Read 2606 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.
    • View Profile
Mix like on a turntable
« on: December 02, 2021, 02:01:59 pm »
Imagine that your computer is a DJ on a turntable and randomly changes direction and speed during playback. This is the first (but not the last) version of this effect.

Code: QB64: [Select]
  1. misto = 44100 * 5 '5 seconds after music start playing start effect
  2. mistoE = misto + 88200 'effect ends 2 seconds after effect start
  3. rychlost = 88200
  4.  
  5. Dim As _MEM L, R, L2, R2
  6. Dim As Integer LS, RS
  7.  
  8. file$ = "a.mp3"
  9. Print "Opening sound file "; file$
  10. f = _SndOpen(file$)
  11. L = _MemSound(f, 1)
  12. R = _MemSound(f, 2)
  13.  
  14. Type SND
  15.     L As Integer
  16.     R As Integer
  17. Dim snd(_SndRate * _SndLen(f)) As SND
  18.  
  19. Print "Creating standard array"
  20. Do Until Done& = L.SIZE
  21.     snd(i).L = _MemGet(L, L.OFFSET + Done&, Integer)
  22.     snd(i).R = _MemGet(R, R.OFFSET + Done&, Integer)
  23.     i = i + 1
  24.     Done& = Done& + 2
  25.  
  26. i = i - 2
  27.  
  28. Dim snd2(3 * UBound(snd)) As SND 'this time i do not calculate array size - because this demo use random output lenght
  29. zacatek = misto
  30. konec = mistoE
  31. psi2 = _Pi(1) / (zacatek - konec)
  32. Dim As Long misto, mistoE
  33.  
  34. copy = 0
  35. Print "Creating pseudo mix"
  36. Do Until copy >= UBound(snd) - 1
  37.     If original > misto And original < mistoE Then
  38.         k2 = k2 + psi2
  39.         newi = Sin(k2) * 44100
  40.         copy = ocopy + newi
  41.         original = original + Abs(Sin(k2))
  42.     Else
  43.         ocopy = copy
  44.         copy = copy + 1
  45.         original = Int(original + 1)
  46.     End If
  47.  
  48.     If original > mistoE + 44100 Then 'pause between two mix hits (44100 = 1 sec)
  49.         misto = original + 44100 * Rnd 'effect start in samples (44100 x time)
  50.         mistoE = misto + 44100 * Rnd + 500 'effect end in samples
  51.         zacatek = misto
  52.         konec = mistoE
  53.         psi2 = _Pi(1) / (zacatek - konec)
  54.  
  55.         If misto > UBound(snd2) Or misto2 > UBound(snd2) Then misto = 0: mistoE = 0
  56.     End If
  57.  
  58.  
  59.  
  60.     If original > UBound(snd2) Then Print "Snd2 overlow"
  61.     If copy > UBound(snd) Then Print "Snd overlow"; copy; krok
  62.  
  63.     snd2(original).L = snd(copy).L
  64.     snd2(original).R = snd(copy).R
  65.  
  66. Print "Playing"
  67. For test = 0 To original
  68.     _SndRaw snd2(test).L / 32768, snd2(test).R / 32768
  69.  
  70.