LoginSignup
0
0

More than 5 years have passed since last update.

raspberry pi 1でtensrorflow lite その24

Posted at

概要

raspberry pi 1でtensorflow liteやってみた。
マイク入力を扱うので、alsaを叩いてみた。
ラズパイで、リアルタイム処理してみた。
波形を表示してみた。

写真

2018-12-23-094651_1440x900_scrot.png

サンプルコード

#include <gtkmm/drawingarea.h>
#include <ctime>
#include <cmath>
#include <cairomm/context.h>
#include <glibmm/main.h>
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <stdio.h>
#include <alsa/asoundlib.h>

#define IN_FREQ             44100

class Clock: public Gtk::DrawingArea
{
public:
    Clock();
    virtual ~Clock();
protected:
    bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr) override;
    bool on_timeout();
    double m_radius;
    double m_line_width;
    int err;
    int i;
    snd_pcm_t * in_handle;
    snd_pcm_sframes_t frames;
    char * device = "default";
    short in_buffer[IN_FREQ * 5];
};
Clock::Clock(): m_radius(0.42), m_line_width(0.05)
{
    Glib::signal_timeout().connect(sigc::mem_fun(* this, &Clock::on_timeout), 1000);
    if ((err = snd_pcm_open(&in_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0)
    {
        fprintf (stderr, "Capture open error: %s\n", snd_strerror(err));
    }
    if ((err = snd_pcm_set_params(in_handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, IN_FREQ, 1, 1000 * 1000)) < 0)
    {
        fprintf (stderr, "Capture open error: %s\n", snd_strerror(err));
    }
}
Clock::~Clock()
{
    snd_pcm_close(in_handle);
}
bool Clock::on_draw(const Cairo::RefPtr<Cairo::Context> &cr)
{
    Gtk::Allocation allocation = get_allocation();
    const int width = allocation.get_width();
    const int height = allocation.get_height();
    cr->set_line_width(1);
    int w;
    for (int i = 0; i < 10; i++)
    {

    }
    frames = snd_pcm_readi(in_handle, in_buffer, IN_FREQ);
    if (frames < 0)
    {
        frames = snd_pcm_recover(in_handle, frames, 0);
    }
    cr->move_to(0, 200);
    for (i = 0; i < 44000; i += 60)
    {
        w = (int) (in_buffer[i] / 256.) + 128.;
        cr->line_to(i / 60, w);     
    }
    cr->stroke();
    return true;
}
bool Clock::on_timeout()
{
    auto win = get_window();
    if (win)
    {
        Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
        win->invalidate_rect(r, false);
    }
    return true;
}
int main(int argc, char ** argv)
{
    auto app = Gtk::Application::create(argc, argv, "com.ohisamallc");
    Gtk::Window win;
    win.set_title("alsa");
    win.set_default_size(800, 300);
    Clock c;
    win.add(c);
    c.show();
    return app->run(win);
}



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0