void HtkFile::write_header() { int nSamples; int sampPeriod; short sampSize; short parmKind; // swap bytes first if (!big_endian) { swap4Bytes(&header.nSamples, &nSamples); swap4Bytes(&header.sampPeriod, &sampPeriod); swap2Bytes(&header.sampSize, &sampSize); swap2Bytes(&header.parmKind, &parmKind); } else { nSamples = header.nSamples; sampPeriod = header.sampPeriod; sampSize = header.sampSize; parmKind = header.parmKind; } // This function reads the header into the struct if (fwrite(&nSamples, 4, 1, stream) != 1) throw std::bad_alloc(); if (fwrite(&sampPeriod, 4, 1, stream) != 1) throw std::bad_alloc(); if (fwrite(&sampSize, 2, 1, stream) != 1) throw std::bad_alloc(); if (fwrite(&parmKind, 2, 1, stream) != 1) throw std::bad_alloc(); }
void HtkFile::read_header() { // This function reads the header into the struct if (fread(&header.nSamples, 4, 1, stream) != 1) throw std::bad_alloc(); if (fread(&header.sampPeriod, 4, 1, stream) != 1) throw std::bad_alloc(); if (fread(&header.sampSize, 2, 1, stream) != 1) throw std::bad_alloc(); if (fread(&header.parmKind, 2, 1, stream) != 1) throw std::bad_alloc(); if (!big_endian) { swap4Bytes(&header.nSamples, &header.nSamples); swap4Bytes(&header.sampPeriod, &header.sampPeriod); swap2Bytes(&header.sampSize, &header.sampSize); swap2Bytes(&header.parmKind, &header.parmKind); } // Should never find EOF here since you would be expecting the data in // the MultiFrame too. if (feof(stream) != 0 || ferror(stream) != 0) throw std::bad_alloc(); if (header.sampSize <= 0 || header.sampSize > 5000 || header.nSamples <= 0 || header.sampPeriod <= 0 || header.sampPeriod > 1000000) { std::cerr << "Error: HTK header is not readable." << std::endl; throw std::exception(); } }
size_t HtkFile::write_next_vector(float *data) { if (!big_endian) { for (size_t i = 0; i < (unsigned int)num_coefs(); i++) { swap4Bytes(&(data[i]),&(data[i])); } } size_t rc = fwrite(data,sizeof(float),num_coefs(),stream); return rc; }
size_t HtkFile::read_next_vector(float *data) { size_t rc = fread(data,sizeof(float),num_coefs(),stream); if (!big_endian) { for (size_t i = 0; i < rc; i++) { swap4Bytes(&(data[i]),&(data[i])); } } return rc; }
size_t HtkFile::read_next_vector(double *data) { float fdata[num_coefs()]; size_t rc = fread(fdata,sizeof(float), num_coefs(),stream); for (size_t i = 0; i < rc; i++) { if (!big_endian) { swap4Bytes(&(fdata[i]),&(fdata[i])); } data[i] = fdata[i]; } return rc; }
size_t HtkFile::read_next_vector(infra::vector &data) { float *fdata; fdata = new float[num_coefs()]; data.resize(num_coefs()); size_t rc = fread(fdata,sizeof(float), num_coefs(),stream); for (size_t i = 0; i < rc; i++) { if (!big_endian) { swap4Bytes(&(fdata[i]),&(fdata[i])); } data[i] = fdata[i]; } delete [] fdata; return rc; }