eouia / MMM-Assistant

(Deprecated) This project is not supported any more from original owner. Use alternative MMM-AssistantMk2(https://github.com/eouia/MMM-AssistantMk2)
https://github.com/eouia/MMM-AssistantMk2
MIT License
25 stars 8 forks source link

What is the correct ALSA setting? #24

Open E3V3A opened 6 years ago

E3V3A commented 6 years ago

In the Wiki:

You SHOULD be able to execute below commands on terminal. arecord -d 10 test.wav && aplay test.wav

That fails with:

$ arecord -d 10 test6.wav && aplay test6.wav

Recording WAVE 'test6.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono
arecord: set_params:1299: Sample format non available
Available formats:
- S16_LE

So then I tried with:

$ arecord -d 10 -fS16_LE test6.wav && aplay test6.wav

Recording WAVE 'test6.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
Warning: rate is not accurate (requested = 8000Hz, got = 44100Hz)
         please, try the plug plugin (-Dplug:default)
Playing WAVE 'test6.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono
aplay: set_params:1305: Channels count non available

# but this works:
arecord --device=hw:1,0 --format S16_LE --rate 44100 -c1 -V mono test5.wav 

Since I only have an ALSA in ~/.asoundrc, I guess MM never see my settings or they are not right. What are the settings you have?

eouia commented 6 years ago

As I wrote in Wiki,

If fails, You should find the way to use mic and speaker on your MagicMirror. But I cannot give you a help about that kind of problem.

Because there could be many reasons for not working sound system. I cannot guess which is the real problem. Well, sometimes I failed even in my own systems... :( I'm not an expert of that kind. I have no idea how to set properly in your ALSA config. I don't know your types of devices and your current settings. but your problem looks like device number assign problem. Maybe https://www.raspberrypi.org/forums/viewtopic.php?t=124016 could be a help or at least beginning of solution.

E3V3A commented 6 years ago

Problem is that I'm able to play but I have no idea how the MMM-Assistant is using the sound devices.

eouia commented 6 years ago
arecord --device=hw:1,0 ....

It means this device is not configured as DEFAULT device, so

$ arecord -d 10 test6.wav && aplay test6.wav

doesn't work. MM-Assistant use arecord as default recorder and using it's default setting. so you should find way to set your target device as default device for your RPI.

And you have another options to use rec or sox. (See the record section in full version of configuration), but in that case, you also make your device as default of each sw.

E3V3A commented 6 years ago

@eouia Hi, yeah, I'm working on this now. Seem different sound related modules, use different ALSA settings. rec works on cli, for me, but I still need to try with Assistant. This now works:

# arecord -d 10 zz.wav && aplay zz.wav

Recording WAVE 'zz.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono
Playing WAVE 'zz.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono

But the default bitrate is very low, giving a very crappy recording. Using rec is much better:

 # rec zz1.wav && aplay zz1.wav

Input File     : 'default' (alsa)
Channels       : 2
Sample Rate    : 48000
Precision      : 16-bit
Sample Encoding: 16-bit Signed Integer PCM

In:0.00% 00:00:14.08 [00:00:00.00] Out:672k  [      |      ]        Clip:0    ^C
Aborted.
Playing WAVE 'zz1.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
E3V3A commented 6 years ago

Hey @eouia
It's alive!! And its Awesome!

Ok here is the lowdown.

# cat ~/.asoundrc

# To ensure you don't have to specify the default *devices* (like `--device="hw:1,0"`).
pcm.!default {
  type asym
   playback.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
   capture.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
}

# This set the default control options to use *hw* card #1 
ctl.!default {
        type hw
        card 1
}

NOTE:

  1. This is surely not the only config that will work for you. As noted above, this config still default to a crappy bit-rate, but since it works, let's keep it simple like this for now.
  2. If you update/change your ~/.asoundrc, you must run sudo /etc/init.d/alsa-utils restart to make it take effect!
# aplay -l && arecord -l

**** List of PLAYBACK Hardware Devices ****
card 1: Set [C-Media USB Headphone Set], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
**** List of CAPTURE Hardware Devices ****
card 1: Set [C-Media USB Headphone Set], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

# cat /proc/asound/modules
 1 snd_usb_audio

# cat /proc/asound/cards
 1 [Set            ]: USB-Audio - C-Media USB Headphone Set
                      C-Media USB Headphone Set at usb-3f980000.usb-1.5, full speed

Next, as I said above, rec seem to work better out-of-the-box. But it is a wrapper for sox so you need to ensure you have sox + dependencies installed. Then edit your full MMM-Assistant configuration config.js. To this:

{
  module: 'MMM-Assistant',
  position: 'bottom_left',
  config: {
    assistant: {
      auth: {
        keyFilePath: "secret.json",             // REQUIRED (Google Assistant API) -- OAuth2 x509 cert
        savedTokensPath: "resources/tokens.js"  // REQUIRED (Google Assitant API) -- accesss_token & refresh_token
      },
      audio: {
        encodingIn: "LINEAR16",     // Default. No need to change.
        sampleRateOut: 16000        // Default. No need to change.
      }
    },
    snowboy: {
      models: [
        {
          file: "resources/smart_mirror.umdl",  // This file define your MM wake word. (See doc notes.)
          sensitivity: 0.5,
          hotwords : "MIRROR"                   // Default model: "MIRROR". (This is not the wake word!)
        },
        {
          file: "resources/snowboy.umdl",       // This file define your GA wake word. (See doc notes.)
          sensitivity: 0.5,
          hotwords : "ASSISTANT"                // Default model: "ASSISTANT". (This is not the wake word!)
        }
      ]
    },
    record: {
      threshold: 0,                 // Default. No need to change.
      verbose:false,                // Deafult: true  -- for checking recording status.
      recordProgram: 'rec',         // You can use 'rec', 'sox', but we recommend 'arecord'
      silence: 2.0                  // Default. No need to change.
    },
    stt: {
      auth: [{                       // You can use multiple accounts to save money
        projectId:'mmm-xx',                         // REQUIRED (Google Voice API) -- project_id
        keyFilename: 'MMM-XX-aaaabbbbcccc.json'     // REQUIRED (Google Voice API) -- service_account / private_key
      }],
      request: {
        encoding: 'LINEAR16',       // Default. No need to change.
        sampleRateHertz: 16000,     // Default. No need to change.
        languageCode: 'en-US'       // [en-US]  To set the default GA speech request language.
                                    // (See: https://cloud.google.com/speech/docs/languages)
      },
    },
    speak: {
      useAlert: true,               // [true]  Enable this to show the understood text of your speech
      language: 'en-US',            // [en-US] To set the default GA speech reply language.
    },
    alias: [                        // You can use aliases for difficult pronunciation or easy using.
      {
        "help :command" : ["teach me :command", "what is :command"]
      }
    ]
  }
},

Then run MM with PM2 normally. To see the result of your voice input, follow the pm2 log files in a terminal with this:

# put this in your .bashrc for easy use
alias tailo='tail -f ~/.pm2/logs/mm-out-0.log'
tailo
...

Note, the wake words are smart mirror (for MM control) and snowboy (for GA). Probably it would help to tune those files to your own voice, but I have no idea how to do that.

Next, I discovered a bug that freezes the Assistant module. See issue #25.