X-Git-Url: http://git.kpe.io/?p=wdq2wav.git;a=blobdiff_plain;f=wdq2wav.cpp;h=bcfd3b69267dfb8932174068efcfa37dc40e729b;hp=39b5a81c693ce2a0e87ae2efffe7c02cf471c89b;hb=6e0f65ebf4769e7f90e2ef29539cd493de9bf14b;hpb=3e5a9b0f75acd40dc7179719d17334b11fe9c7a2 diff --git a/wdq2wav.cpp b/wdq2wav.cpp index 39b5a81..bcfd3b6 100644 --- a/wdq2wav.cpp +++ b/wdq2wav.cpp @@ -56,7 +56,7 @@ const int g_fileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; #define O_BINARY 0 #endif #endif - + void error_msg (const char *msg) @@ -83,7 +83,7 @@ fileBasename (const char* filename) const char* pbackslash = strrchr (filename, '\\'); const char* p = filename; if (pbackslash && (! pslash || pbackslash >= pslash)) - p = pbackslash+1; + p = pbackslash+1; else if (pslash && (! pbackslash || pslash >= pbackslash)) p = pslash+1; @@ -134,12 +134,12 @@ main (int argc, char *argv[]) bool play = false; const char* progname = argv[0]; - + while ((c = getopt (argc, argv, "rqvzmdph")) != -1) { switch (c) { case 'r': std::cout << "Version " << g_szIdStr << std::endl; - exit(0); + exit(0); break; case 'q': g_quiet = true; @@ -170,7 +170,7 @@ main (int argc, char *argv[]) } if (g_verbose || g_debug) - std::cout << "Version " << g_szIdStr << std::endl; + std::cout << "Version " << g_szIdStr << std::endl; argc -= optind; argv += optind; @@ -195,7 +195,7 @@ main (int argc, char *argv[]) std::cout << "Enter channel number: "; std::cin.getline (channel_buf, MAX_INPUT_STR); } - + char *channel_endptr; int channel = static_cast(strtol (channel_buf, &channel_endptr, 10)); if (*channel_endptr != 0) { @@ -213,10 +213,10 @@ main (int argc, char *argv[]) std::cout << "Enter output wav filename: "; std::cin.getline (wav_fname, MAX_INPUT_STR); } - + if (! wdq2wav (wdq_fname, channel, wav_fname, play)) return 1; - + return 0; } @@ -239,24 +239,24 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname, bool p } if (wdq.any_packed_channels()) { - std::ostringstream os; - os << "File contains 'packed' channels." << std::endl; - os << "Convert to 'Advanced CODAS headers' before processing with wdq2wav."; - error_msg (os.str().c_str()); - return false; + std::ostringstream os; + os << "File contains 'packed' channels." << std::endl; + os << "Convert to 'Advanced CODAS headers' before processing with wdq2wav."; + error_msg (os.str().c_str()); + return false; } if (! g_quiet || g_verbose || g_debug) { std::ostringstream os1; - os1 << "File: " << wdq_fname; + os1 << "File: " << wdq_fname; info_msg (os1.str().c_str()); - std::ostringstream os; - os << "Legacy Format: "; - if (wdq.m_bLegacy_format) - os << "Yes"; - else - os << "No"; - info_msg(os.str().c_str()); + std::ostringstream os; + os << "Legacy Format: "; + if (wdq.m_bLegacy_format) + os << "Yes"; + else + os << "No"; + info_msg(os.str().c_str()); std::ostringstream os2; time_t time = wdq.m_time_acq_start; struct tm* tm = gmtime (&time); @@ -273,7 +273,7 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname, bool p ", Sample Rate: " << wdq.m_sample_rate; info_msg (os4.str().c_str()); } - + WindaqChannel wdq_channel (wdq, channel); if (! wdq_channel.m_valid) { error_msg ("Error reading data from channel"); @@ -292,7 +292,7 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname, bool p ", maximum: " << wdq_channel.m_max_raw_data; info_msg (os3.str().c_str()); } - + if (g_debug) { std::ostringstream os4; os4 << " Scaled minimum: " << wdq_channel.m_min_scaled_data << @@ -318,7 +318,7 @@ wdq2wav (const char* wdq_fname, const int channel, const char *wav_fname, bool p if (play) wav.Play(); - + return true; } @@ -354,7 +354,7 @@ bool read_int2 (int fd, unsigned short int& n) tmp2 = tmp1; if (read (fd, &tmp1, 1) != 1) return false; - + n = tmp2 + (tmp1 * 256); return true; } @@ -368,7 +368,7 @@ bool read_int4 (int fd, unsigned int& n) tmp4 = tmp2; if (! read_int2 (fd, tmp2)) return false; - + n = tmp4 + (tmp2 * 65536); return true; } @@ -409,11 +409,11 @@ WindaqFile::ReadHeader () short unsigned int byte1 = (element1 & 0xFF00) >> 8; short unsigned int byte2 = element1 & 0xFF; if (byte1 == 0 || byte1 == 1) { - m_bLegacy_format = false; - m_sr_denom = m_sr_numer = 0; + m_bLegacy_format = false; + m_sr_denom = m_sr_numer = 0; } else { - m_sr_denom = (element1 & 0x7fff) >> 5; - m_sr_numer = (element1 & 0x8000) << 1; + m_sr_denom = (element1 & 0x7fff) >> 5; + m_sr_numer = (element1 & 0x8000) << 1; m_bLegacy_format = true; } unsigned short int element2; @@ -421,32 +421,32 @@ WindaqFile::ReadHeader () return false; if (m_bLegacy_format) - m_sr_numer |= element2; + m_sr_numer |= element2; unsigned char element3; if (! read_int1 (m_fd, element3)) return false; m_channel_offset = element3; if (g_debug) - std::cout << "Channel offset: " << m_channel_offset << std::endl; + std::cout << "Channel offset: " << m_channel_offset << std::endl; unsigned char element4; if (! read_int1 (m_fd, element4)) return false; m_nBytes_channel_header = element4; if (g_debug) - std::cout << "Channel header bytes: " << m_nBytes_channel_header << std::endl; - + std::cout << "Channel header bytes: " << m_nBytes_channel_header << std::endl; + unsigned short int element5; if (! read_int2 (m_fd, element5)) return false; m_nHeader_bytes = element5; if (g_debug) - std::cout << "Header bytes: " << m_nHeader_bytes << std::endl; + std::cout << "Header bytes: " << m_nHeader_bytes << std::endl; m_nMaxChannels = (m_nHeader_bytes - 112) / 36; if (m_nMaxChannels >= 144) - m_nChannels = byte2 & 0xFF; + m_nChannels = byte2 & 0xFF; else m_nChannels = byte2 & 0x1F; @@ -455,20 +455,20 @@ WindaqFile::ReadHeader () return false; m_nData_bytes = element6; if (g_debug) - std::cout << "Data bytes: " << m_nData_bytes << std::endl; + std::cout << "Data bytes: " << m_nData_bytes << std::endl; m_nSamples = (m_nData_bytes / m_nChannels) / 2; lseek (m_fd, 28, SEEK_SET); double element13; if (! read_float8 (m_fd, element13)) - return false; + return false; m_time_between_channel_samples = element13; if (m_bLegacy_format) m_sample_rate = (double) m_sr_numer / (double) (m_sr_denom * m_nChannels); - else - m_sample_rate = (double) m_nChannels / m_time_between_channel_samples; + else + m_sample_rate = (double) m_nChannels / m_time_between_channel_samples; lseek (m_fd, 36, SEEK_SET); if (! read_int4 (m_fd, m_time_acq_start)) @@ -480,17 +480,17 @@ WindaqFile::ReadHeader () lseek (m_fd, 100, SEEK_SET); unsigned short int element27; if (! read_int2 (m_fd, element27)) - return false; + return false; m_bHires = (element27 & 0x0001) ? true : false; if (g_debug) { - std::cout << "High resolution: "; - if (m_bHires) - std::cout << "Yes"; - else - std::cout << "No"; + std::cout << "High resolution: "; + if (m_bHires) + std::cout << "Yes"; + else + std::cout << "No"; - std::cout << std::endl; + std::cout << std::endl; } // Verify Windaq signature @@ -501,7 +501,7 @@ WindaqFile::ReadHeader () if (element35 != 0x8001) { std::ostringstream os; - m_error = "Incorrect signagure: file is not a valid WinDAQ file"; + m_error = "Incorrect signagure: file is not a valid WinDAQ file"; return false; } @@ -512,11 +512,11 @@ WindaqFile::ReadHeader () bool WindaqFile::any_packed_channels () { - for (int iChannel = 0; iChannel < m_nChannels; iChannel++) - if (is_channel_packed (iChannel)) - return true; + for (int iChannel = 0; iChannel < m_nChannels; iChannel++) + if (is_channel_packed (iChannel)) + return true; - return false; + return false; } bool @@ -527,11 +527,11 @@ WindaqFile::is_channel_packed (const int channel) lseek (m_fd, iStart + 31, SEEK_SET); unsigned char iReadings_per_data_point; if (! read_int1 (m_fd, iReadings_per_data_point)) - return false; + return false; if (iReadings_per_data_point > 1) - return true; - + return true; + return false; } @@ -542,11 +542,11 @@ WindaqChannel::WindaqChannel (WindaqFile& wdq, const int channel) if (wdq.m_valid) { if (channel >= 1 && channel <= wdq.m_nChannels) { if (read_channel_data()) - m_valid = true; + m_valid = true; } else { std::ostringstream os; os << "Channel " << channel << " is invalid, valid range 1-" << - wdq.m_nChannels; + wdq.m_nChannels; error_msg (os.str().c_str()); } } @@ -565,9 +565,9 @@ WindaqChannel::read_channel_data () m_data = new signed short int [r_wdq.m_nSamples * 2]; - lseek (fd, r_wdq.m_channel_offset + 8 + - (m_channel - 1) * r_wdq.m_nBytes_channel_header, - SEEK_SET); + lseek (fd, r_wdq.m_channel_offset + 8 + + (m_channel - 1) * r_wdq.m_nBytes_channel_header, + SEEK_SET); if (! read_float8 (fd, m_slope)) return false; @@ -581,11 +581,11 @@ WindaqChannel::read_channel_data () return false; } m_units = units; - + long int row_bytes = 2 * r_wdq.m_nChannels; signed short int *sample_row = new signed short int [row_bytes]; - + signed short int* psample = &sample_row[m_channel - 1]; lseek (fd, r_wdq.m_nHeader_bytes, SEEK_SET); @@ -604,24 +604,24 @@ WindaqChannel::read_channel_data () #if WORDS_BIG_ENDIAN unsigned char* p = reinterpret_cast(&v); - unsigned char c = p[0]; p[0] = p[1]; p[1] = c; + unsigned char c = p[0]; p[0] = p[1]; p[1] = c; #endif signed short int value = v; - if (! r_wdq.m_bHires) - value >>= 2; + if (! r_wdq.m_bHires) + value >>= 2; m_data[i] = value; total_data += value; - + if (i == 0) { data_max = value; data_min = value; } else { if (value > data_max) - data_max = value; + data_max = value; else if (value < data_min) - data_min = value; + data_min = value; } } @@ -635,11 +635,11 @@ WindaqChannel::read_channel_data () int mean = nearest(dmean); std::cout << "Removing mean: " << (dmean * m_slope) + m_intercept << " " << m_units << std::endl; - + for (i = 0; i < r_wdq.m_nSamples; i++) m_data[i] -= mean; } - + delete sample_row; return true; } @@ -659,22 +659,22 @@ WavFile::WavFile (WindaqChannel& wdq_channel, const char* fname) if (g_ignore_zero) { data_offset = -wdq_channel.m_min_scaled_data; if (wdq_channel.m_max_scaled_data != wdq_channel.m_min_scaled_data) - data_scale = 65535. / (wdq_channel.m_max_scaled_data - - wdq_channel.m_min_scaled_data); + data_scale = 65535. / (wdq_channel.m_max_scaled_data - + wdq_channel.m_min_scaled_data); } else { double max_value = fabs(wdq_channel.m_max_scaled_data); if (fabs (wdq_channel.m_min_scaled_data) > max_value) - max_value = fabs (wdq_channel.m_min_scaled_data); + max_value = fabs (wdq_channel.m_min_scaled_data); if (max_value != 0.) - data_scale = 32767. / max_value; + data_scale = 32767. / max_value; } - + if (g_debug) { std::ostringstream os; os << " Wav data_scale: " << data_scale << ", data_offset: " << data_offset; info_msg (os.str().c_str()); } - + m_nHeaderBytes = 44; m_nDataBytes = m_nSamples * m_nBytesPerSample * m_nChannels; m_nFileBytes = m_nHeaderBytes + m_nDataBytes; @@ -686,7 +686,7 @@ WavFile::WavFile (WindaqChannel& wdq_channel, const char* fname) signed short int* output = &m_data[nHeaderShortInts]; double slope = wdq_channel.m_slope; double intercept = wdq_channel.m_intercept; - + if (! fill_header ()) return; @@ -696,16 +696,16 @@ WavFile::WavFile (WindaqChannel& wdq_channel, const char* fname) double value = input[i]; value = (slope * value) + intercept; if (g_ignore_zero) { - value = (value + data_offset) * data_scale; - value += 0.5 - 32768; + value = (value + data_offset) * data_scale; + value += 0.5 - 32768; } else { - value = value * data_scale; + value = value * data_scale; } - + signed short int v = static_cast(value); #if WORDS_BIG_ENDIAN unsigned char* p = reinterpret_cast(&v); - unsigned char c = p[0]; p[0] = p[1]; p[1] = c; + unsigned char c = p[0]; p[0] = p[1]; p[1] = c; #endif output[i] = v; } @@ -719,7 +719,7 @@ WavFile::~WavFile () { if (m_fd != 0) close (m_fd); - + if (m_data != NULL) delete m_data; } @@ -764,13 +764,13 @@ WavFile::fill_header () // Sample Rate put_int4 (pData + 24, static_cast (m_rate + 0.5)); - // Bytes per second + // Bytes per second put_int4 (pData + 28, static_cast (m_rate * m_nBytesPerSample + 0.5)); // Bytes per sample put_int2 (pData + 32, m_nBytesPerSample * m_nChannels); - // Bits per sample + // Bits per sample put_int2 (pData + 34, m_nBitsPerSample); strncpy (pData + 36, "data", 4); @@ -802,7 +802,7 @@ WavFile::WriteFile () if (close (m_fd) < 0) error_msg ("Error closing output file"); - + m_fd = 0; return true; } @@ -827,7 +827,7 @@ WavFile::Play () error_msg ("Error opening /dev/dsp"); return false; } - + int format = AFMT_S16_LE; if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) == -1) { error_msg ("Error setting DSP format"); @@ -837,7 +837,7 @@ WavFile::Play () error_msg ("DSP Format not set"); close(fd); return false; } - + unsigned int channels = m_nChannels; if (ioctl (fd, SNDCTL_DSP_CHANNELS, &format) == -1) { error_msg ("Error setting number of channels"); @@ -858,7 +858,7 @@ WavFile::Play () os << "Warning: Sample rate set to " << speed << ", not " << m_rate; error_msg (os.str().c_str()); } - + if (write (fd, reinterpret_cast(m_data) + m_nHeaderBytes, m_nDataBytes) != m_nDataBytes) { error_msg ("Error writing audio samples"); @@ -869,8 +869,8 @@ WavFile::Play () return true; #else #endif - + return false; } - +