audio_test.cpp

来自「一个语言识别引擎」· C++ 代码 · 共 73 行

CPP
73
字号
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include <stdio.h>
#include <math.h>

#include <yarp/os/all.h>

#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/AudioGrabberInterfaces.h>

using namespace yarp::os;
using namespace yarp::sig;
using namespace yarp::dev;

int main(int argc, char *argv[]) {
  Network::init();

  // Get an audio device.

  Property p;
  if (argc>1) {
    p.fromCommand(argc,argv);
  } else {
    p.fromString("(device portaudio)");
  }
  PolyDriver poly(p);
  if (!poly.isValid()) {
    printf("cannot open driver\n");
    return 1;
  }


  // Make sure we can both read and write sound

  IAudioGrabberSound *get;
  IAudioRender *put;
  poly.view(get);
  poly.view(put);
  if (get==NULL&&put==NULL) {
    printf("cannot open interface\n");
    return 1;
  }


  // echo from microphone to headphones, superimposing an annoying tone

  double vv = 0;
  while (true) {
    Sound s;
    get->getSound(s);
    for (int i=0; i<s.getSamples(); i++) {
      double now = Time::now();
      static double first = now;
      now -= first;
      if ((long int) (now*2) % 2 == 0) {
	vv += 0.08;
      } else {
	vv += 0.04;
      }
      double dv = 500*sin(vv);
      for (int j=0; j<s.getChannels(); j++) {
	int v = s.get(i,j);
	s.set((int)(v+dv+0.5),i,j);
      }
    }
    put->renderSound(s);
  }

  Network::fini();

  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?