示例#1
0
文件: sndio.c 项目: FOSSRIT/baresip
static void auplay_destructor(void *arg)
{
	struct auplay_st *st = arg;

	if (st->run) {
		st->run = false;
		(void)pthread_join(st->thread, NULL);
	}

	if (st->hdl)
		sio_close(st->hdl);

	mem_deref(st->sampv);
	mem_deref(st->ap);
}
示例#2
0
static void ao_sndio_close(ao_driver_t *this_gen)
{
  sndio_driver_t *this = (sndio_driver_t *) this_gen;

  xprintf (this->xine, XINE_VERBOSITY_DEBUG,
           "audio_sndio_out: ao_sndio_close called\n");

  if (!sio_stop(this->hdl)) {
    xprintf (this->xine, XINE_VERBOSITY_DEBUG,
             "audio_sndio_out: ao_sndio_close could not stop\n");
  }

  sio_close(this->hdl);
  this->hdl = NULL;
}
示例#3
0
static void close_module(input_module_t *mod)
{
    if(mod)
    {
        if(mod->internal)
        {
            im_sndio_state *s = mod->internal;
            if(s->hdl != NULL)
                sio_close(s->hdl);
            thread_mutex_destroy(&s->metadatalock);
            free(s);
        }
        free(mod);
    }
}
static int
init (struct xmp_context *ctx)
{
	 struct sio_par par, askpar;
	 struct xmp_options *opt = &ctx->o;

	 hdl = sio_open (NULL, SIO_PLAY, 0);
	 if (hdl == NULL) {
		 fprintf (stderr, "%s: failed to open audio device\n",
			 __func__);
		 return XMP_ERR_DINIT;
	 }

	 sio_initpar (&par);
	 par.pchan = opt->outfmt & XMP_FMT_MONO ? 1 : 2;
	 par.rate = opt->freq;
	 par.bits = opt->resol;
	 par.sig = opt->resol > 8 ? 1 : 0;
	 par.le = SIO_LE_NATIVE;
	 par.appbufsz = par.rate / 4;

	 askpar = par;
	 if (!sio_setpar (hdl, &par) || !sio_getpar (hdl, &par)) {
		 fprintf (stderr, "%s: failed to set parameters\n", __func__);
		 goto error;
	 }

	 if ((par.bits == 16 && par.le != askpar.le) ||
	     par.bits != askpar.bits ||
	     par.sig != askpar.sig ||
	     par.pchan != askpar.pchan ||
	     ((par.rate * 1000 < askpar.rate * 995) ||
	      (par.rate * 1000 > askpar.rate * 1005))) {
		 fprintf (stderr, "%s: parameters not supported\n", __func__);
		 goto error;
	 }

	 if (!sio_start (hdl)) {
		 fprintf (stderr, "%s: failed to start audio device\n",
			 __func__);
		 goto error;
	 }
	 return xmp_smix_on (ctx);

error:
	 sio_close (hdl);
	 return XMP_ERR_DINIT;
}
示例#5
0
static void
sndio_release(void *dp)
{
	sndio_in_driver *d = dp;

	d->read_trun = 0;
	pthread_join(d->read_tid, NULL);

	if (d->sndbuf)
		free(d->sndbuf);
	d->sndbuf = NULL;

	if (d->hdl)
		sio_close(d->hdl);
	d->hdl = NULL;
}
示例#6
0
static void
sndio_exit(int input)
{
	if (input)
		return;

	_unix_bg_man->unregister_func(sndio_update);

	_AL_FREE(sndio_play_bufdata);
	sndio_play_bufdata = 0;

	_mixer_exit();

	if (hdl != NULL)
		sio_close(hdl);
	hdl = NULL;
}
示例#7
0
/*
=======================================
    RS232 接收线程
=======================================
*/
CR_API uint_t STDCALL
qst_rs232_main (
  __CR_IN__ void_t* parm
    )
{
    sQstComm*   ctx = (sQstComm*)parm;

    /* 工作循环 */
    while (!ctx->comm.quit)
    {
        uint_t  back;
        ansi_t  cha, *data;

        /* 一个个字节读 */
        back = sio_read(ctx->comm.obj.port, &cha, 1);
        if (back == 1) {
            if (CR_VCALL(ctx->bufs)->putb_no(ctx->bufs, cha))
                ctx->size += 1;
            continue;
        }
        if (back != 0) {
            thread_sleep(20);
            continue;
        }

        /* 文本模式处理 */
        if (ctx->size == 0)
            continue;
        data = (ansi_t*)(CR_VCALL(ctx->bufs)->flush(ctx->bufs));
        if (ctx->comm.text)
            ctx->size = qst_txt_mode(data, ctx->size);

        /* 渲染读到的内容 */
        _ENTER_COM_SINGLE_
        ctx->comm.render(parm, data, ctx->size);
        _LEAVE_COM_SINGLE_

        /* 缓存指针复位 */
        CR_VCALL(ctx->bufs)->reput(ctx->bufs, 0);
        ctx->size = 0;
    }

    /* 退出时关闭串口 */
    sio_close(ctx->comm.obj.port);
    return (QST_OKAY);
}
示例#8
0
static int
sndio_detect(int input)
{
	if (input) {
		if (digi_driver != digi_input_driver) {
			ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE,
			    get_config_text("sndio output driver must be installed before input can be read"));
			return FALSE;
		}
		return TRUE;
	}

	if (open_sndio_device(0) != 0)
		return FALSE;

	sio_close(hdl);
	return TRUE;
}
示例#9
0
文件: Gyro.cpp 项目: WangRobo/S2BOT
/***************************************************************
【函数功能】: 关闭串口
【输    入】: 无
【输    出】: 执行结果:0正确
【说    明】: 
***************************************************************/
int CGyro::Close()
{
	if (m_bPortOpened)
	{
		if (m_bOutEnabled)
		{
			Enable(false);
			m_bOutEnabled = false;
		}
		sio_flush(m_iPort,0);	// 仅清空Input缓冲区
		sio_close(m_iPort);		// 等待发送缓冲区为空后close

	}
	m_bPortOpened = false;
	//m_iErrCnt = 0;
	cout<<"[-] Gyro: Stoped!"<<endl;
	return 0;
}
示例#10
0
void
audio_term()
{

 /* Close audio system  */
 if (hdl)
  {
   sio_close(hdl);
   hdl = NULL;
  }

 /* Finish linear file */
 if (linear_fd >= 0)
  {
   ftruncate(linear_fd, lseek(linear_fd, 0L, SEEK_CUR));
   close(linear_fd);
   linear_fd = -1;
  }
}
示例#11
0
static PaError
CloseStream(PaStream *stream)
{
	PaSndioStream *s = (PaSndioStream *)stream;

	DPR("CloseStream:\n");

	if (!s->stopped)
		StopStream(stream);

	if (s->mode & SIO_REC)
		free(s->rbuf);
	if (s->mode & SIO_PLAY)
		free(s->wbuf);
	sio_close(s->hdl);
        PaUtil_TerminateStreamRepresentation(&s->base);
	PaUtil_TerminateBufferProcessor(&s->bufproc);
	PaUtil_FreeMemory(s);
	return paNoError;
}
void SndioAudioDriver::disconnect()
{
	INFOLOG("SndioAudioDriver::disconnect");

	sndioAudioDriver_running = false;

	pthread_join(sndioAudioDriverThread, NULL);

	if (hdl != NULL) {
		sio_close(hdl);
		hdl = NULL;
	}

	delete [] out_L;
	out_L = NULL;

	delete [] out_R;
	out_R = NULL;

	delete[] audioBuffer;
	audioBuffer = NULL;
}
示例#13
0
文件: cap.c 项目: SylvestreG/bitrig
int
main(int argc, char **argv) {
	int ch;
	unsigned mode = SIO_PLAY | SIO_REC;
	struct sio_hdl *hdl;
	
	while ((ch = getopt(argc, argv, "pr")) != -1) {
		switch(ch) {
		case 'p':
			mode &= ~SIO_REC;
			break;
		case 'r':
			mode &= ~SIO_PLAY;
			break;
		default:
			usage();
			exit(1);
			break;
		}
	}
	if (mode == 0) {
		fprintf(stderr, "-p and -r flags are mutualy exclusive\n");
		exit(1);
	}
	hdl = sio_open(NULL, mode, 0);
	if (hdl == NULL) {
		fprintf(stderr, "sio_open() failed\n");
		exit(1);
	}
	if (!sio_getcap(hdl, &cap)) {
		fprintf(stderr, "sio_setcap() failed\n");
		exit(1);
	}
	cap_pr(&cap);
	sio_close(hdl);
	return 0;
}
示例#14
0
/*
 * delete_fluid_sndio_audio_driver
 */
void
delete_fluid_sndio_audio_driver(fluid_audio_driver_t* p)
{
  fluid_sndio_audio_driver_t* dev = (fluid_sndio_audio_driver_t*) p;

  if (dev == NULL) {
    return;
  }
  dev->cont = 0;
  if (dev->thread) {
    if (pthread_join(dev->thread, NULL)) {
      FLUID_LOG(FLUID_ERR, "Failed to join the audio thread");
      return;
    }
  }
  if (dev->hdl) {
    sio_close(dev->hdl);
  }
  if (dev->buffer != NULL) {
    FLUID_FREE(dev->buffer);
  }
  FLUID_FREE(dev);
  return;
}
示例#15
0
文件: Sensor.cpp 项目: WangRobo/S2BOT
/***************************************************************
【函数功能】: 关闭串口
【输    入】: 无
【输    出】: 执行结果:0正确
【说    明】: 
***************************************************************/
int CSensor::Close()
{
	if (m_bPortOpened)
	{
		// 关闭定时器
//		EnableTimer(false);
		// 关闭发送线程
		if(m_hSendThreadHandle != INVALID_HANDLE_VALUE)
		{
			m_bSendThreadStopFlag = true;
			Sleep(1);
			WaitForSingleObject(m_hSendThreadHandle,INFINITE);
			//CloseHandle(m_hSendThreadHandle);
			m_hSendThreadHandle = INVALID_HANDLE_VALUE;
		}
		// 清空发送缓冲
		sio_flush(m_iPort,0);	// 仅清空Input缓冲区
		sio_close(m_iPort);		// 等待发送缓冲区为空后close
	}
	m_bPortOpened = false;
	cout<<"[-] Sensor: Stoped !"<<endl;
	// 复位错误统计变量值
	//m_sErr_USensor.err = RESULT_ERR_SENSOR_OK;
	//m_sErr_USensor.err_cnt = 0;
	//m_sErr_USensor.cmd = 0;
	//m_sErr_Voltage.err = RESULT_ERR_SENSOR_OK;
	//m_sErr_Voltage.err_cnt = 0;
	//m_sErr_Voltage.cmd = 0;
	//m_sErr_Current.err = RESULT_ERR_SENSOR_OK;
	//m_sErr_Current.err_cnt = 0;
	//m_sErr_Current.cmd = 0;
	//m_sErr_IOconfig.err = RESULT_ERR_SENSOR_OK;
	//m_sErr_IOconfig.err_cnt = 0;
	//m_sErr_IOconfig.cmd = 0;
	return 0;
}
示例#16
0
void
audioClose()
{
	if (hdl != NULL)
		sio_close(hdl);
}
示例#17
0
/*
---------------------------------------
    打开 RS232 通讯接口
---------------------------------------
*/
static bool_t
qst_com_rs232 (
  __CR_IN__ void_t*     parm,
  __CR_IN__ uint_t      argc,
  __CR_IN__ ansi_t**    argv
    )
{
    int32u  baud;
    uint_t  port, bits;
    uint_t  stop, parity;
    /* --------------- */
    const ansi_t*   sstop;
    const ansi_t*   sparity;

    /* 参数解析 <串口号> [波特率] [数据位] [校验位] [停止位] */
    if (argc < 2)
        return (FALSE);
    bits = 8;
    baud = 115200UL;
    stop = CR_SIO_STOP10;
    parity = CR_SIO_NOP;
    sstop = "1";
    sparity = "no";
    port = str2intxA(argv[1]);
    if (argc > 2) {
        baud = str2intx32A(argv[2]);
        if (argc > 3) {
            bits = str2intxA(argv[3]);
            if (argc > 4) {
                sparity = argv[4];
                if (str_cmpA(argv[4], "no") == 0)
                    parity = CR_SIO_NOP;
                else
                if (str_cmpA(argv[4], "odd") == 0)
                    parity = CR_SIO_ODD;
                else
                if (str_cmpA(argv[4], "even") == 0)
                    parity = CR_SIO_EVEN;
                else
                if (str_cmpA(argv[4], "mark") == 0)
                    parity = CR_SIO_MARK;
                else
                if (str_cmpA(argv[4], "space") == 0)
                    parity = CR_SIO_SPCE;
                else
                    sparity = "no";
                if (argc > 5) {
                    sstop = argv[5];
                    if (str_cmpA(argv[5], "1") == 0)
                        stop = CR_SIO_STOP10;
                    else
                    if (str_cmpA(argv[5], "1.5") == 0)
                        stop = CR_SIO_STOP15;
                    else
                    if (str_cmpA(argv[5], "2") == 0)
                        stop = CR_SIO_STOP20;
                    else
                        sstop = "1";
                }
            }
        }
    }

    sQstComm*   ctx = (sQstComm*)parm;

    /* 关闭当前接口并打开串口 */
    if (!sio_open(port))
        return (FALSE);
    sio_setup(port, baud, bits, parity, stop);
    sio_set_buffer(port, 1024, 1024);
    sio_set_rd_timeout(port, 0, 0, QCOM_CUTDOWN);
    sio_set_wr_timeout(port, 0, QCOM_SNDTOUT);
    sio_clear_error(port);
    sio_flush(port, CR_SIO_FLU_RT);

    /* 设置工作参数 */
    qst_com_close(parm, argc, argv);
    ctx->comm.obj.port = port;
    ctx->comm.send = qst_rs232_send;

    /* 启动接收线程 */
    ctx->comm.thrd = thread_new(0, qst_rs232_main, parm, FALSE);
    if (ctx->comm.thrd == NULL) {
        sio_close(port);
        return (FALSE);
    }
    TRY_FREE(ctx->comm.title);
    ctx->comm.title = str_fmtA(" - COM%u, %u, %u, %s, %s",
                            port, baud, bits, sparity, sstop);
    qst_update_title(ctx);
    return (TRUE);
}
示例#18
0
static int
sndio_stream_init(cubeb * context,
                  cubeb_stream ** stream,
                  char const * stream_name,
                  cubeb_devid input_device,
                  cubeb_stream_params * input_stream_params,
                  cubeb_devid output_device,
                  cubeb_stream_params * output_stream_params,
                  unsigned int latency_frames,
                  cubeb_data_callback data_callback,
                  cubeb_state_callback state_callback,
                  void *user_ptr)
{
  cubeb_stream *s;
  struct sio_par wpar, rpar;
  cubeb_sample_format format;
  int rate;
  size_t bps;

  DPR("sndio_stream_init(%s)\n", stream_name);

  s = malloc(sizeof(cubeb_stream));
  if (s == NULL)
    return CUBEB_ERROR;
  memset(s, 0, sizeof(cubeb_stream));
  s->mode = 0;
  if (input_stream_params) {
    if (input_stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK) {
      DPR("sndio_stream_init(), loopback not supported\n");
      goto err;
    }
    s->mode |= SIO_REC;
    format = input_stream_params->format;
    rate = input_stream_params->rate;
  }
  if (output_stream_params) {
    if (output_stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK) {
      DPR("sndio_stream_init(), loopback not supported\n");
      goto err;
    }
    s->mode |= SIO_PLAY;
    format = output_stream_params->format;
    rate = output_stream_params->rate;
  }
  if (s->mode == 0) {
    DPR("sndio_stream_init(), neither playing nor recording\n");
    goto err;
  }
  s->context = context;
  s->hdl = sio_open(NULL, s->mode, 1);
  if (s->hdl == NULL) {
    DPR("sndio_stream_init(), sio_open() failed\n");
    goto err;
  }
  sio_initpar(&wpar);
  wpar.sig = 1;
  wpar.bits = 16;
  switch (format) {
  case CUBEB_SAMPLE_S16LE:
    wpar.le = 1;
    break;
  case CUBEB_SAMPLE_S16BE:
    wpar.le = 0;
    break;
  case CUBEB_SAMPLE_FLOAT32NE:
    wpar.le = SIO_LE_NATIVE;
    break;
  default:
    DPR("sndio_stream_init() unsupported format\n");
    goto err;
  }
  wpar.rate = rate;
  if (s->mode & SIO_REC)
    wpar.rchan = input_stream_params->channels;
  if (s->mode & SIO_PLAY)
    wpar.pchan = output_stream_params->channels;
  wpar.appbufsz = latency_frames;
  if (!sio_setpar(s->hdl, &wpar) || !sio_getpar(s->hdl, &rpar)) {
    DPR("sndio_stream_init(), sio_setpar() failed\n");
    goto err;
  }
  if (rpar.bits != wpar.bits || rpar.le != wpar.le ||
      rpar.sig != wpar.sig || rpar.rate != wpar.rate ||
      ((s->mode & SIO_REC) && rpar.rchan != wpar.rchan) ||
      ((s->mode & SIO_PLAY) && rpar.pchan != wpar.pchan)) {
    DPR("sndio_stream_init() unsupported params\n");
    goto err;
  }
  sio_onmove(s->hdl, sndio_onmove, s);
  s->active = 0;
  s->nfr = rpar.round;
  s->rbpf = rpar.bps * rpar.rchan;
  s->pbpf = rpar.bps * rpar.pchan;
  s->rchan = rpar.rchan;
  s->pchan = rpar.pchan;
  s->nblks = rpar.bufsz / rpar.round;
  s->data_cb = data_callback;
  s->state_cb = state_callback;
  s->arg = user_ptr;
  s->mtx = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
  s->hwpos = s->swpos = 0;
  if (format == CUBEB_SAMPLE_FLOAT32LE) {
    s->conv = 1;
    bps = sizeof(float);
  } else {
    s->conv = 0;
    bps = rpar.bps;
  }
  if (s->mode & SIO_PLAY) {
    s->pbuf = malloc(bps * rpar.pchan * rpar.round);
    if (s->pbuf == NULL)
      goto err;
  }
  if (s->mode & SIO_REC) {
    s->rbuf = malloc(bps * rpar.rchan * rpar.round);
    if (s->rbuf == NULL)
      goto err;
  }
  *stream = s;
  DPR("sndio_stream_init() end, ok\n");
  (void)context;
  (void)stream_name;
  return CUBEB_OK;
err:
  if (s->hdl)
    sio_close(s->hdl);
  if (s->pbuf)
    free(s->pbuf);
  if (s->rbuf)
    free(s->pbuf);
  free(s);
  return CUBEB_ERROR;
}
示例#19
0
文件: vol.c 项目: SylvestreG/bitrig
int
main(int argc, char **argv) {
	int ch;
	int tty;
	struct sio_hdl *hdl;
	struct termios tio;
	struct pollfd pfd[2];
	char cmd;
	ssize_t n, len;
	
	/*
	 * defaults parameters
	 */
	sio_initpar(&par);
	par.sig = 1;
	par.bits = 16;
	par.pchan = 2;
	par.rate = 44100;

	if (isatty(STDIN_FILENO)) {
		fprintf(stderr, "stdin can't be a tty\n");
		exit(1);
	}
	tty = open("/dev/tty", O_RDWR);
	if (tty < 0) {
		perror("/dev/tty");
		exit(1);
	}
	if (tcgetattr(tty, &tio) < 0) {
		perror("tcsetattr");
		exit(1);
	}
	tio.c_lflag &= ~ICANON;
	tio.c_lflag &= ~ECHO;
	tio.c_cc[VMIN] = 1;
	tio.c_cc[VTIME] = 0;
	if (tcsetattr(tty, TCSAFLUSH, &tio) < 0) {
		perror("tcsetattr");
		exit(1);
	}

	while ((ch = getopt(argc, argv, "r:c:e:b:x:")) != -1) {
		switch(ch) {
		case 'r':
			if (sscanf(optarg, "%u", &par.rate) != 1) {
				fprintf(stderr, "%s: bad rate\n", optarg);
				exit(1);
			}
			break;
		case 'c':
			if (sscanf(optarg, "%u", &par.pchan) != 1) {
				fprintf(stderr, "%s: bad channels\n", optarg);
				exit(1);
			}
			break;
		case 'e':
			if (!sio_strtoenc(&par, optarg)) {
				fprintf(stderr, "%s: bad encoding\n", optarg);
				exit(1);
			}
			break;
		default:
			usage();
			exit(1);
			break;
		}
	}

	hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
	if (hdl == NULL) {
		fprintf(stderr, "sio_open() failed\n");
		exit(1);
	}
	if (!sio_setpar(hdl, &par)) {
		fprintf(stderr, "sio_setpar() failed\n");
		exit(1);
	}
	if (!sio_onvol(hdl, onvol, NULL))
		fprintf(stderr, "warning: no volume knob on this device\n");
	fprintf(stderr, "use ``+'' and ``-'' to adjust the volume\n");
	if (!sio_start(hdl)) {
		fprintf(stderr, "sio_start() failed\n");
		exit(1);
	}
	for (;;) {
		pfd[0].fd = tty;
		pfd[0].events = POLLIN;
		sio_pollfd(hdl, &pfd[1], POLLOUT);
		if (poll(pfd, 2, -1) < 0) {
			perror("poll");
			exit(1);
		}
		if (pfd[0].revents & POLLIN) {
			if (read(tty, &cmd, 1) < 0) {
				perror("read(tty)");
				exit(1);
			}
			switch (cmd) {
			case '+':
				if (vol < SIO_MAXVOL) {
					vol++;
					sio_setvol(hdl, vol);
				}
				break;
			case '-':
				if (vol > 0) {
					vol--;
					sio_setvol(hdl, vol);
				}
				break;
			}
		}
		if (sio_revents(hdl, &pfd[1]) & POLLOUT) {
			len = read(STDIN_FILENO, buf, BUFSZ);
			if (len < 0) {
				perror("stdin");
				exit(1);
			}
			if (len == 0)
				break;
			n = sio_write(hdl, buf, len);
			if (n == 0) {
				fprintf(stderr, "sio_write: failed\n");
				exit(1);
			}
		}
	}
	sio_close(hdl);
	return 0;
}
示例#20
0
void CMainFrame::Scan()
{    
	 CView *pView		= GetActiveView();		//获取当前VIEW视图
     CDC *pDC			= pView->GetDC();		//得到VIEW的DC
    
	 CString cs;
     cs.Format("%d",Scan_num+1);
     pDC->TextOut(0,Height/2,cs,cs.GetLength());
     pView->ReleaseDC(pDC);
     


     int i,k;
	 unsigned char MAX=0;
	 int MAX_i=0;
	 unsigned char seq[3];
	 float  peak;
	 float  distance;
	 float  Phi;
	 //float  Theta;
	 float Z;
	 float L1,L2,L3,L4;
	 float r;
	 uchar* ptr;

	 HVSTATUS status =STATUS_OK;
	/********采集单帧图像**********/
    unsigned char *ppbuffer[1];
	ppbuffer[0]=m_pRawBuffer;
   	

	status =HVSnapShot(m_hhv,ppbuffer,1);

    HV_VERIFY(status);

	/********显示当前帧************/
	OnSnapChange(NULL,NULL);

    
	//生成支持OPENCV的IPLIMAGE数据结构,并使用相机采集的图像数据初始化//
    CvSize cvSize;
	cvSize.width = Width;
 	cvSize.height = Height;
 	
	
	IplImage *iplImage = cvCreateImageHeader(cvSize,IPL_DEPTH_8U,3);
 	cvSetData(iplImage,m_pImageBuffer,Width*3);
 	
	//申请灰度空间,将BGR格式转化为灰度数据
 	IplImage *iplgray = cvCreateImage(cvGetSize(iplImage),IPL_DEPTH_8U,1);

 	cvCvtColor(iplImage,iplgray,CV_BGR2GRAY);
    
	for(k=0;k<Height;k++)
	{
		ptr=(uchar*)(iplgray->imageData+k*iplgray->widthStep);

        for(i=0;i<Width;i++)


     	for(i=0;i<Width;i++)
		{
	    	if( ptr[i]>=MAX)
			{
			  MAX=ptr[i];
			  MAX_i=i;
			}
		}
        
		if(MAX<=50)
		{
           Scan_result[Scan_num][k].X=0;
           Scan_result[Scan_num][k].Y=0;
		   Scan_result[Scan_num][k].Z=0;
		   continue;
		}

		if(MAX_i<=1)
		   seq[2]=0;
		else 
		  seq[2]=ptr[MAX_i-1];

		seq[1]=ptr[MAX_i];
		
		if(MAX_i>=Width)
		  seq[0]=0;
		else
		  seq[0]=ptr[MAX_i+1];


		//peak=1/2*(log(seq[0])-log(seq[2]))/(log(seq[0])-2*log(seq[1])+log(seq[2]))+Width-MAX_i;
              peak=(seq[2]-seq[0])/(seq[0]+seq[1]+seq[2])+Width-MAX_i;
           //peak=(2*ptr[MAX_i-2]+ptr[MAX_i-1]-ptr[MAX_i+1]-2*ptr[MAX_i+2])/(ptr[MAX_i+2]+ptr[MAX_i+1]+ptr[MAX_i]+ptr[MAX_i-1]+ptr[MAX_i-2])+Width-MAX_i;
 

        
        /************************计算某行扫描结果*****************************************/

        distance=FS/(PIX_SIZE*peak+ PIX_OFF)+DIST_OFF; 
		if(distance>1800)
		{
           Scan_result[Scan_num][k].X=0;
           Scan_result[Scan_num][k].Y=0;
		   Scan_result[Scan_num][k].Z=0;
		   continue;
		}
		//Theta=atan((k-Height/2)*PIX_SIZE/f);
        Z=(distance-DIST_OFF)*(Height/2-k)*PIX_SIZE/f;
        L1=sqrt(distance*distance+Z*Z);
        

		L3=  distance*tan(PI/2- LaserAngle);
		L2=  sqrt(L1*L1+L3*L3);
		L4=  sqrt(L3*L3+distance*distance);

		r=   sqrt( (L3-rotation_r)*(L3-rotation_r)+distance*distance);

        Phi=Scan_num*Scan_step+PI/2-acos( (rotation_r*rotation_r+r*r-L4*L4)/2.0f/rotation_r/r);


		Scan_result[Scan_num][k].X=r*cos(Phi);
        Scan_result[Scan_num][k].Y=r*sin(Phi);
		Scan_result[Scan_num][k].Z=Z;
		/****************************************************************************/
	
	}
 


	cvReleaseImage(&iplgray);
  /************如果扫描完成,关闭串口,并输出数据********/
	if(Scan_num==Scan_total-1)
	{  
	   MessageBox("扫描完成");
	   Scan_num=0;
	   OnBtnSend('2');
       //OnBtnSend('2');
	   sio_close(Port); //关闭串口
	   /*************数据输出**************/
	   char* fileName=dataout;
	   CString strtemp;

       CFile file;
	   CFileException fileException;
	   if(!file.Open(fileName,CFile::modeCreate | CFile::modeWrite, &fileException))
	   {
		   CString errorInfo;
		   errorInfo.Format("不能打开文件%s,错误:%u\n",fileName,fileException.m_cause);
		   MessageBox(errorInfo,"错误",MB_OK|MB_ICONERROR);
		   return;
	   }

	   for(i=0;i<Scan_total;i++)
		   for(k=0;k<Height;k++)
		   {
			   if(Scan_result[i][k].X==0 & Scan_result[i][k].Y==0 & Scan_result[i][k].Z==0)
                  continue;
			   else
			   { strtemp.Format("%.3f, %.3f, %.3f\r\n\r\n",Scan_result[i][k].X,Scan_result[i][k].Y,Scan_result[i][k].Z);
				   file.Write(strtemp,strtemp.GetLength());
			   }
				  
		   }
	    file.Close();
		MessageBox("已成功输出数据","信息",MB_OK|MB_ICONINFORMATION);  

	   return;
	}
  /**********扫描未完成,发送串口指令***********/
    else
    { 
		OnBtnSend('1');
		//OnBtnSend('1');
		Scan_num=Scan_num+1;
	
    }  
 
}
示例#21
0
static int
sndio_stream_init(cubeb * context,
                  cubeb_stream ** stream,
                  char const * stream_name,
                  cubeb_devid input_device,
                  cubeb_stream_params * input_stream_params,
                  cubeb_devid output_device,
                  cubeb_stream_params * output_stream_params,
                  unsigned int latency_frames,
                  cubeb_data_callback data_callback,
                  cubeb_state_callback state_callback,
                  void *user_ptr)
{
  cubeb_stream *s;
  struct sio_par wpar, rpar;
  DPR("sndio_stream_init(%s)\n", stream_name);
  size_t size;

  assert(!input_stream_params && "not supported.");
  if (input_device || output_device) {
    /* Device selection not yet implemented. */
    return CUBEB_ERROR_DEVICE_UNAVAILABLE;
  }

  s = malloc(sizeof(cubeb_stream));
  if (s == NULL)
    return CUBEB_ERROR;
  s->context = context;
  s->hdl = sio_open(NULL, SIO_PLAY, 1);
  if (s->hdl == NULL) {
    free(s);
    DPR("sndio_stream_init(), sio_open() failed\n");
    return CUBEB_ERROR;
  }
  sio_initpar(&wpar);
  wpar.sig = 1;
  wpar.bits = 16;
  switch (output_stream_params->format) {
  case CUBEB_SAMPLE_S16LE:
    wpar.le = 1;
    break;
  case CUBEB_SAMPLE_S16BE:
    wpar.le = 0;
    break;
  case CUBEB_SAMPLE_FLOAT32NE:
    wpar.le = SIO_LE_NATIVE;
    break;
  default:
    DPR("sndio_stream_init() unsupported format\n");
    return CUBEB_ERROR_INVALID_FORMAT;
  }
  wpar.rate = output_stream_params->rate;
  wpar.pchan = output_stream_params->channels;
  wpar.appbufsz = latency_frames;
  if (!sio_setpar(s->hdl, &wpar) || !sio_getpar(s->hdl, &rpar)) {
    sio_close(s->hdl);
    free(s);
    DPR("sndio_stream_init(), sio_setpar() failed\n");
    return CUBEB_ERROR;
  }
  if (rpar.bits != wpar.bits || rpar.le != wpar.le ||
      rpar.sig != wpar.sig || rpar.rate != wpar.rate ||
      rpar.pchan != wpar.pchan) {
    sio_close(s->hdl);
    free(s);
    DPR("sndio_stream_init() unsupported params\n");
    return CUBEB_ERROR_INVALID_FORMAT;
  }
  sio_onmove(s->hdl, sndio_onmove, s);
  s->active = 0;
  s->nfr = rpar.round;
  s->bpf = rpar.bps * rpar.pchan;
  s->pchan = rpar.pchan;
  s->data_cb = data_callback;
  s->state_cb = state_callback;
  s->arg = user_ptr;
  s->mtx = PTHREAD_MUTEX_INITIALIZER;
  s->rdpos = s->wrpos = 0;
  if (output_stream_params->format == CUBEB_SAMPLE_FLOAT32LE) {
    s->conv = 1;
    size = rpar.round * rpar.pchan * sizeof(float);
  } else {
    s->conv = 0;
    size = rpar.round * rpar.pchan * rpar.bps;
  }
  s->buf = malloc(size);
  if (s->buf == NULL) {
    sio_close(s->hdl);
    free(s);
    return CUBEB_ERROR;
  }
  *stream = s;
  DPR("sndio_stream_init() end, ok\n");
  (void)context;
  (void)stream_name;
  return CUBEB_OK;
}
示例#22
0
文件: rec.c 项目: SylvestreG/bitrig
int
main(int argc, char **argv) {
	int ch;
	struct sio_hdl *hdl;
	ssize_t n;
	
	/*
	 * defaults parameters
	 */
	sio_initpar(&par);
	par.sig = 1;
	par.bits = 16;
	par.rchan = 2;
	par.rate = 44100;

	while ((ch = getopt(argc, argv, "r:c:e:b:x:")) != -1) {
		switch(ch) {
		case 'r':
			if (sscanf(optarg, "%u", &par.rate) != 1) {
				fprintf(stderr, "%s: bad rate\n", optarg);
				exit(1);
			}
			break;
		case 'c':
			if (sscanf(optarg, "%u", &par.rchan) != 1) {
				fprintf(stderr, "%s: channels number\n", optarg);
				exit(1);
			}
			break;
		case 'e':
			if (!sio_strtoenc(&par, optarg)) {
				fprintf(stderr, "%s: unknown encoding\n", optarg);
				exit(1);
			}
			break;
		case 'x':
			for (par.xrun = 0;; par.xrun++) {
				if (par.xrun == sizeof(xstr) / sizeof(char *)) {
					fprintf(stderr, 
					    "%s: bad xrun mode\n", optarg);
					exit(1);
				}
				if (strcmp(xstr[par.xrun], optarg) == 0)
					break;
			}
			break;			
		default:
			usage();
			exit(1);
			break;
		}
	}

	hdl = sio_open(SIO_DEVANY, SIO_REC, 0);
	if (hdl == NULL) {
		fprintf(stderr, "sio_open() failed\n");
		exit(1);
	}
	sio_onmove(hdl, cb, NULL);
	if (!sio_setpar(hdl, &par)) {
		fprintf(stderr, "sio_setpar() failed\n");
		exit(1);
	}
	if (!sio_getpar(hdl, &par)) {
		fprintf(stderr, "sio_getpar() failed\n");
		exit(1);
	}
	if (!sio_start(hdl)) {
		fprintf(stderr, "sio_start() failed\n");
		exit(1);
	}
	for (;;) {
		n = sio_read(hdl, buf, BUFSZ);
		if (n == 0) {
			fprintf(stderr, "sio_write: failed\n");
			exit(1);
		}
		rlat -= n / (int)(par.bps * par.rchan);
		if (write(STDOUT_FILENO, buf, n) < 0) {
			perror("stdout");
			exit(1);
		}
	}
	sio_close(hdl);
	return 0;
}
示例#23
0
/*
 * Open the audio device for writing to.
 */
static int ao_sndio_open(ao_driver_t *this_gen,
                         uint32_t bits, uint32_t rate, int mode)
{
  sndio_driver_t *this = (sndio_driver_t *) this_gen;
  struct sio_par par;

  xprintf (this->xine, XINE_VERBOSITY_DEBUG,
           "audio_sndio_out: ao_sndio_open bits=%d rate=%d, mode=%d\n",
           bits, rate, mode);

  if (this->hdl != NULL) {
    sio_close (this->hdl);
    this->hdl = NULL;
  }

  this->hdl = sio_open(NULL, SIO_PLAY, 0);
  if (this->hdl == NULL)
    goto bad;

  sio_initpar(&par);

  switch (mode) {
  case AO_CAP_MODE_MONO:
    par.pchan = 1;
    break;
  case AO_CAP_MODE_STEREO:
    par.pchan = 2;
    break;
#if 0
  case AO_CAP_MODE_4CHANNEL:
    par.pchan = 4;
    break;
  case AO_CAP_MODE_4_1CHANNEL:
  case AO_CAP_MODE_5CHANNEL:
  case AO_CAP_MODE_5_1CHANNEL:
    par.pchan = 6;
    break;
#endif
  default:
    xprintf (this->xine, XINE_VERBOSITY_DEBUG,
             "audio_sndio_out: ao_sndio_open does not support the requested mode: 0x%X\n",
	     mode);
    goto bad;
  }

  switch (bits) {
  case 8:
    par.bits = 8;
    par.sig = 0;
    break;
  case 16:
    par.bits = 16;
    par.sig = 1;
    break;
  default:
    xprintf (this->xine, XINE_VERBOSITY_DEBUG,
             "audio_sndio_out: ao_sndio_open bits per sample not supported: %d\n", bits);
    goto bad;
  }

  par.rate = rate;
  par.appbufsz = par.rate * 250 / 1000; /* 250ms buffer */

  if (!sio_setpar(this->hdl, &par)) {
    xprintf (this->xine, XINE_VERBOSITY_DEBUG,
             "audio_sndio_out: ao_sndio_open could not set params\n");
    goto bad;
  }

  if (!sio_getpar(this->hdl, &par)) {
    xprintf (this->xine, XINE_VERBOSITY_DEBUG,
             "audio_sndio_out: ao_sndio_open could not get params\n");
    goto bad;
  }

  xprintf (this->xine, XINE_VERBOSITY_DEBUG,
           "audio_sndio_out: ao_sndio_open %d channels output\n",
           par.pchan);

  this->num_channels           = par.pchan;
  this->bytes_per_frame        = par.bps * par.pchan;
  this->playpos                = 0;
  this->realpos                = 0;
  sio_onmove(this->hdl, ao_sndio_onmove_cb, this);

  if (!sio_start(this->hdl)) {
    xprintf (this->xine, XINE_VERBOSITY_DEBUG,
             "audio_sndio_out: ao_sndio_open could not start\n");
    goto bad;
  }

  return par.rate;

bad:
  if (this->hdl != NULL)
    sio_close(this->hdl);
  return 0;
}
示例#24
0
void CAESinkSNDIO::EnumerateDevicesEx(AEDeviceInfoList &list, bool force)
{
  struct sio_hdl *hdl;
  struct sio_cap cap;

  if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == nullptr)
  {
    CLog::Log(LOGERROR, "CAESinkSNDIO::EnumerateDevicesEx - sio_open");
    return;
  }

  if (!sio_getcap(hdl, &cap))
  {
    CLog::Log(LOGERROR, "CAESinkSNDIO::EnumerateDevicesEx - sio_getcap");
    return;
  }

  sio_close(hdl);
  hdl = nullptr;

  for (unsigned int i = 0; i < cap.nconf; i++)
  {
    CAEDeviceInfo info;
    sio_cap::sio_conf conf = cap.confs[i];

    info.m_deviceName = SIO_DEVANY;
    info.m_displayName = "sndio";
    info.m_displayNameExtra = "#" + std::to_string(i);
    info.m_deviceType = AE_DEVTYPE_PCM;
    info.m_wantsIECPassthrough = false;

    unsigned int maxchan = 0;
    for (unsigned int j = 0; j < SIO_NCHAN; j++)
    {
      if (conf.pchan & (1 << j))
        maxchan = MAX(maxchan, cap.pchan[j]);
    }

    maxchan = MIN(maxchan, nitems(channelMap));
    for (unsigned int j = 0; j < maxchan; j++)
      info.m_channels += channelMap[j];

    for (unsigned int j = 0; j < SIO_NRATE; j++)
    {
      if (conf.rate & (1 << j))
      {
        info.m_sampleRates.push_back(cap.rate[j]);
      }
    }

    for (unsigned int j = 0; j < SIO_NENC; j++)
    {
      if (conf.enc & (1 << j))
      {
        AEDataFormat format = lookupDataFormat(cap.enc[j].bits, cap.enc[j].bps, cap.enc[j].sig, cap.enc[j].le, cap.enc[j].msb);
        if (format != AE_FMT_INVALID)
          info.m_dataFormats.push_back(format);
      }
    }

    list.push_back(info);
  }
}
示例#25
0
gboolean
gst_sndio_open (struct gstsndio *sio, gint mode)
{
  GValue list = G_VALUE_INIT, item = G_VALUE_INIT;
  GstStructure *s;
  GstCaps *caps;
  struct sio_enc *enc;
  struct sio_cap cap;
  char fmt[16];
  int i, chan;

  GST_DEBUG_OBJECT (sio->obj, "open");

  sio->hdl = sio_open (sio->device, mode, 0);
  if (sio->hdl == NULL) {
    GST_ELEMENT_ERROR (sio->obj, RESOURCE, OPEN_READ_WRITE,
	("Couldn't open sndio device"), (NULL));
    return FALSE;
  }
  sio->mode = mode;

  if (!sio_getcap(sio->hdl, &cap)) {
    GST_ELEMENT_ERROR (sio, RESOURCE, OPEN_WRITE,
	("Couldn't get device capabilities"), (NULL));
    sio_close(sio->hdl);
    sio->hdl = NULL;
    return FALSE;
  }
  if (cap.nconf == 0) {
    GST_ELEMENT_ERROR (sio, RESOURCE, OPEN_WRITE,
	("Device has empty capabilities"), (NULL));
    sio_close(sio->hdl);
    sio->hdl = NULL;
    return FALSE;
  }

  caps = gst_caps_new_empty ();
  s = gst_structure_new ("audio/x-raw", (char *)NULL, (void *)NULL);

  /*
   * scan supported rates
   */
  g_value_init (&list, GST_TYPE_LIST);
  g_value_init (&item, G_TYPE_INT);
  for (i = 0; i < SIO_NRATE; i++) {
      if ((cap.confs[0].rate & (1 << i)) == 0)
	  continue;
      g_value_set_int(&item, cap.rate[i]);
      gst_value_list_append_value (&list, &item);
  }
  gst_structure_set_value (s, "rate", &list);
  g_value_unset (&item);
  g_value_unset (&list);

  /*
   * scan supported channels
   */
  g_value_init (&list, GST_TYPE_LIST);
  g_value_init (&item, G_TYPE_INT);
  chan = (mode == SIO_PLAY) ? cap.confs[0].pchan : cap.confs[0].rchan;
  for (i = 0; i < SIO_NCHAN; i++) {
      if ((chan & (1 << i)) == 0)
	  continue;
      g_value_set_int(&item, (mode == SIO_PLAY) ? cap.pchan[i] : cap.rchan[i]);
      gst_value_list_append_value (&list, &item);
  }
  gst_structure_set_value (s, "channels", &list);
  g_value_unset (&item);
  g_value_unset (&list);

  /*
   * scan supported encodings
   */
  g_value_init (&list, GST_TYPE_LIST);
  g_value_init (&item, G_TYPE_STRING);
  for (i = 0; i < SIO_NENC; i++) {
      if ((cap.confs[0].enc & (1 << i)) == 0)
	  continue;
      enc = cap.enc + i;
      if (enc->bits % 8 != 0)
	  continue;
      if (enc->bits < enc->bps * 8 && enc->msb)
	  continue;
      if (enc->bits == enc->bps * 8) {
	  snprintf(fmt, sizeof(fmt), "%s%u%s",
		   enc->sig ? "S" : "U",
		   enc->bits,
		   enc->bps > 1 ? (enc->le ? "LE" : "BE") : "");
      } else {
	  snprintf(fmt, sizeof(fmt), "%s%u_%u%s",
		   enc->sig ? "S" : "U",
		   enc->bits,
		   enc->bps * 8,
		   enc->bps > 1 ? (enc->le ? "LE" : "BE") : "");
      }
      g_value_set_string(&item, fmt);
      gst_value_list_append_value (&list, &item);
  }
  gst_structure_set_value (s, "format", &list);
  g_value_unset (&item);
  g_value_unset (&list);

  /*
   * add the only supported layout: interleaved
   */
  g_value_init (&item, G_TYPE_STRING);
  g_value_set_string(&item, "interleaved");
  gst_structure_set_value (s, "layout", &item);
  g_value_unset (&item);

  gst_caps_append_structure (caps, s);
  sio->cur_caps = caps;
  fprintf(stderr, "caps are %s\n", gst_caps_to_string(caps));
  return TRUE;
}
示例#26
0
static int
sndio_init(int input, int voices)
{
	char tmp1[128], tmp2[128];

	if (input) {
		digi_driver->rec_cap_bits = 16;
		digi_driver->rec_cap_stereo = TRUE;
		return 0;
	}

	if (open_sndio_device(0) != 0)
		return -1;

	sndio_play_bufdata = _AL_MALLOC_ATOMIC(sndio_play_bufsize);
	if (sndio_play_bufdata == 0) {
		ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE,
		    get_config_text("Can not allocate audio buffer"));
		sio_close(hdl);
		return -1;
	}

	sndio_realpos = sndio_playpos = 0;
	sio_onmove(hdl, movecb, NULL);

	sndio_volume = 127;
	sio_onvol(hdl, volcb, NULL);

	if (!sio_start(hdl)) {
		ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE,
		    get_config_text("Can not start sndio"));
		sio_close(hdl);
		return -1;
	}

	digi_sndio.voices = voices;

	/* first arg is total number of samples */
	if (_mixer_init(sndio_play_round * (_sound_stereo ? 2 : 1),
	    _sound_freq, _sound_stereo, ((_sound_bits == 16) ? 1 : 0),
	    &digi_sndio.voices) != 0) {
		ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE,
		    get_config_text("Can not init software mixer"));
		sio_close(hdl);
		return -1;
	}

	_mix_some_samples((uintptr_t) sndio_play_bufdata, 0, sndio_signed);

	/* Add audio interrupt.  */
	_unix_bg_man->register_func(sndio_update);

	uszprintf(sndio_desc, sizeof(sndio_desc),
	    get_config_text("%s: %d bits, %s, %d Hz, %s"),
	    "sndio device",
	    _sound_bits,
	    uconvert_ascii((sndio_signed ? "signed" : "unsigned"), tmp1),
	    _sound_freq,
	    uconvert_ascii((par.pchan == 2 ? "stereo" : "mono"), tmp2));

	digi_driver->desc = sndio_desc;

	return 0;
}
示例#27
0
文件: siofile.c 项目: UNGLinux/Obase
/*
 * Open the device.
 */
struct siofile *
siofile_new(struct fileops *ops, char *path, unsigned int *rmode,
    struct aparams *ipar, struct aparams *opar,
    unsigned int *bufsz, unsigned int *round)
{
	struct sio_par par;
	struct sio_hdl *hdl;
	struct siofile *f;
	unsigned int mode = *rmode;

	hdl = sio_open(path, mode, 1);
	if (hdl == NULL) {
		if (mode != (SIO_PLAY | SIO_REC))
			return NULL;
		hdl = sio_open(path, SIO_PLAY, 1);
		if (hdl != NULL)
			mode = SIO_PLAY;
		else {
			hdl = sio_open(path, SIO_REC, 1);
			if (hdl != NULL)
				mode = SIO_REC;
			else
				return NULL;
		}
#ifdef DEBUG
		if (debug_level >= 1) {
			dbg_puts("warning, device opened in ");
			dbg_puts(mode == SIO_PLAY ? "play-only" : "rec-only");
			dbg_puts(" mode\n");
		}
#endif
	}

	sio_initpar(&par);
	if (mode & SIO_REC) {
		par.bits = ipar->bits;
		par.bps = ipar->bps;
		par.sig = ipar->sig;
		par.le = ipar->le;
		par.msb = ipar->msb;
		par.rate = ipar->rate;
		par.rchan = ipar->cmax + 1;
	} else {
		par.bits = opar->bits;
		par.bps = opar->bps;
		par.sig = opar->sig;
		par.le = opar->le;
		par.msb = opar->msb;
		par.rate = opar->rate;
	}
	if (mode & SIO_PLAY)
		par.pchan = opar->cmax + 1;
	if (*bufsz)
		par.appbufsz = *bufsz;
	if (*round)
		par.round = *round;
	if (!sio_setpar(hdl, &par))
		goto bad_close;
	if (!sio_getpar(hdl, &par))
		goto bad_close;
	if (mode & SIO_REC) {
		ipar->bits = par.bits;
		ipar->bps = par.bps;
		ipar->sig = par.sig;
		ipar->le = par.le;
		ipar->msb = par.msb;
		ipar->rate = par.rate;
		ipar->cmin = 0;
		ipar->cmax = par.rchan - 1;
	}
	if (mode & SIO_PLAY) {
		opar->bits = par.bits;
		opar->bps = par.bps;
		opar->sig = par.sig;
		opar->le = par.le;
		opar->msb = par.msb;
		opar->rate = par.rate;
		opar->cmin = 0;
		opar->cmax = par.pchan - 1;
	}
	*rmode = mode;
	*bufsz = par.bufsz;
	*round = par.round;
	f = (struct siofile *)file_new(ops, path, sio_nfds(hdl));
	if (f == NULL)
		goto bad_close;
	f->hdl = hdl;
	f->started = 0;
	f->wtickets = 0;
	f->rtickets = 0;
	f->wbpf = par.pchan * par.bps;
	f->rbpf = par.rchan * par.bps;
	f->bufsz = par.bufsz;
	sio_onmove(f->hdl, siofile_cb, f);
	return f;
 bad_close:
	sio_close(hdl);
	return NULL;
}
示例#28
0
文件: ao_sndio.c 项目: enkore/mpv
/*
 * open device and setup parameters
 * return: 0=success -1=fail
 */
static int init(struct ao *ao)
{
    struct priv *p = ao->priv;

    struct af_to_par {
        int format, bits, sig, le;
    } static const af_to_par[] = {
        {AF_FORMAT_U8,      8, 0, 0},
        {AF_FORMAT_S8,      8, 1, 0},
        {AF_FORMAT_U16_LE, 16, 0, 1},
        {AF_FORMAT_U16_BE, 16, 0, 0},
        {AF_FORMAT_S16_LE, 16, 1, 1},
        {AF_FORMAT_S16_BE, 16, 1, 0},
        {AF_FORMAT_U24_LE, 16, 0, 1},
        {AF_FORMAT_U24_BE, 24, 0, 0},
        {AF_FORMAT_S24_LE, 24, 1, 1},
        {AF_FORMAT_S24_BE, 24, 1, 0},
        {AF_FORMAT_U32_LE, 32, 0, 1},
        {AF_FORMAT_U32_BE, 32, 0, 0},
        {AF_FORMAT_S32_LE, 32, 1, 1},
        {AF_FORMAT_S32_BE, 32, 1, 0}
    }, *ap;
    int i;

    p->hdl = sio_open(p->dev, SIO_PLAY, 0);
    if (p->hdl == NULL) {
        MP_ERR(ao, "can't open sndio %s\n", p->dev);
        goto error;
    }

    ao->format = af_fmt_from_planar(ao->format);

    sio_initpar(&p->par);
    for (i = 0, ap = af_to_par;; i++, ap++) {
        if (i == sizeof(af_to_par) / sizeof(struct af_to_par)) {
            MP_VERBOSE(ao, "unsupported format\n");
            p->par.bits = 16;
            p->par.sig = 1;
            p->par.le = SIO_LE_NATIVE;
            break;
        }
        if (ap->format == ao->format) {
            p->par.bits = ap->bits;
            p->par.sig = ap->sig;
            if (ap->bits > 8)
                p->par.le = ap->le;
            if (ap->bits != SIO_BPS(ap->bits))
                p->par.bps = ap->bits / 8;
            break;
        }
    }
    p->par.rate = ao->samplerate;

    struct mp_chmap_sel sel = {0};
    for (int n = 0; n < MP_NUM_CHANNELS+1; n++)
        mp_chmap_sel_add_map(&sel, &sndio_layouts[n]);

    if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
        goto error;

    p->par.pchan = ao->channels.num;
    p->par.appbufsz = p->par.rate * 250 / 1000;    /* 250ms buffer */
    p->par.round = p->par.rate * 10 / 1000;    /*  10ms block size */
    if (!sio_setpar(p->hdl, &p->par)) {
        MP_ERR(ao, "couldn't set params\n");
        goto error;
    }
    if (!sio_getpar(p->hdl, &p->par)) {
        MP_ERR(ao, "couldn't get params\n");
        goto error;
    }
    if (p->par.bits == 8 && p->par.bps == 1) {
        ao->format = p->par.sig ? AF_FORMAT_S8 : AF_FORMAT_U8;
    } else if (p->par.bits == 16 && p->par.bps == 2) {
        ao->format = p->par.sig ?
            (p->par.le ? AF_FORMAT_S16_LE : AF_FORMAT_S16_BE) :
            (p->par.le ? AF_FORMAT_U16_LE : AF_FORMAT_U16_BE);
    } else if ((p->par.bits == 24 || p->par.msb) && p->par.bps == 3) {
        ao->format = p->par.sig ?
            (p->par.le ? AF_FORMAT_S24_LE : AF_FORMAT_S24_BE) :
            (p->par.le ? AF_FORMAT_U24_LE : AF_FORMAT_U24_BE);
    } else if ((p->par.bits == 32 || p->par.msb) && p->par.bps == 4) {
        ao->format = p->par.sig ?
            (p->par.le ? AF_FORMAT_S32_LE : AF_FORMAT_S32_BE) :
            (p->par.le ? AF_FORMAT_U32_LE : AF_FORMAT_U32_BE);
    } else {
        MP_ERR(ao, "couldn't set format\n");
        goto error;
    }

    ao->bps = p->par.bps * p->par.pchan * p->par.rate;
    p->havevol = sio_onvol(p->hdl, volcb, p);
    sio_onmove(p->hdl, movecb, p);
    p->delay = 0;
    if (!sio_start(p->hdl))
        MP_ERR(ao, "init: couldn't start\n");

    p->pfd = calloc (sio_nfds(p->hdl), sizeof (struct pollfd));
    if (!p->pfd)
        goto error;

    return 0;

error:
    if (p->hdl)
      sio_close(p->hdl);

    return -1;
}
示例#29
0
av_cold int ff_sndio_open(AVFormatContext *s1, int is_output,
                          const char *audio_device)
{
    SndioData *s = s1->priv_data;
    struct sio_hdl *hdl;
    struct sio_par par;

    hdl = sio_open(audio_device, is_output ? SIO_PLAY : SIO_REC, 0);
    if (!hdl) {
        av_log(s1, AV_LOG_ERROR, "Could not open sndio device\n");
        return AVERROR(EIO);
    }

    sio_initpar(&par);

    par.bits = 16;
    par.sig  = 1;
    par.le   = SIO_LE_NATIVE;

    if (is_output)
        par.pchan = s->channels;
    else
        par.rchan = s->channels;
    par.rate = s->sample_rate;

    if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
        av_log(s1, AV_LOG_ERROR, "Impossible to set sndio parameters, "
               "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
        goto fail;
    }

    if (par.bits != 16 || par.sig != 1 ||
        (is_output  && (par.pchan != s->channels)) ||
        (!is_output && (par.rchan != s->channels)) ||
        (par.rate != s->sample_rate)) {
        av_log(s1, AV_LOG_ERROR, "Could not set appropriate sndio parameters, "
               "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
        goto fail;
    }

    s->buffer_size = par.round * par.bps *
                     (is_output ? par.pchan : par.rchan);

    if (is_output) {
        s->buffer = av_malloc(s->buffer_size);
        if (!s->buffer) {
            av_log(s1, AV_LOG_ERROR, "Could not allocate buffer\n");
            goto fail;
        }
    }

    s->codec_id    = par.le ? CODEC_ID_PCM_S16LE : CODEC_ID_PCM_S16BE;
    s->channels    = is_output ? par.pchan : par.rchan;
    s->sample_rate = par.rate;
    s->bps         = par.bps;

    sio_onmove(hdl, movecb, s);

    if (!sio_start(hdl)) {
        av_log(s1, AV_LOG_ERROR, "Could not start sndio\n");
        goto fail;
    }

    s->hdl = hdl;

    return 0;

fail:
    av_freep(&s->buffer);

    if (hdl)
        sio_close(hdl);

    return AVERROR(EIO);
}
示例#30
0
//释放端口
void CComm_Thread::Exit_ComPort()
{
    COApp* pApp=(COApp*)AfxGetApp();
    CommLink=false;
    sio_close(pApp->x_Port);
}