From: jweigele Date: Sun, 25 Sep 2022 05:12:13 +0000 (-0700) Subject: Flip back to ffmpeg decoding (see didn't even need to change the name of the file!) X-Git-Url: http://git.hexthepla.net/?a=commitdiff_plain;h=ffbf0e94dc450869754baf8ea5e74f21c11cd223;p=grahbot Flip back to ffmpeg decoding (see didn't even need to change the name of the file!) * AudioFile processing was too slow through pedalboard * Allows some of the async to get work done while spinning off the subprocess, maybe? * And there was a bug with certain files just not being read properly (okay??) Seems to work better now! --- diff --git a/Dockerfile b/Dockerfile index c901502..3d28339 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN apt-get update #RUN apk musl-dev RUN apt-get install -y python3-dev RUN apt-get install -y python3-numpy -#RUN apt-get install -y ffmpeg +RUN apt-get install -y ffmpeg RUN apt-get install -y python3-wheel RUN apt-get install -y python3-pip RUN apt-get install -y python3-cffi diff --git a/ffmpegfile.py b/ffmpegfile.py index 47cbd43..e66db9f 100644 --- a/ffmpegfile.py +++ b/ffmpegfile.py @@ -156,10 +156,20 @@ class AudioBuffer(discord.AudioSource): #next_bytes = byte_reader.read(MAX_LENGTH) buffer_samples = None - with AudioFile(filename).resampled_to(target_sample_rate=SAMPLE_RATE) as f: - # we're reading in the whole file at once, maybe don't want to later for better responsiveness? - buffer_samples = f.read(f.frames) - #log.debug('samples length: {}'.format(len(buffer_samples))) + args = ['ffmpeg', '-i', '{}'.format(filename)] + args += ['-f', 's16le', '-acodec', 'pcm_s16le', '-ac', '1', '-ar', '48000', '-'] + p = await asyncio.create_subprocess_exec(*args, stdin=None, stderr=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE) + stdout, stderr = await p.communicate() + log.debug(stderr) + return_bytes = stdout + await p.wait() + byte_length = len(return_bytes) + buffer_samples = numpy.array([x[0] for x in struct.iter_unpack(' 0: channels = len(next_samples.shape) while len(next_samples) > 0: # this is to format the samples in the style opus wants #log.debug('channels are {}'.format(channels)) - if channels == 2: - # interleave the samples L/R to form a bytestream eventually - next_samples = numpy.squeeze(numpy.dstack((next_samples[0], next_samples[1])).reshape(1, -1)) + #if channels == 2: + # # interleave the samples L/R to form a bytestream eventually + # next_samples = numpy.squeeze(numpy.dstack((next_samples[0], next_samples[1])).reshape(1, -1)) # right now, this is the only path taken (because we discard the other channel) - else: - next_samples = numpy.repeat(next_samples, 2) + #else: + next_samples = numpy.repeat(next_samples, 2) # for the downcast to int16 next_samples *= 32768.0 next_bytes = next_samples.astype('int16') @@ -189,7 +199,7 @@ class AudioBuffer(discord.AudioSource): chunks_to_append.append(chunk) # get next chunk index += MAX_SAMPLES - next_samples = buffer_samples[0][index:index+MAX_SAMPLES] + next_samples = buffer_samples[index:index+MAX_SAMPLES] # chunk append/insertion (along with mixing)