0

I want to adjust the volume of the mp3 file while it is being playing by adjusting the potentiometer. I am reading the potentiometer signal serially via Arduino board with python scripts. With the help of pydub library i can able to read the file but cannot adjust the volume of the file while it is being playing. This is the code i have done after a long search I specified only the portion of Pydub part. for your information im using vlc media player for changing the volume.

>>> from pydub import AudioSegment
>>> song = AudioSegment.from_wav("C:\Users\RAJU\Desktop\En_Iniya_Ponnilave.wav")

While the file is playing, i cannot adjust the value. Please, someone explain how to do it.

Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
rajupurush
  • 11
  • 4
  • pydub is a library for *manipulating audio*; it does not play audio [by itself](https://github.com/jiaaro/pydub/blob/master/pydub/playback.py). "Changing volume", on the other hand is manipulating *the player* - and you haven't mentioned [which player you're using](https://github.com/jiaaro/pydub/blob/master/pydub/utils.py#L119). You should edit your question to make all this clear - this is **not** a pydub question – loopbackbee Feb 13 '14 at 12:14
  • dear Goncalopp, Thanks for your answers. I will consider reedit the question. Actually iam using VLC player. is there any library available for manipulating the player? or can i use os.system or something like that. Thank you – rajupurush Feb 13 '14 at 16:31
  • You probably should check the [VLC python bindings](https://wiki.videolan.org/Python_bindings) then – loopbackbee Feb 13 '14 at 17:33

2 Answers2

0

First you need decode your audio signal to raw audio and Split your signal in X frames, and you can manipulate your áudio and at every frame you can change Volume or change the Pitch or change the Speed, etc!

To change the volume you just need multiply your raw audio vector by one factor (this can be your potentiometer data signal).

This factor can be different if your vector are in short int or float point format !

One way to get raw audio data from wav files in python is using wave lib

import wave

spf = wave.open('wavfile.wav','r')

#Extract Raw Audio from Wav File
signal = spf.readframes(-1)
decoded = numpy.fromstring(signal, 'Float32');

Now you can multiply the vector decoded by one factor, for example if you want increase 10dB you need calculate 10^(DbValue/20) then in python 10**(10/20) = 3.1623

newsignal = decoded * 3.1623;

Now you need encode the vector again to play the new framed audio, you can use "from struct import pack" and pyaudio to do it!

stream = pyaud.open(
    format = pyaudio.paFloat32,
    channels = 1,
    rate = 44100,
    output = True,
    input = True)

EncodeAgain = pack("%df"%(len(newsignal)), *list(newsignal))

And finally Play your framed audio, note that you will do it at every frame and play it in one loop, this process is too fast and the latency can be imperceptibly !

stream.write(EncodeAgain)

PS: This example is for float point format !

ederwander
  • 3,410
  • 1
  • 18
  • 23
  • While this is very useful information, note that he wants to change the volume *while the file is playing*, not actually change the amplitude of the data in the file – loopbackbee Feb 13 '14 at 17:34
  • yes I know, this is the way, if you split your audio that you want play in N frames may be of size = 4096, the "stream.write" will play the audio at every Frame, now you can change the amplitude while playing ... – ederwander Feb 13 '14 at 17:42
  • Thank you very much ederwander.. You understand my question and has given a accurate answer. it is streaming but i cannot stop the stream and also i unable to change the amplitude.. even if i kill the process the file is streaming continuosly. It can be stopped only when restarting the pc.. – rajupurush Feb 14 '14 at 06:12
0

Ederwander,As u said I have treid coding but when packing the data, im getting total zero. so it is not streaming. I understand the problem may occur in converting the format data types.This is the code i have written. Please look at it and say the suggestion

    import sys
    import serial
    import time
    import os
    from pydub import AudioSegment
    import wave
    from struct import pack
    import numpy
    import pyaudio
    CHUNK = 1024

    wf = wave.open('C:\Users\RAJU\Desktop\En_Iniya_Ponnilave.wav', 'rb')

    # instantiate PyAudio (1)
    p = pyaudio.PyAudio()

    # open stream (2)
    stream = p.open(format = p.get_format_from_width(wf.getsampwidth()),channels =   wf.getnchannels(),rate = wf.getframerate(),output = True)

    # read data
    data_read = wf.readframes(CHUNK)
    decoded = numpy.fromstring(data_read, 'int32', sep = '');
    data = decoded*3.123
    while(1):
          EncodeAgain = struct.pack(h,data)
           stream.write(EncodeAgain)
rajupurush
  • 11
  • 4