2019-12-02 Weekend Learnings

December 2, 2019 • edited September 6, 2024

I managed to send audio to my Google Home, discovered the Go tool trace and started a post about Python and Jupyter.

My new Google Home

I was playing with my new Google Home device when I wonder if it would be possible to trigger local actions, like sending a message to my Raspberry Pi. After some research I found the Hiden API of Google home but It is a little bit disappointing. There is nothing interesting to do with it.

The I discovered a post about how to use the Google Home as a Choromecast and this was a little bit more promising. The post starts with a simple tutorial to control the volume using pychromecast library. Then, using gtts Google Text To Speech library, generates an mp3 file and send a request to the Google Home with the url that serves that sound. In the original post, It’s using an Apache web server, I replaced it by a simple web static file server in Go. Here is the code. The logRequest is just to have some feedback.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
	"log"
	"net/http"
)

func main() {
	fs := logRequest(http.FileServer(http.Dir("music")))
	http.Handle("/mp3_cache/", fs)

	log.Println("Listening...")
	http.ListenAndServe(":80", nil)
}

func logRequest(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Println("Request")
		next.ServeHTTP(w, r)
	})
}

So this code basically serves the files in the directory music. You just have to modify the Python code to store the mp3 there. Keep in mind that by default, Go looks for the full path in the given directory. This means that the folder structure should look like this

1
2
3
4
5
- main.go
- ghome_say.py
- music
  - mp3_cache
    - voice.mp3

This is really funny. I’m thinking about removing the dust from my Raspberry Pi and program it warn me if the temperature of my home is too high or too low. why? Because I can.

Go Tool Trace

I’ve found two interesting post about this tool. post1 post2. This has been really interesting for me as I’ve been wondering for some time if there was any way of visualizing the program execution time.

As you my guess, the first thing I did is to run the Mandelbrot tests and visualize the output. This help me to prove that the reason why the performance is the same for the configuration 2x2x512 is that one of the areas is much more expensive to calculate.

Code could not finish, this are some reasons why this happen. - Plot name not defined. The first parameter of the shortcode is the name. - There is a syntax error. check browser console.

Adding some tracing to the code I got the exact timing for each section and this is how it looks like.

When Elapsed Goroutine ID Events
0.007260554s 568.048659ms Task 1 (complete)
0.007260554 . 8a task Calculate (id 1, parent 0) created
0.007466081 . 205527 39 region Area 0 started (duration: 289.046531ms)
0.007476658 . 10577 38 region Area 2 started (duration: 4.819706ms)
0.007532978 . 56320 37 region Area 1 started (duration: 563.917497ms)
0.012303297 . 4770319 38 region Area 3 started (duration: 148.341257ms)
0.575309213 .563005916 8 task end
- - - GC:74.168µs

So now it’s clear, The area with index 1 is the most expensive and takes 563ms to complete. This means that meanwhile one worker is doing this task, the other workers have plenty of time to finish the other areas.

This is just a brief summary of all the information the the tracer can provide. I’m thinking about doing a single post about this.

Python an Jupyter

Provably will be the next #howto post. stay tunned!

References

blogweekendgo

Add Plots With Hugo Shortcodes - Plotly

Avoid Github Government Block