Skip to content

Commit

Permalink
refactor: remove PatchedBuff (#2335)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheerapakG authored Sep 24, 2023
1 parent 870165f commit 22905cd
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 72 deletions.
68 changes: 0 additions & 68 deletions musicbot/player.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import asyncio
import audioop
import json
import logging
import os
import subprocess
import sys
from array import array
from collections import deque
from enum import Enum
from shutil import get_terminal_size
from threading import Thread

from discord import FFmpegPCMAudio, PCMVolumeTransformer, AudioSource
Expand All @@ -17,74 +13,10 @@
from .entry import URLPlaylistEntry, StreamPlaylistEntry
from .exceptions import FFmpegError, FFmpegWarning
from .lib.event_emitter import EventEmitter
from .utils import avg

log = logging.getLogger(__name__)


class PatchedBuff:
"""
PatchedBuff monkey patches a readable object, allowing you to vary what the volume is as the song is playing.
"""

def __init__(self, buff, *, draw=False):
self.buff = buff
self.frame_count = 0
self.volume = 1.0

self.draw = draw
self.use_audioop = True
self.frame_skip = 2
self.rmss = deque([2048], maxlen=90)

def __del__(self):
if self.draw:
print(" " * (get_terminal_size().columns - 1), end="\r")

def read(self, frame_size):
self.frame_count += 1

frame = self.buff.read(frame_size)

if self.volume != 1:
frame = self._frame_vol(frame, self.volume, maxv=2)

if self.draw and not self.frame_count % self.frame_skip:
# these should be processed for every frame, but "overhead"
rms = audioop.rms(frame, 2)
self.rmss.append(rms)

max_rms = sorted(self.rmss)[-1]
meter_text = "avg rms: {:.2f}, max rms: {:.2f} ".format(
avg(self.rmss), max_rms
)
self._pprint_meter(rms / max(1, max_rms), text=meter_text, shift=True)

return frame

def _frame_vol(self, frame, mult, *, maxv=2, use_audioop=True):
if use_audioop:
return audioop.mul(frame, 2, min(mult, maxv))
else:
# ffmpeg returns s16le pcm frames.
frame_array = array("h", frame)

for i in range(len(frame_array)):
frame_array[i] = int(frame_array[i] * min(mult, min(1, maxv)))

return frame_array.tobytes()

def _pprint_meter(self, perc, *, char="#", text="", shift=True):
tx, ty = get_terminal_size()

if shift:
outstr = text + "{}".format(char * (int((tx - len(text)) * perc) - 1))
else:
outstr = text + "{}".format(char * (int(tx * perc) - 1))[len(text) :]

print(outstr.ljust(tx - 1), end="\r")


class MusicPlayerState(Enum):
STOPPED = 0 # When the player isn't playing anything
PLAYING = 1 # The player is actively playing music.
Expand Down
4 changes: 0 additions & 4 deletions musicbot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ def safe_print(content, *, end="\n", flush=True):
sys.stdout.flush()


def avg(i):
return sum(i) / len(i)


def objdiff(obj1, obj2, *, access_attr=None, depth=0):
changes = {}

Expand Down

0 comments on commit 22905cd

Please sign in to comment.