static int ad_conv_v22ea_rf(const char *path, const struct stat *sp, const struct vol *vol) { EC_INIT; struct adouble adv2; struct adouble adea; LOG(log_debug, logtype_default,"ad_conv_v22ea_rf(\"%s\"): BEGIN", fullpathname(path)); if (S_ISDIR(sp->st_mode)) return 0; ad_init(&adea, vol); ad_init_old(&adv2, AD_VERSION2, adea.ad_options); /* Open and lock adouble:v2 file */ EC_ZERO( ad_open(&adv2, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR) ); if (adv2.ad_rlen > 0) { EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) ); /* Create a adouble:ea resource fork */ EC_ZERO_LOG( ad_open(&adea, path, ADFLAGS_HF | ADFLAGS_RF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) ); EC_ZERO_LOG( copy_fork(ADEID_RFORK, &adea, &adv2) ); adea.ad_rlen = adv2.ad_rlen; ad_flush(&adea); } EC_CLEANUP: EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_RF) ); EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_RF) ); LOG(log_debug, logtype_default,"ad_conv_v22ea_rf(\"%s\"): END: %d", fullpathname(path), ret); EC_EXIT; }
int recordInit() { ad = ad_open(); if (ad == NULL) { printf("Error opening recording device.\n"); return 0; } c_ad = cont_ad_init(ad, ad_read); if (c_ad == NULL) { printf("Error initializing continues ad.\n"); return 0; } if (ad_start_rec(ad) < 0) { printf("Error starting recording.\n"); return 0; } if (cont_ad_calib(c_ad) < 0) { printf("Error calibrating continues ad.\n"); return 0; } ad_stop_rec(ad); return 1; }
int init() { cmd_ln_t *config; printf("Initializing audio device...\n"); config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-lm", MODELDIR "/lm/en/turtle.DMP", "-dict", MODELDIR "/lm/en/turtle.dic", NULL); if(config == NULL) { fprintf(stderr, "Could not init sphinx config.\n"); return 1; } psDecoder = ps_init(config); if(psDecoder == NULL) { fprintf(stderr, "Could not init psDecoder.\n"); return 2; } audioDevice = ad_open(); if(audioDevice == NULL) { fprintf(stderr, "Could not open Audio Device.\n"); return 3; } continousAudoDevice = cont_ad_init(audioDevice, ad_read); if(continousAudoDevice == NULL) { fprintf(stderr, "Could not open Audio Device.\n"); return 4; } if(ad_start_rec(audioDevice) < 0) { fprintf(stderr,"Failed to start recording.\n"); return 5; } if(cont_ad_calib(continousAudoDevice) < 0) { fprintf(stderr,"Failed to calibrate voice activity detection.\n"); return 6; } return 0; }
/* * Test reading of audio files. */ void test_audio_file() { START_TEST; char* filenames[] = {"data/mono_0:10.wav", "data/stereo_0:10.wav", "data/mono_0:10.mp3", "data/stereo_0:10.mp3", "data/mono_0:10.m4a", "data/stereo_0:10.m4a", "data/mono_0:10.opus", "data/stereo_0:10.opus"}; int i; for(i=0;i<G_N_ELEMENTS(filenames);i++){ WfDecoder f = {{0,}}; char* filename = find_wav(filenames[i]); if(!ad_open(&f, filename)) FAIL_TEST("file open: %s", filenames[i]); if(!g_str_has_suffix(filenames[i], ".opus")){ assert(f.info.sample_rate == 44100, "samplerate: %i (expected 44100)", f.info.sample_rate); }else{ assert(f.info.sample_rate == 48000, "samplerate: %i (expected 48000)", f.info.sample_rate); } int n = 8; int read_len = WF_PEAK_RATIO * n; int16_t data[f.info.channels][read_len]; WfBuf16 buf = { .buf = {data[0], data[1]}, .size = n * WF_PEAK_RATIO }; size_t readcount = 0; size_t total = 0; do { readcount = ad_read_short(&f, &buf); total += readcount; } while (readcount > 0); dbg(1, "diff=%zu", abs((int)total - (int)f.info.frames)); if(g_str_has_suffix(filenames[i], ".wav") || g_str_has_suffix(filenames[i], ".flac")){ assert(total == f.info.frames, "%s: incorrect number of frames read: %"PRIi64, filenames[i], f.info.frames); assert(!(total % 512) || !(total % 100), "%s: bad framecount: %zu", filenames[i], total); // test file sizes are always a round number }else{ // for some file types, f.info.frames is only an estimate assert(abs((int)total - (int)f.info.frames) < 2048, "%s: incorrect number of frames read: %"PRIi64, filenames[i], f.info.frames); } ad_close(&f); g_free(filename); }
/** * play audio-file with given file-path. * @param path absolute path to the file. * @param reset_pitch 1: reset set midi pitch adj. * @return 0 on success, -1 on error. */ static int jplay__play_pathX(const char* path, int reset_pitch) { #ifdef JACK_MIDI if (reset_pitch) { midi_note = 0; midi_octave = 0; } #endif WfDecoder* d = g_new0(WfDecoder, 1); if(!ad_open(d, path)) return -1; seek_request = -1.0; if(thread_run || rb){ jplay__stop(); } JACKaudiooutputinit(d); return 0; }
static int openAudioDevice(inputThread_t *p_thread) { int ret; // open audio device, used for recording audio data p_thread->audioDevice = ad_open(); if (p_thread->audioDevice == NULL) { PRINT_ERR("Failed to open audio device.\n"); return -1; } // init audio device as continous audio device p_thread->contAudioDevice = cont_ad_init(p_thread->audioDevice, ad_read); if (p_thread->contAudioDevice == NULL) { PRINT_ERR("Failed to init continuous audio device.\n"); return -2; } // calibrate audio device ret = ad_start_rec(p_thread->audioDevice); if (ret < 0) { PRINT_ERR("Failed to start recording (%d).\n", ret); return ret; } ret = cont_ad_calib(p_thread->contAudioDevice); if (ret < 0) { PRINT_ERR("Failed to calibrate continuous audio device (%d).\n", ret); return ret; } ad_stop_rec(p_thread->audioDevice); pthread_barrier_wait(&p_thread->startBarrier); return 0; }
double ad_maxsignal(const char *fn) { struct adinfo nfo; void * sf = ad_open(fn, &nfo); if (!sf) return 0.0; int read_len = 1024 * nfo.channels; float* sf_data = malloc(sizeof(float) * read_len); int readcount; float max_val = 0.0; do { readcount = ad_read(sf, sf_data, read_len); int k; for (k = 0; k < readcount; k++){ const float temp = fabs (sf_data [k]); max_val = temp > max_val ? temp : max_val; }; } while (readcount > 0); ad_close(sf); free(sf_data); ad_free_nfo(&nfo); return max_val; }
static int ad_conv_v22ea_hf(const char *path, const struct stat *sp, const struct vol *vol) { EC_INIT; struct adouble adv2; struct adouble adea; const char *adpath; int adflags; uint32_t ctime, mtime, afpinfo = 0; char *emptyad; LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): BEGIN", fullpathname(path)); ad_init(&adea, vol); ad_init_old(&adv2, AD_VERSION2, adea.ad_options); adflags = S_ISDIR(sp->st_mode) ? ADFLAGS_DIR : 0; /* Open and lock adouble:v2 file */ EC_ZERO( ad_open(&adv2, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR) ); EC_NEG1_LOG( ad_tmplock(&adv2, ADEID_RFORK, ADLOCK_WR | ADLOCK_FILELOCK, 0, 0, 0) ); EC_NEG1_LOG( adv2.ad_ops->ad_header_read(path, &adv2, sp) ); /* Check if it's a non-empty header */ if (S_ISREG(sp->st_mode)) emptyad = &emptyfilad[0]; else emptyad = &emptydirad[0]; if (ad_getentrylen(&adv2, ADEID_COMMENT) != 0) goto copy; if (ad_getentryoff(&adv2, ADEID_FINDERI) && (ad_getentrylen(&adv2, ADEID_FINDERI) == ADEDLEN_FINDERI) && (memcmp(ad_entry(&adv2, ADEID_FINDERI), emptyad, ADEDLEN_FINDERI) != 0)) goto copy; if (ad_getentryoff(&adv2, ADEID_FILEDATESI)) { EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_CREATE | AD_DATE_UNIX, &ctime) ); EC_ZERO_LOG( ad_getdate(&adv2, AD_DATE_MODIFY | AD_DATE_UNIX, &mtime) ); if ((ctime != mtime) || (mtime != sp->st_mtime)) goto copy; } if (ad_getentryoff(&adv2, ADEID_AFPFILEI)) { if (memcmp(ad_entry(&adv2, ADEID_AFPFILEI), &afpinfo, ADEDLEN_AFPFILEI) != 0) goto copy; } LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): default adouble", fullpathname(path), ret); goto EC_CLEANUP; copy: /* Create a adouble:ea meta EA */ LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): copying adouble", fullpathname(path), ret); EC_ZERO_LOGSTR( ad_open(&adea, path, adflags | ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE), "ad_conv_v22ea_hf(\"%s\"): error creating metadata EA: %s", fullpathname(path), strerror(errno)); EC_ZERO_LOG( ad_copy_header(&adea, &adv2) ); ad_flush(&adea); EC_CLEANUP: EC_ZERO_LOG( ad_close(&adv2, ADFLAGS_HF | ADFLAGS_SETSHRMD) ); EC_ZERO_LOG( ad_close(&adea, ADFLAGS_HF | ADFLAGS_SETSHRMD) ); LOG(log_debug, logtype_default,"ad_conv_v22ea_hf(\"%s\"): END: %d", fullpathname(path), ret); EC_EXIT; }
static int do_move(const char *from, const char *to) { struct stat sb; int ask, ch, first; /* * Check access. If interactive and file exists, ask user if it * should be replaced. Otherwise if file exists but isn't writable * make sure the user wants to clobber it. */ if (!fflg && !access(to, F_OK)) { /* prompt only if source exist */ if (lstat(from, &sb) == -1) { SLOG("%s: %s", from, strerror(errno)); return (1); } ask = 0; if (nflg) { if (vflg) printf("%s not overwritten\n", to); return (0); } else if (iflg) { (void)fprintf(stderr, "overwrite %s? (y/n [n]) ", to); ask = 1; } else if (access(to, W_OK) && !stat(to, &sb)) { (void)fprintf(stderr, "override for %s? (y/n [n]) ", to); ask = 1; } if (ask) { first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); if (first != 'y' && first != 'Y') { (void)fprintf(stderr, "not overwritten\n"); return (0); } } } int mustcopy = 0; /* * Besides the usual EXDEV we copy instead of moving if * 1) source AFP volume != dest AFP volume * 2) either source or dest isn't even an AFP volume */ if (!svolume.vol->v_path || !dvolume.vol->v_path || strcmp(svolume.vol->v_path, dvolume.vol->v_path) != 0) mustcopy = 1; cnid_t cnid = 0; if (!mustcopy) { if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) { SLOG("Couldn't resolve CNID for %s", from); return -1; } if (stat(from, &sb) != 0) { SLOG("Cant stat %s: %s", to, strerror(errno)); return -1; } if (rename(from, to) != 0) { if (errno == EXDEV) { mustcopy = 1; char path[MAXPATHLEN]; /* * If the source is a symbolic link and is on another * filesystem, it can be recreated at the destination. */ if (lstat(from, &sb) == -1) { SLOG("%s: %s", from, strerror(errno)); return (-1); } if (!S_ISLNK(sb.st_mode)) { /* Can't mv(1) a mount point. */ if (realpath(from, path) == NULL) { SLOG("cannot resolve %s: %s: %s", from, path, strerror(errno)); return (1); } } } else { /* != EXDEV */ SLOG("rename %s to %s: %s", from, to, strerror(errno)); return (1); } } /* rename != 0*/ switch (sb.st_mode & S_IFMT) { case S_IFREG: if (dvolume.vol->vfs->vfs_renamefile(dvolume.vol, -1, from, to) != 0) { SLOG("Error moving adouble file for %s", from); return -1; } break; case S_IFDIR: break; default: SLOG("Not a file or dir: %s", from); return -1; } /* get CNID of new parent dir */ cnid_t newpdid, newdid; if ((newdid = cnid_for_paths_parent(&dvolume, to, &newpdid)) == CNID_INVALID) { SLOG("Couldn't resolve CNID for parent of %s", to); return -1; } if (stat(to, &sb) != 0) { SLOG("Cant stat %s: %s", to, strerror(errno)); return 1; } char *p = strdup(to); char *name = basename(p); if (cnid_update(dvolume.vol->v_cdb, cnid, &sb, newdid, name, strlen(name)) != 0) { SLOG("Cant update CNID for: %s", to); return 1; } free(p); struct adouble ad; ad_init(&ad, dvolume.vol); if (ad_open(&ad, to, S_ISDIR(sb.st_mode) ? (ADFLAGS_DIR | ADFLAGS_HF | ADFLAGS_RDWR) : ADFLAGS_HF | ADFLAGS_RDWR) != 0) { SLOG("Error opening adouble for: %s", to); return 1; } ad_setid(&ad, sb.st_dev, sb.st_ino, cnid, newdid, dvolume.db_stamp); ad_flush(&ad); ad_close(&ad, ADFLAGS_HF); if (vflg) printf("%s -> %s\n", from, to); return (0); } if (mustcopy) return copy(from, to); /* If we get here it's an error */ return -1; }
int ad_set(int argc, char **argv, AFPObj *obj) { int c, firstarg; afpvol_t vol; struct stat st; int adflags = 0; struct adouble ad; while ((c = getopt(argc, argv, ":l:t:c:f:a:")) != -1) { switch(c) { case 'l': new_label = strdup(optarg); break; case 't': new_type = strdup(optarg); break; case 'c': new_creator = strdup(optarg); break; case 'f': new_flags = strdup(optarg); break; case 'a': new_attributes = strdup(optarg); break; case ':': case '?': usage_set(); return -1; break; } } if (argc <= optind) exit(1); cnid_init(); openvol(obj, argv[optind], &vol); if (vol.vol->v_path == NULL) exit(1); if (stat(argv[optind], &st) != 0) { perror("stat"); exit(1); } if (S_ISDIR(st.st_mode)) adflags = ADFLAGS_DIR; ad_init(&ad, vol.vol); if (ad_open(&ad, argv[optind], adflags | ADFLAGS_HF | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666) < 0) goto exit; if (new_label) change_label(argv[optind], &vol, &st, &ad, new_label); if (new_type) change_type(argv[optind], &vol, &st, &ad, new_type); if (new_creator) change_creator(argv[optind], &vol, &st, &ad, new_creator); if (new_flags) change_flags(argv[optind], &vol, &st, &ad, new_flags); if (new_attributes) change_attributes(argv[optind], &vol, &st, &ad, new_attributes); ad_flush(&ad); ad_close(&ad, ADFLAGS_HF); exit: closevol(&vol); return 0; }
int ad_finfo (const char *fn, struct adinfo *nfo) { ad_clear_nfo(nfo); void * sf = ad_open(fn, nfo); return ad_close(sf)?1:0; }
bool wf_ff_peakgen(const char* infilename, const char* peak_filename) { WfDecoder f = {{0,}}; if(!ad_open(&f, infilename)) return false; SNDFILE* outfile; SF_INFO sfinfo = { .format = SF_FORMAT_WAV | SF_FORMAT_PCM_16, .channels = f.info.channels, .samplerate = f.info.sample_rate, }; gchar* basename = g_path_get_basename(peak_filename); gchar* tmp_path = g_build_filename("/tmp", basename, NULL); g_free(basename); if(!(outfile = sf_open(tmp_path, SFM_WRITE, &sfinfo))) { printf ("Not able to open output file %s.\n", tmp_path); puts(sf_strerror(NULL)); return false; } int total_frames_written = 0; WfPeakSample total[sfinfo.channels]; memset(total, 0, sizeof(WfPeakSample) * sfinfo.channels); int n = 8; int read_len = WF_PEAK_RATIO * n; float* sf_data = g_malloc(sizeof(float) * read_len); int16_t data[f.info.channels][read_len]; WfBuf16 buf = { .buf = { data[0], data[1] }, .size = n * WF_PEAK_RATIO }; int readcount; int total_readcount = 0; do { readcount = ad_read_short(&f, &buf); total_readcount += readcount; int remaining = readcount; WfPeakSample peak[sfinfo.channels]; int j = 0; for(; j<n; j++) { WfPeakSample w[sfinfo.channels]; memset(peak, 0, sizeof(WfPeakSample) * sfinfo.channels); int k; for (k = 0; k < MIN(remaining, WF_PEAK_RATIO); k+=sfinfo.channels) { int c; for(c=0; c<sfinfo.channels; c++) { int16_t val = buf.buf[c][WF_PEAK_RATIO * j + k]; peak[c] = (WfPeakSample) { MAX(peak[c].positive, val), MIN(peak[c].negative, MAX(val, -32767)), // TODO value of SHRT_MAX messes up the rendering - why? }; } }; remaining -= WF_PEAK_RATIO; int c; for(c=0; c<sfinfo.channels; c++) { w[c] = peak[c]; total[c] = (WfPeakSample) { MAX(total[c].positive, w[c].positive), MIN(total[c].negative, w[c].negative), }; } total_frames_written += sf_writef_short (outfile, (short*)w, WF_PEAK_VALUES_PER_SAMPLE); } } while (readcount > 0); #if 0 if(f.info.channels > 1) dbg(0, "max=%i,%i min=%i,%i", total[0].positive, total[1].positive, total[0].negative, total[1].negative); else dbg(0, "max=%i min=%i", total[0].positive, total[0].negative); #endif #ifdef DEBUG if(g_str_has_suffix(infilename, ".mp3")) { dbg(1, "mp3"); f.info.frames = total_readcount; // update the estimate with the real frame count. } #else if(total_frames_written / WF_PEAK_VALUES_PER_SAMPLE != f.info.frames / WF_PEAK_RATIO) { gwarn("unexpected number of frames: %i != %Lu", total_frames_written / WF_PEAK_VALUES_PER_SAMPLE, f.info.frames / WF_PEAK_RATIO); } #endif ad_close(&f); sf_close (outfile); g_free(sf_data); int renamed = !rename(tmp_path, peak_filename); g_free(tmp_path); if(!renamed) return false; return true; }