/* -------------------------------------------------------------------------- */ static void usbhid_get(t_usbhid *x, t_float length_arg) { if(x->debug_level) post("usbhid_get"); int i; int packet_bytes = (int)length_arg; char packet[packet_bytes]; if ( !hid_is_opened(x->x_hidinterface) ) { error("[usbhid] device not open, can't get data"); return; } x->x_hid_return = hid_get_input_report(x->x_hidinterface, NULL, x->x_read_element_count, packet, length_arg); if (x->x_hid_return != HID_RET_SUCCESS) { error("[usbhid] hid_get_input_report failed with return code %d\n", x->x_hid_return); reset_output(x); add_float_to_output(x, x->x_hid_return); outlet_anything(x->x_status_outlet, gensym("getError"), x->output_count, x->output); } reset_output(x); for(i=0; i<packet_bytes; ++i) add_float_to_output(x,packet[i]); outlet_list(x->x_data_outlet, &s_list, x->output_count, x->output); post("x->x_read_elements %d",x->x_read_elements); }
void Errors::redirect_output(bool to_buff, bool main_console) { #ifdef _WCON if (to_buff) { wcon_redirect_output(cmsg,new ostrstream(msg_buff,MSG_BUFF_SIZE),main_console); wcon_redirect_output(cerr,new ostrstream(err_buff,ERR_BUFF_SIZE),main_console); } else { wcon_redirect_off(cmsg); wcon_redirect_off(cerr); } #else if (to_buff) { _cmsg_out = &str_cmsg; _cerr_out = &str_cerr; mDllOutputRedirected = true; str_cmsg.seekp(0); str_cerr.seekp(0); } else { reset_output(); mDllOutputRedirected = false; str_cmsg << ends; str_cerr << ends; } #endif }
// Output struct void corrobj::make_output() { output.nrad = par.nrad; output.nlum = par.nlum; output.nkgmr = par.nkgmr; output.rsum.resize(par.nrad); output.kgflux.resize(par.nrad); output.krflux.resize(par.nrad); output.kiflux.resize(par.nrad); switch (par.output_type) { case 1: output.radcounts.resize(par.nrad); break; case 2: output.radcounts.resize(par.nrad); output.radlum.resize(par.nrad); break; case 3: output.counts.Allocate(par.nkgmr, par.nlum, par.nrad); output.lum.Allocate(par.nkgmr, par.nlum, par.nrad); break; default: break; } reset_output(); }
/* -------------------------------------------------------------------------- */ static void usbhid_set(t_usbhid *x, t_symbol *s, int argc, t_atom *argv) { if(x->debug_level) post("usbhid_set"); char *packet; if ( !hid_is_opened(x->x_hidinterface) ) { error("[usbhid] device not open, can't set data"); return; } packet = make_hid_packet(argc, argv); x->x_hid_return = hid_set_output_report(x->x_hidinterface, x->x_write_paths, x->x_write_path_count, packet, argc); if (x->x_hid_return != HID_RET_SUCCESS) { error("[usbhid] hid_get_input_report failed with return code %d\n", x->x_hid_return); reset_output(x); add_float_to_output(x, x->x_hid_return); outlet_anything(x->x_status_outlet, gensym("set_error"), x->output_count, x->output); } }
/* -------------------------------------------------------------------------- */ static void usbhid_close(t_usbhid *x) { if(x->debug_level) post("usbhid_close"); t_int ret; char string_buffer[STRING_BUFFER_LENGTH]; if ( hid_is_opened(x->x_hidinterface) ) { ret = get_device_string(x->x_hidinterface,string_buffer); x->x_hid_return = hid_close(x->x_hidinterface); if (x->x_hid_return == HID_RET_SUCCESS) { if (ret) post("[usbhid]: closed %s",string_buffer); else post("[usbhid]: closed device"); reset_output(x); add_float_to_output(x,0); outlet_anything(x->x_status_outlet, gensym("open"), x->output_count, x->output); } else { error("[usbhid] could not close %d, error #%d",x->x_device_number,x->x_hid_return); } } }
/* -------------------------------------------------------------------------- */ static void usbhid_write(t_usbhid *x, t_symbol *s, int argc, t_atom *argv) { if(x->debug_level) post("usbhid_write"); int i; // const int path[] = {0x000c0001, 0x000c0001}; // int path[] = {0x00010005, 0x00010036}; int path[] = {0xffa00001, 0xffa00005}; // int *path; unsigned int const depth = 2; // number of 32bit chunks in the path unsigned char const SEND_PACKET_LEN = 1; // number of bytes in packet char const PACKET[] = { 0xff, 0xff }; // the data to write // char const PACKET[] = { 0x00, 0x00 }; // the data to write // char PACKET[] = { 0x00, 0x00 }; // the data to write if ( !hid_is_opened(x->x_hidinterface) ) { error("[usbhid] device not open, can't set data"); return; } /* path = getbytes(sizeof(int) * (argc - 1)); depth = (argc - 1) / 2; for(i = 0; i < argc - 1; ++i) { path[(i+1)/2] = (strtol(atom_getsymbol(argv + i)->s_name, NULL, 16) << 16) + (strtol(atom_getsymbol(argv + i + 1)->s_name, NULL, 16) & 0x0000ffff); ++i; } SEND_PACKET_LEN = 2; PACKET[1] = (unsigned short) atom_getfloat(argv + argc - 1); */ post("depth: %d SEND_PACKET_LEN: %d PACKET[0]: %d PACKET[1]: %d", depth, SEND_PACKET_LEN, PACKET[0], PACKET[1]); for(i = 0; i < (argc - 1) / 2; ++i) { post("path %d: 0x%08x", i, path[i]); } x->x_hid_return = hid_set_output_report(x->x_hidinterface, path, depth, PACKET, SEND_PACKET_LEN); if (x->x_hid_return != HID_RET_SUCCESS) { error("[usbhid] hid_set_output_report failed with return code %d", x->x_hid_return); reset_output(x); add_float_to_output(x, x->x_hid_return); outlet_anything(x->x_status_outlet, gensym("get_error"), x->output_count, x->output); } post("wrote"); }
/* -------------------------------------------------------------------------- */ static void usbhid_open(t_usbhid *x, t_symbol *vendor_id_hex, t_symbol *product_id_hex) { if(x->debug_level) post("usbhid_open"); char string_buffer[STRING_BUFFER_LENGTH]; if( init_libhid(x) != HID_RET_SUCCESS ) return; /* convert hex symbols to ints */ x->vendor_id = (unsigned short) strtol(vendor_id_hex->s_name, NULL, 16); x->product_id = (unsigned short) strtol(product_id_hex->s_name, NULL, 16); if( hid_is_opened(x->x_hidinterface) ) { if( (x->vendor_id == x->x_hidinterface->device->descriptor.idVendor) && (x->product_id == x->x_hidinterface->device->descriptor.idProduct)) { post("[usbhid] device already opened"); return; } else { usbhid_close(x); } } if( !hid_is_opened(x->x_hidinterface) ) { HIDInterfaceMatcher matcher = { x->vendor_id, x->product_id, NULL, NULL, 0 }; x->x_hid_return = hid_force_open(x->x_hidinterface, 0, &matcher, 3); if (x->x_hid_return == HID_RET_SUCCESS) { if (get_device_string(x->x_hidinterface,string_buffer)) post("[usbhid]: opened %s",string_buffer); reset_output(x); add_float_to_output(x,1); outlet_anything(x->x_status_outlet, gensym("open"), x->output_count, x->output); } else { error("[usbhid] hid_force_open failed with return code %d\n", x->x_hid_return); } } }
/* * Change the playback sample rate. * Consider that changing it after starting playback is not covered by gapless code! */ static void reset_audio(long rate, int channels, int format) { #ifndef NOXFERMEM if (param.usebuffer) { /* wait until the buffer is empty, * then tell the buffer process to * change the sample rate. [OF] */ while (xfermem_get_usedspace(buffermem) > 0) if (xfermem_block(XF_WRITER, buffermem) == XF_CMD_TERMINATE) { intflag = TRUE; break; } buffermem->freeindex = -1; buffermem->readindex = 0; /* I know what I'm doing! ;-) */ buffermem->freeindex = 0; if (intflag) return; buffermem->rate = pitch_rate(rate); buffermem->channels = channels; buffermem->format = format; buffer_reset(); } else { #endif if(ao == NULL) { error("Audio handle should not be NULL here!"); safe_exit(98); } ao->rate = pitch_rate(rate); ao->channels = channels; ao->format = format; if(reset_output(ao) < 0) { error1("failed to reset audio device: %s", strerror(errno)); safe_exit(1); } #ifndef NOXFERMEM } #endif }
void buffer_loop(audio_output_t *ao, sigset_t *oldsigset) { int bytes, outbytes; int my_fd = buffermem->fd[XF_READER]; txfermem *xf = buffermem; int done = FALSE; int preload; catchsignal (SIGINT, catch_interrupt); catchsignal (SIGUSR1, catch_usr1); sigprocmask (SIG_SETMASK, oldsigset, NULL); xfermem_putcmd(my_fd, XF_CMD_WAKEUP); debug("audio output: waiting for cap requests"); /* wait for audio setup queries */ while(1) { int cmd; cmd = xfermem_block(XF_READER, xf); if(cmd == XF_CMD_AUDIOCAP) { ao->rate = xf->rate; ao->channels = xf->channels; ao->format = ao->get_formats(ao); debug3("formats for %liHz/%ich: 0x%x", ao->rate, ao->channels, ao->format); xf->format = ao->format; xfermem_putcmd(my_fd, XF_CMD_AUDIOCAP); } else if(cmd == XF_CMD_WAKEUP) { debug("got wakeup... leaving config mode"); xfermem_putcmd(buffermem->fd[XF_READER], XF_CMD_WAKEUP); break; } else { error1("unexpected command %i", cmd); return; } } /* Fill complete buffer on first run before starting to play. * Live mp3 streams constantly approach buffer underrun otherwise. [dk] */ preload = (int)(param.preload*xf->size); if(preload > xf->size) preload = xf->size; if(preload < 0) preload = 0; for (;;) { if (intflag) { debug("handle intflag... flushing"); intflag = FALSE; ao->flush(ao); /* Either prepare for waiting or empty buffer now. */ if(!xf->justwait) xf->readindex = xf->freeindex; else { int cmd; debug("Prepare for waiting; draining command queue. (There's a lot of wakeup commands pending, usually.)"); do { cmd = xfermem_getcmd(my_fd, FALSE); /* debug1("drain: %i", cmd); */ } while(cmd > 0); } if(xf->wakeme[XF_WRITER]) xfermem_putcmd(my_fd, XF_CMD_WAKEUP); } if (usr1flag) { debug("handling usr1flag"); usr1flag = FALSE; /* close and re-open in order to flush * the device's internal buffer before * changing the sample rate. [OF] */ /* writer must block when sending SIGUSR1 * or we will lose all data processed * in the meantime! [dk] */ xf->readindex = xf->freeindex; /* We've nailed down the new starting location - * writer is now safe to go on. [dk] */ if (xf->wakeme[XF_WRITER]) xfermem_putcmd(my_fd, XF_CMD_WAKEUP); ao->rate = xf->rate; ao->channels = xf->channels; ao->format = xf->format; if (reset_output(ao) < 0) { error1("failed to reset audio: %s", strerror(errno)); exit(1); } } if ( (bytes = xfermem_get_usedspace(xf)) < outburst ) { /* if we got a buffer underrun we first * fill 1/8 of the buffer before continue/start * playing */ if (preload < xf->size>>3) preload = xf->size>>3; if(preload < outburst) preload = outburst; } debug1("bytes: %i", bytes); if(xf->justwait || bytes < preload) { int cmd; if (done && !bytes) { break; } if(xf->justwait || !done) { /* Don't spill into errno check below. */ errno = 0; cmd = xfermem_block(XF_READER, xf); debug1("got %i", cmd); switch(cmd) { /* More input pending. */ case XF_CMD_WAKEUP_INFO: continue; /* Yes, we know buffer is low but * know we don't care. */ case XF_CMD_WAKEUP: break; /* Proceed playing. */ case XF_CMD_ABORT: /* Immediate end, discard buffer contents. */ return; /* Cleanup happens outside of buffer_loop()*/ case XF_CMD_TERMINATE: /* Graceful end, playing stuff in buffer and then return. */ debug("going to terminate"); done = TRUE; break; case XF_CMD_RESYNC: debug("ordered resync"); if (param.outmode == DECODE_AUDIO) ao->flush(ao); xf->readindex = xf->freeindex; if (xf->wakeme[XF_WRITER]) xfermem_putcmd(my_fd, XF_CMD_WAKEUP); continue; break; case -1: if(intflag || usr1flag) /* Got signal, handle it at top of loop... */ { debug("buffer interrupted"); continue; } if(errno) error1("Yuck! Error in buffer handling... or somewhere unexpected: %s", strerror(errno)); done = TRUE; xf->readindex = xf->freeindex; xfermem_putcmd(xf->fd[XF_READER], XF_CMD_TERMINATE); break; default: fprintf(stderr, "\nEh!? Received unknown command 0x%x in buffer process.\n", cmd); } } } /* Hack! The writer issues XF_CMD_WAKEUP when first adjust * audio settings. We do not want to lower the preload mark * just yet! */ if (xf->justwait || !bytes) continue; preload = outburst; /* set preload to lower mark */ if (bytes > xf->size - xf->readindex) bytes = xf->size - xf->readindex; if (bytes > outburst) bytes = outburst; /* The output can only take multiples of framesize. */ bytes -= bytes % ao->framesize; debug("write"); outbytes = flush_output(ao, (unsigned char*) xf->data + xf->readindex, bytes); if(outbytes < bytes) { if(outbytes < 0) outbytes = 0; if(!intflag && !usr1flag) { error1("Ouch ... error while writing audio data: %s", strerror(errno)); /* * done==TRUE tells writer process to stop * sending data. There might be some latency * involved when resetting readindex to * freeindex so we might need more than one * cycle to terminate. (The number of cycles * should be finite unless I managed to mess * up something. ;-) [dk] */ done = TRUE; xf->readindex = xf->freeindex; xfermem_putcmd(xf->fd[XF_READER], XF_CMD_TERMINATE); } else debug("buffer interrupted"); } bytes = outbytes; xf->readindex = (xf->readindex + bytes) % xf->size; if (xf->wakeme[XF_WRITER]) xfermem_putcmd(my_fd, XF_CMD_WAKEUP); } }
void corrobj::correlate() { int step=100; int bigStep=5000; cout << endl << "Each dot is " << step << " primaries" << endl; //FILE *mFptr = fopen(par.output_file, "w"); mFptr = fopen(par.output_file, "w"); if (par.corrtype == 3) { string index_file = par.output_file; index_file += ".index"; mIndexFptr = fopen(index_file.c_str(), "w"); // Only have a header for the index file cout << "Pairs written to file: " << par.output_file << endl; cout << "Writing header to index file: " << index_file << endl; //return; write_pairindex_header(mIndexFptr); } else if (par.corrtype == 1) { max_kept_imag = 0; cout << "Writing header for file " << par.output_file << endl; write_header(mFptr); } else { cout << "Writing header for file " << par.output_file << endl; write_edge_header(mFptr); } t0 = time(NULL); int64 npairs=0; vector<uint64> idlist; vector<int> seclist; vector<float> radlist; for (int index=0; index < par.nprimary; index++) { if (debug) { cout<<"index = "<<index<<endl; fflush(stdout); } double ra = primary[index].ra; double dec = primary[index].dec; float z = primary[index].z; float DA = primary[index].DA; float DLum = DA*(1+z)*(1+z); if (debug) { cout<<"before intersect"<<endl; fflush(stdout); } // Get triangle list intersect(ra, dec, DA, idlist); if (debug) { cout<<"before seclist"<<endl; fflush(stdout); } // Get list of objects within rmin,rmax get_seclist(ra, dec, DA, idlist, seclist, radlist); // WARNING: corrtype == 3 isn't actually implemented and I think // it is mixed up in definition if (par.corrtype == 3) { // Just writing the pair information int tnpairs = write_colorlumrad_pairs(index, z, DLum, seclist, radlist); // Now write to index if any pairs were found npairs += tnpairs; fwrite((char *)&primary[index].index, sizeof(int), 1, mIndexFptr); fwrite((char *)&tnpairs, sizeof(int), 1, mIndexFptr); } else if (par.corrtype == 1) { if (debug) { cout<<"before bin"<<endl; fflush(stdout); } // bin into grid of color, luminosity and radius, or perhaps // just radius depending on output_type bin_by_colorlumrad(z, DLum, seclist, radlist); if (debug) { cout<<"Writing output"<<endl; fflush(stdout); } output.index = primary[index].index; write_output(mFptr); if (debug) { cout<<"Resetting output"<<endl; fflush(stdout); } reset_output(); } else { // bin just into a grid of radius // This is for edges, we don't care anything about the lum or // color of the secondaries because it is only for randoms // secondaries anyway bin_by_rad(seclist, radlist); edge_output.index = primary[index].index; write_edge_output(mFptr); reset_edge_output(); } // Print some stuff if ( (index % step) == 0 && (index != 0)) printstuff(index, bigStep); if (debug) { cout<<"restarting loop"<<endl; fflush(stdout); } } if (par.corrtype == 3) { fclose(mIndexFptr); //rewind(mFptr); //fprintf(mFptr, "NROWS = %15lld\n", npairs); } fclose(mFptr); time_t t1 = time(NULL); //if(!quiet) printf("\nTime: %ld sec\n", (t1-t0)); if (par.corrtype == 1) cout << endl << "Max kept imag: " << max_kept_imag << endl; cout << endl << "Done" << endl; }
/* -------------------------------------------------------------------------- */ static void usbhid_get_descriptor(t_usbhid *x) { if(x->debug_level) post("usbhid_get"); unsigned int i = 0; t_int input_size = 0; t_int output_size = 0; t_int feature_size = 0; char buf[MAXPDSTRING]; if (!hid_is_opened(x->x_hidinterface)) { error("[usbget] cannot dump tree of unopened HIDinterface."); } else { post("[usbhid] parse tree of HIDInterface %s:\n", x->x_hidinterface->id); // reset_output(x); while (HIDParse(x->x_hidinterface->hid_parser, x->x_hidinterface->hid_data)) { reset_output(x); // add_symbol_to_output(x, gensym("path")); switch(x->x_hidinterface->hid_data->Type) { case 0x80: add_symbol_to_output(x, gensym("input")); input_size = input_size + x->x_hidinterface->hid_data->Size; break; case 0x90: add_symbol_to_output(x, gensym("output")); output_size = output_size + x->x_hidinterface->hid_data->Size; break; case 0xb0: add_symbol_to_output(x, gensym("feature")); feature_size = feature_size + x->x_hidinterface->hid_data->Size; break; default: add_symbol_to_output(x, gensym("UNKNOWN_TYPE")); } add_float_to_output(x, x->x_hidinterface->hid_data->Size); add_float_to_output(x, x->x_hidinterface->hid_data->Offset); add_symbol_to_output(x, gensym("path")); for (i = 0; i < x->x_hidinterface->hid_data->Path.Size; ++i) { // sprintf(buf, "0x%04x", x->x_hidinterface->hid_data->Path.Node[i].UPage); // add_symbol_to_output(x, gensym(buf)); // sprintf(buf, "0x%04x", x->x_hidinterface->hid_data->Path.Node[i].Usage); // add_symbol_to_output(x, gensym(buf)); // post("0x%04x%04x",x->x_hidinterface->hid_data->Path.Node[i].UPage, // x->x_hidinterface->hid_data->Path.Node[i].Usage); } add_symbol_to_output(x, gensym("logical")); add_float_to_output(x, x->x_hidinterface->hid_data->LogMin); add_float_to_output(x, x->x_hidinterface->hid_data->LogMax); outlet_anything(x->x_status_outlet, gensym("element"), x->output_count, x->output); } reset_output(x); // add_symbol_to_output(x, gensym("totalSize")); add_float_to_output(x, input_size); add_float_to_output(x, output_size); add_float_to_output(x, feature_size); outlet_anything(x->x_status_outlet, gensym("totalSize"), x->output_count, x->output); // outlet_anything(x->x_status_outlet, gensym("device"), // x->output_count, x->output); } }