3v324v23 commited on
Commit
3e6df42
1 Parent(s): 1691f82

added limits to avoid infinite queue

Browse files
Files changed (1) hide show
  1. beat_manipulator/main.py +27 -12
beat_manipulator/main.py CHANGED
@@ -10,7 +10,7 @@ class song:
10
  if audio is None:
11
  from tkinter import filedialog
12
  audio = filedialog.askopenfilename()
13
-
14
  if isinstance(audio, song): self.path = audio.path
15
  self.audio, self.sr = io._load(audio=audio, sr=sr)
16
 
@@ -18,7 +18,7 @@ class song:
18
  if isinstance(audio, str):
19
  self.path = audio
20
  elif not isinstance(audio, song):
21
- self.path = 'unknown_' + str(hex(int(np.sum(self.audio)*(10**18))))
22
 
23
  self.log = log
24
  self.beatmap = None
@@ -27,12 +27,10 @@ class song:
27
  def _slice(self, a):
28
  if a is None: return None
29
  elif isinstance(a, float):
30
- if (a_dec:=a%1) != 0:
31
- a_int = int(int(a)//1)
32
- start = self.beatmap[a_int]
33
- return int(start + a_dec * (self.beatmap[a_int+1] - start))
34
- else:
35
- return self.beatmap[int(a)]
36
  elif isinstance(a, int): return self.beatmap[a]
37
  else: raise TypeError(f'slice indices must be int, float, or None, not {type(a)}. Indice is {a}')
38
 
@@ -70,8 +68,8 @@ class song:
70
  song_copy.beatmap = self.beatmap.copy()
71
  song_copy.beatmap = np.insert(song_copy.beatmap, 0, 0)
72
  result = song_copy.beatswap(pattern = pattern, return_audio = True)
73
- if isinstance(self.audio, np.ndarray): return result
74
- else: return result.tolist()
75
 
76
  elif isinstance(s, float):
77
  start = self._slice(s-1)
@@ -158,7 +156,7 @@ class song:
158
  beatmap.save_settings(audio = self.audio, filename = self.path, scale = scale, shift = shift,adjust = adjust, normalized = normalized, log=self.log, overwrite=overwrite, lib = self.lib)
159
 
160
  def beatswap(self, pattern = '1;"cowbell"s3v2, 2;"cowbell"s2, 3;"cowbell", 4;"cowbell"s0.5, 5;"cowbell"s0.25, 6;"cowbell"s0.4, 7;"cowbell"s0.8, 8;"cowbell"s1.6',
161
- scale:float = 1, shift:float = 0, length = None, samples:dict = BM_SAMPLES, effects:dict = BM_EFFECTS, metrics:dict = BM_METRICS, smoothing: int = 100, adjust=500, return_audio = False, normalize = False):
162
 
163
  if normalize is True:
164
  self.normalize_beats()
@@ -245,18 +243,27 @@ class song:
245
  #for i in pattern: print(i)
246
 
247
 
248
-
 
249
 
250
  # loop over pattern until it reaches the last beat
251
  while n*pattern_length <= len(self.beatmap):
252
  n+=1
253
 
 
 
254
  # Every time pattern loops, shuffles beats with #
255
  if len(shuffle_beats) > 0:
256
  pattern = parse._shuffle(pattern, shuffle_beats, shuffle_groups)
257
 
258
  # Loops over all beats in pattern
259
  for num, b in enumerate(pattern):
 
 
 
 
 
 
260
  if len(b) == 4: beat = b[3] # Sample has length 4
261
  else: beat = b[0] # Else take the beat
262
 
@@ -370,7 +377,15 @@ class song:
370
  else:
371
  beat = e(beat, v)
372
 
 
373
  beat = np.clip(beat, -1, 1)
 
 
 
 
 
 
 
374
 
375
  # Adds the processed beat to list of beats.
376
  # Separator is `,`
 
10
  if audio is None:
11
  from tkinter import filedialog
12
  audio = filedialog.askopenfilename()
13
+
14
  if isinstance(audio, song): self.path = audio.path
15
  self.audio, self.sr = io._load(audio=audio, sr=sr)
16
 
 
18
  if isinstance(audio, str):
19
  self.path = audio
20
  elif not isinstance(audio, song):
21
+ self.path = f'unknown_{hex(int(np.sum(self.audio) * 10**18))}'
22
 
23
  self.log = log
24
  self.beatmap = None
 
27
  def _slice(self, a):
28
  if a is None: return None
29
  elif isinstance(a, float):
30
+ if (a_dec := a % 1) == 0: return self.beatmap[int(a)]
31
+ a_int = int(int(a)//1)
32
+ start = self.beatmap[a_int]
33
+ return int(start + a_dec * (self.beatmap[a_int+1] - start))
 
 
34
  elif isinstance(a, int): return self.beatmap[a]
35
  else: raise TypeError(f'slice indices must be int, float, or None, not {type(a)}. Indice is {a}')
36
 
 
68
  song_copy.beatmap = self.beatmap.copy()
69
  song_copy.beatmap = np.insert(song_copy.beatmap, 0, 0)
70
  result = song_copy.beatswap(pattern = pattern, return_audio = True)
71
+ return result if isinstance(self.audio, np.ndarray) else result.tolist()
72
+
73
 
74
  elif isinstance(s, float):
75
  start = self._slice(s-1)
 
156
  beatmap.save_settings(audio = self.audio, filename = self.path, scale = scale, shift = shift,adjust = adjust, normalized = normalized, log=self.log, overwrite=overwrite, lib = self.lib)
157
 
158
  def beatswap(self, pattern = '1;"cowbell"s3v2, 2;"cowbell"s2, 3;"cowbell", 4;"cowbell"s0.5, 5;"cowbell"s0.25, 6;"cowbell"s0.4, 7;"cowbell"s0.8, 8;"cowbell"s1.6',
159
+ scale:float = 1, shift:float = 0, length = None, samples:dict = BM_SAMPLES, effects:dict = BM_EFFECTS, metrics:dict = BM_METRICS, smoothing: int = 100, adjust=500, return_audio = False, normalize = False, limit_beats=10000, limit_length = 52920000):
160
 
161
  if normalize is True:
162
  self.normalize_beats()
 
243
  #for i in pattern: print(i)
244
 
245
 
246
+ stop = False
247
+ total_length = 0
248
 
249
  # loop over pattern until it reaches the last beat
250
  while n*pattern_length <= len(self.beatmap):
251
  n+=1
252
 
253
+ if stop is True: break
254
+
255
  # Every time pattern loops, shuffles beats with #
256
  if len(shuffle_beats) > 0:
257
  pattern = parse._shuffle(pattern, shuffle_beats, shuffle_groups)
258
 
259
  # Loops over all beats in pattern
260
  for num, b in enumerate(pattern):
261
+
262
+ # check if beats limit has been reached
263
+ if limit_beats is not None and len(result) >= limit_beats:
264
+ stop = True
265
+ break
266
+
267
  if len(b) == 4: beat = b[3] # Sample has length 4
268
  else: beat = b[0] # Else take the beat
269
 
 
377
  else:
378
  beat = e(beat, v)
379
 
380
+ # clip beat to -1, 1
381
  beat = np.clip(beat, -1, 1)
382
+
383
+ # checks if length limit has been reached
384
+ if limit_length is not None:
385
+ total_length += len(beat[0])
386
+ if total_length>= limit_length:
387
+ stop = True
388
+ break
389
 
390
  # Adds the processed beat to list of beats.
391
  # Separator is `,`