sending line in data to a speaker and to a pwm pin

Moderator: flatmax

Post Reply
lapollaz
Posts: 3
Joined: Wed Jun 07, 2017 1:10 am

sending line in data to a speaker and to a pwm pin

Post by lapollaz » Wed Jun 07, 2017 8:53 pm

Hi,
I’ve got an Audio Injector stereo soundcard, I have installed the .deb package, made some tests and everything seems fine.
My next step would be to get the sound from line in (connected to the headphone socket of a radio) and perform two things simultaneously: 1) sending the data to an amplifier/speaker connected to the audio out and 2) read the audio data to pilot a PWM pin of the raspberry (to drive a fan accordingly). Is it possible? Any suggestion to get this done?
In particular how to read the audio in? (something like analogRead() in Arduino)

thanks in advance

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: sending line in data to a speaker and to a pwm pin

Post by flatmax » Thu Jun 08, 2017 12:53 pm

Hi there,

What language do you want to use ?
Peter and dBm have been using python 3.0 on this thread :
viewtopic.php?f=5&t=1098

Perhaps you would like to join in there ?

Otherwise if you want to use C++ I can give you an example.

By the way, are you wanting to do PWM or just turn the GPIO on and off ?

thanks
Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

lapollaz
Posts: 3
Joined: Wed Jun 07, 2017 1:10 am

Re: sending line in data to a speaker and to a pwm pin

Post by lapollaz » Fri Jun 09, 2017 1:09 am

Hi Matt,

thanks for your prompt reply. I don’t know what language I will use, maybe a little more familiar with C but I will try soon the python example in the thread you pointed out. If you have a C++ example done, it will be great, too.

For the output I really need to use PWM capability, not just turning on and off, even if it is not totally clear to me at this point if it is feasible and how, because of the limited number of PWM pin on the rpi (I’m much more familiar with arduino environment), maybe I have to use software driven PWM handler (like softPwm library).

thanks
valentina

flatmax
Posts: 609
Joined: Sat Jul 23, 2016 11:39 pm

Re: sending line in data to a speaker and to a pwm pin

Post by flatmax » Sat Jun 10, 2017 9:29 pm

Great,

I am glad you have an idea for the PWM part.

By the way you can use the 'bypass' functionality in the alsamixer to send the input audio directly to the output with almost zero delay, meanwhile you can probably also process the input audio to operate your pwm.

For C++, a simple way to program the audio for analysis is to use jackd in combination with a jack client, such as the example jack client in the gtkiostream software.
Here for example is the location of a simple jack client.

In your case, perhaps rename and alter the JackClientTest class to allow you to program some audio manipulation code. For example :

Code: Select all

class JackPWMClient : public JackClient {
    float thresh; // a class variable

    int processAudio(jack_nframes_t nframes) { 
        //      put output data into the buffers - you man not want to do this if you are using the analogue bypass in the mixer.
        for (uint i=0; i<outputPorts.size(); i++) {
            jack_default_audio_sample_t *out = ( jack_default_audio_sample_t* ) jack_port_get_buffer ( outputPorts[i], nframes );
            for (uint j=0; j<nframes; j++)
                out[j]=0.;
        }

        float rms=0.;
        //      print input data rms power to std out per channel
        for (uint i=0; i<inputPorts.size(); i++) {
            jack_default_audio_sample_t *in = ( jack_default_audio_sample_t* ) jack_port_get_buffer ( inputPorts[i], nframes );
            for (uint j=0; j<nframes; j++)
                rms+=in[j]*in[j];
            cout<<"input ch "<<i<<" rms = "<<sqrt(rms/nframes)<<'\t';
        }
        cout<<'\n';
        return 0;
    }
public:
    TestJackClient() {
        thresh=0.1; // set the class variable
    }
};
Matt
Check out our audiophile quality crossovers : https://bit.ly/2kb1nzZ
Please review the Zero sound card on Amazon USA : https://www.amazon.com/dp/B075V1VNDD
---
Check out our new forum on github : https://github.com/Audio-Injector

lapollaz
Posts: 3
Joined: Wed Jun 07, 2017 1:10 am

Re: sending line in data to a speaker and to a pwm pin

Post by lapollaz » Fri Jul 07, 2017 6:17 pm

Hi Matt
sorry, I had a very busy month.

Trying to use pyaudio with pyhton 2.7 we encountered issue related to jackd not running; solved referring to
https://github.com/alexylem/jarvis/issues/364
In particular
sudo apt-get install dbus-x11
sudo apt-get install jackd2
dbus-launch jack_control start

and everything works fine.
Here is the basic script

Code: Select all

import pyaudio
import struct
import math
import numpy as np
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(33, GPIO.OUT)

my_pwm = GPIO.PWM(33, 50)  # frequency=50Hz
my_pwm.start(0)


CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100


p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index=0,
                frames_per_buffer=CHUNK)

print("* recording")

while True:
    data = stream.read(CHUNK)
    numpydata = np.fromstring(data, dtype=np.int16)
    mean = np.mean(numpydata)
    mean_pwm =math.fabs(mean)
    if(mean_pwm <100):
        my_pwm.ChangeDutyCycle(mean_pwm)
    else:
        my_pwm.ChangeDutyCycle(100)

my_pwm.stop()
GPIO.cleanup()
stream.close()
p.terminate()

The bypass functionality in alsamixer works as well. Thanks for the help!
valentina

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests