OSDN Git Service

[COMMON][VM][Qt] Add common wav-loading / saving helper functions to common.cpp .
authorK.Ohta <whatisthis.sowhat@gmail.com>
Tue, 26 Jun 2018 13:18:33 +0000 (22:18 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Tue, 26 Jun 2018 13:18:33 +0000 (22:18 +0900)
[COMMON] Update min() and max().

source/src/common.cpp
source/src/common.h
source/src/qt/osd_sound.cpp
source/src/vm/datarec.cpp
source/src/vm/disk.cpp
source/src/vm/fmgen/opna.cpp
source/src/vm/fp200/io.cpp
source/src/vm/noise.cpp
source/src/vm/pc6001/psub.cpp
source/src/vm/pc6001/sub.cpp
source/src/vm/pc8201/cmt.cpp

index 00cdee0..83779ae 100644 (file)
@@ -65,6 +65,76 @@ uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x)
 #endif
 }
 
+uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x)
+{
+#if defined(__LITTLE_ENDIAN__)
+       return x;
+#else
+       uint32_t y;
+       y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
+           ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
+       return y;
+#endif
+}
+
+uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x)
+{
+#if defined(__LITTLE_ENDIAN__)
+       return x;
+#else
+       uint16_t y;
+       y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
+       return y;
+#endif
+}
+
+
+uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x)
+{
+#if defined(__BIG_ENDIAN__)
+       return x;
+#else
+       uint32_t y;
+       y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
+           ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
+       return y;
+#endif
+}
+
+uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x)
+{
+#if defined(__BIG_ENDIAN__)
+       return x;
+#else
+       uint16_t y;
+       y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
+       return y;
+#endif
+}
+
+uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x)
+{
+#if defined(__BIG_ENDIAN__)
+       return x;
+#else
+       uint32_t y;
+       y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
+           ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
+       return y;
+#endif
+}
+
+uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x)
+{
+#if defined(__BIG_ENDIAN__)
+       return x;
+#else
+       uint16_t y;
+       y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
+       return y;
+#endif
+}
+
 #ifndef _MSC_VER
 int DLL_PREFIX max(int a, int b)
 {
@@ -84,6 +154,26 @@ unsigned DLL_PREFIX int max(unsigned int a, unsigned int b)
        }
 }
 
+unsigned DLL_PREFIX int max(unsigned int a, int b)
+{
+       if(b < 0) return a;
+       if(a > (unsigned int)b) {
+               return a;
+       } else {
+               return b;
+       }
+}
+
+unsigned DLL_PREFIX int max(int a, unsigned int b)
+{
+       if(a < 0) return b;
+       if((unsigned int)a > b) {
+               return a;
+       } else {
+               return b;
+       }
+}
+
 int DLL_PREFIX min(int a, int b)
 {
        if(a < b) {
@@ -93,6 +183,30 @@ int DLL_PREFIX min(int a, int b)
        }
 }
 
+int DLL_PREFIX min(unsigned int a, int b)
+{
+       if(b < 0) return b;
+       if(a > INT_MAX) return b;
+       
+       if((int)a < b) {
+               return (int)a;
+       } else {
+               return b;
+       }
+}
+
+int DLL_PREFIX min(int a, unsigned int b)
+{
+       if(a < 0) return a;
+       if(b > INT_MAX) return a;
+       
+       if(a < (int)b) {
+               return a;
+       } else {
+               return (int)b;
+       }
+}
+
 unsigned int DLL_PREFIX min(unsigned int a, unsigned int b)
 {
        if(a < b) {
@@ -1494,3 +1608,409 @@ const _TCHAR *DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCH
        }
        return name[output_index];
 }
+
+// Use this before writing wav_data.
+bool DLL_PREFIX write_dummy_wav_header(void *__fio)
+{
+       if(__fio == NULL) return false;
+
+       FILEIO *fio = (FILEIO *)__fio;
+       uint8_t dummy[sizeof(wav_header_t) + sizeof(wav_chunk_t)];
+
+       if(!fio->IsOpened()) return false;
+       
+       memset(dummy, 0, sizeof(dummy));
+       fio->Fwrite(dummy, sizeof(dummy), 1);
+       return true;
+}
+// Use this after writing wav_data.
+bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
+                                                          uint16_t bits, size_t file_length)
+{
+       uint32_t length = (uint32_t) file_length;
+       
+       if(header == NULL) return false;
+       if(first_chunk == NULL) return false;
+
+       pair_t __riff_chunk_size;
+       pair_t __fmt_chunk_size;
+       pair_t __wav_chunk_size;
+       pair16_t __fmt_id;
+       pair16_t __channels;
+       pair_t __sample_rate;
+       pair_t __data_speed;
+       pair16_t __block_size;
+       pair16_t __sample_bits;
+
+       __riff_chunk_size.d = length - 8;
+       __fmt_chunk_size.d = 16;
+       __fmt_id.w = 1;
+       __channels.w = channels;
+       __sample_rate.d = rate;
+       __block_size.w = (uint16_t)((channels * bits) / 8);
+       __sample_bits.w = bits;
+       __data_speed.d = rate * (uint32_t)(__block_size.w);
+
+       memcpy(&(header->riff_chunk.id), "RIFF", 4);
+       header->riff_chunk.size = __riff_chunk_size.get_4bytes_le_to();
+       
+       memcpy(&(header->wave), "WAVE", 4);
+       memcpy(&(header->fmt_chunk.id), "fmt ", 4);
+       header->fmt_chunk.size = __fmt_chunk_size.get_4bytes_le_to();
+       header->format_id = __fmt_id.get_2bytes_le_to();
+       header->channels = __channels.get_2bytes_le_to();
+       header->sample_rate = __sample_rate.get_4bytes_le_to();
+       header->data_speed =  __data_speed.get_4bytes_le_to();
+       header->block_size = __block_size.get_2bytes_le_to();
+       header->sample_bits = __sample_bits.get_2bytes_le_to();
+
+       memcpy(&(first_chunk->id), "data", 4);
+       __wav_chunk_size.d = length - sizeof(wav_header_t) - sizeof(wav_chunk_t);
+       first_chunk->size = __wav_chunk_size.get_4bytes_le_to();
+
+       return true;
+}
+// Note: buffers are allocated by this, You should free() within user class.
+bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples)
+{
+
+       if(__fio == NULL) return false;
+       if(left_buf == NULL) return false;
+       if(right_buf == NULL) return false;
+       if(rate == NULL) return false;
+       if(got_samples == NULL) return false;
+       //if((bits != 8) && (bits != 16) && (bits != 32)) return false;
+
+       FILEIO *fio = (FILEIO *)__fio;
+       if(!fio->IsOpened()) return false;
+
+       
+       int16_t *left_buffer = NULL;
+       int16_t *right_buffer = NULL;
+       size_t samples = 0;
+       uint32_t sample_rate = 0;
+       
+       wav_header_t header;
+       wav_chunk_t  chunk;
+
+       pair16_t __fmt_id;
+       pair16_t __sample_bits;
+       pair16_t __channels;
+       pair_t __sample_rate;
+       pair_t __chunk_size;
+
+       fio->Fread(&header, sizeof(header), 1);
+       __fmt_id.set_2bytes_le_from(header.format_id);
+       __sample_bits.set_2bytes_le_from(header.sample_bits);
+       __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
+       __channels.set_2bytes_le_from(header.channels);
+       __sample_rate.set_4bytes_le_from(header.sample_rate);
+
+       if((__fmt_id.w == 1) && ((__sample_bits.w == 8) || (__sample_bits.w == 16) || (__sample_bits.w == 32))) {
+               fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
+               bool is_eof = false;
+               while(1) {
+                       if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
+                               is_eof = true;
+                               break;
+                       }
+                       __chunk_size.set_4bytes_le_from(chunk.size);
+                       if(strncmp(chunk.id, "data", 4) == 0) {
+                               break;
+                       }
+                       fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
+               }
+               __chunk_size.set_4bytes_le_from(chunk.size);
+               if(is_eof) {
+                       fio->Fclose();
+                       delete fio;
+                       return false;
+               }
+               
+               samples = (size_t)(__chunk_size.d / __channels.w);
+               int16_t data_l, data_r;
+               union {
+                       int16_t s16;
+                       struct {
+                               uint8_t l, h;
+                       } b;
+               } pair16;
+               union {
+                       int32_t s32;
+                       struct {
+                               uint8_t l, h, h2, h3;
+                       } b;
+               } pair32;
+               
+               if(samples > 0) {
+                       if(__sample_bits.w == 16) {
+                               samples /= 2;
+                       } else if(__sample_bits.w == 32) {
+                               samples /= 4;
+                       }
+                       if(samples == 0) return false;
+                       sample_rate = __sample_rate.d;
+
+                       left_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
+                       right_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
+                       if(left_buffer == NULL) {
+                               if(right_buffer != NULL) free(right_buffer);
+                               return false;
+                       }
+                       if(right_buffer == NULL) {
+                               if(left_buffer != NULL) free(left_buffer);
+                               return false;
+                       }
+                       switch(__sample_bits.w) {
+                       case 8:
+                               if(__channels.sw == 1) {
+                                       for(int i = 0; i < samples; i++) {
+                                               data_l = (int16_t)(fio->FgetUint8());
+                                               data_l = (data_l - 128) * 256;
+                                               left_buffer[i] = data_l;
+                                               right_buffer[i] = data_l;
+                                       }
+                               } else if(__channels.sw == 2) {
+                                       for(int i = 0; i < samples; i++) {
+                                               data_l = (int16_t)(fio->FgetUint8());
+                                               data_l = (data_l - 128) * 256;
+                                               data_r = (int16_t)(fio->FgetUint8());
+                                               data_r = (data_r - 128) * 256;
+                                               left_buffer[i] = data_l;
+                                               right_buffer[i] = data_r;
+                                       }
+                               }
+                               break;
+                       case 16:
+                               if(__channels.sw == 1) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair16.b.l = fio->FgetUint8();
+                                               pair16.b.h = fio->FgetUint8();
+                                               data_l = pair16.s16;
+                                               
+                                               left_buffer[i] = data_l;
+                                               right_buffer[i] = data_l;
+                                       }
+                               } else if(__channels.sw == 2) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair16.b.l = fio->FgetUint8();
+                                               pair16.b.h = fio->FgetUint8();
+                                               data_l = pair16.s16;
+                                               
+                                               pair16.b.l = fio->FgetUint8();
+                                               pair16.b.h = fio->FgetUint8();
+                                               data_r = pair16.s16;
+                                               left_buffer[i] = data_l;
+                                               right_buffer[i] = data_r;
+                                       }
+                               }
+                               break;
+                       case 32:
+                               if(__channels.sw == 1) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair32.b.l = fio->FgetUint8();
+                                               pair32.b.h = fio->FgetUint8();
+                                               pair32.b.h2 = fio->FgetUint8();
+                                               pair32.b.h3 = fio->FgetUint8();
+                                               data_l = (int16_t)(pair32.s32 / 65536);
+                                               
+                                               left_buffer[i] = data_l;
+                                               right_buffer[i] = data_l;
+                                       }
+                               } else if(__channels.sw == 2) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair32.b.l = fio->FgetUint8();
+                                               pair32.b.h = fio->FgetUint8();
+                                               pair32.b.h2 = fio->FgetUint8();
+                                               pair32.b.h3 = fio->FgetUint8();
+                                               data_l = (int16_t)(pair32.s32 / 65536);
+                                               
+                                               pair32.b.l = fio->FgetUint8();
+                                               pair32.b.h = fio->FgetUint8();
+                                               pair32.b.h2 = fio->FgetUint8();
+                                               pair32.b.h3 = fio->FgetUint8();
+                                               data_r = (int16_t)(pair32.s32 / 65536);
+                                               
+                                               left_buffer[i] = data_l;
+                                               right_buffer[i] = data_r;
+                                       }
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+               }
+       } else {
+               return false;
+       }
+       *left_buf = left_buffer;
+       *right_buf = right_buffer;
+       *rate = sample_rate;
+       *got_samples = (int)samples;
+       return true;
+}
+
+bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples)
+{
+
+       if(__fio == NULL) return false;
+       if(buffer == NULL) return false;
+       if(rate == NULL) return false;
+       if(got_samples == NULL) return false;
+       //if((bits != 8) && (bits != 16) && (bits != 32)) return false;
+
+       FILEIO *fio = (FILEIO *)__fio;
+       if(!fio->IsOpened()) return false;
+
+       
+       int16_t *left_buffer = NULL;
+       size_t samples = 0;
+       uint32_t sample_rate = 0;
+       
+       wav_header_t header;
+       wav_chunk_t  chunk;
+
+       pair16_t __fmt_id;
+       pair16_t __sample_bits;
+       pair16_t __channels;
+       pair_t __sample_rate;
+       pair_t __chunk_size;
+
+       fio->Fread(&header, sizeof(header), 1);
+       __fmt_id.set_2bytes_le_from(header.format_id);
+       __sample_bits.set_2bytes_le_from(header.sample_bits);
+       __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
+       __channels.set_2bytes_le_from(header.channels);
+       __sample_rate.set_4bytes_le_from(header.sample_rate);
+
+       if((__fmt_id.w == 1) && ((__sample_bits.w == 8) || (__sample_bits.w == 16) || (__sample_bits.w == 32))) {
+               fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
+               bool is_eof = false;
+               while(1) {
+                       if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
+                               is_eof = true;
+                               break;
+                       }
+                       __chunk_size.set_4bytes_le_from(chunk.size);
+                       if(strncmp(chunk.id, "data", 4) == 0) {
+                               break;
+                       }
+                       fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
+               }
+               __chunk_size.set_4bytes_le_from(chunk.size);
+               if(is_eof) {
+                       fio->Fclose();
+                       delete fio;
+                       return false;
+               }
+               
+               samples = (size_t)(__chunk_size.d / __channels.w);
+               int16_t data_l, data_r;
+               int32_t data32_l, data32_r;
+               union {
+                       int16_t s16;
+                       struct {
+                               uint8_t l, h;
+                       } b;
+               } pair16;
+               union {
+                       int32_t s32;
+                       struct {
+                               uint8_t l, h, h2, h3;
+                       } b;
+               } pair32;
+               
+               if(samples > 0) {
+                       if(__sample_bits.w == 16) {
+                               samples /= 2;
+                       } else if(__sample_bits.w == 32) {
+                               samples /= 4;
+                       }
+                       if(samples == 0) return false;
+                       sample_rate = __sample_rate.d;
+
+                       left_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
+                       if(left_buffer == NULL) {
+                               return false;
+                       }
+                       switch(__sample_bits.w) {
+                       case 8:
+                               if(__channels.sw == 1) {
+                                       for(int i = 0; i < samples; i++) {
+                                               data_l = (int16_t)(fio->FgetUint8());
+                                               data_l = (data_l - 128) * 256;
+                                               left_buffer[i] = data_l;
+                                       }
+                               } else if(__channels.sw == 2) {
+                                       for(int i = 0; i < samples; i++) {
+                                               data_l = (int16_t)(fio->FgetUint8());
+                                               data_l = (data_l - 128) * 256;
+                                               data_r = (int16_t)(fio->FgetUint8());
+                                               data_r = (data_r - 128) * 256;
+                                               left_buffer[i] = (data_l + data_r) / 2;
+                                       }
+                               }
+                               break;
+                       case 16:
+                               if(__channels.sw == 1) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair16.b.l = fio->FgetUint8();
+                                               pair16.b.h = fio->FgetUint8();
+                                               data_l = pair16.s16;
+                                               
+                                               left_buffer[i] = data_l;
+                                       }
+                               } else if(__channels.sw == 2) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair16.b.l = fio->FgetUint8();
+                                               pair16.b.h = fio->FgetUint8();
+                                               data_l = pair16.s16;
+                                               
+                                               pair16.b.l = fio->FgetUint8();
+                                               pair16.b.h = fio->FgetUint8();
+                                               data_r = pair16.s16;
+                                               left_buffer[i] = (data_l + data_r) / 2;
+                                       }
+                               }
+                               break;
+                       case 32:
+                               if(__channels.sw == 1) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair32.b.l = fio->FgetUint8();
+                                               pair32.b.h = fio->FgetUint8();
+                                               pair32.b.h2 = fio->FgetUint8();
+                                               pair32.b.h3 = fio->FgetUint8();
+                                               data_l = (int16_t)(pair32.s32 / 65536);
+                                               
+                                               left_buffer[i] = data_l;
+                                       }
+                               } else if(__channels.sw == 2) {
+                                       for(int i = 0; i < samples; i++) {
+                                               pair32.b.l = fio->FgetUint8();
+                                               pair32.b.h = fio->FgetUint8();
+                                               pair32.b.h2 = fio->FgetUint8();
+                                               pair32.b.h3 = fio->FgetUint8();
+                                               data32_l = pair32.s32 / 65536;
+                                               
+                                               pair32.b.l = fio->FgetUint8();
+                                               pair32.b.h = fio->FgetUint8();
+                                               pair32.b.h2 = fio->FgetUint8();
+                                               pair32.b.h3 = fio->FgetUint8();
+                                               data32_r = pair32.s32 / 65536;
+                                               
+                                               left_buffer[i] = (int16_t)((data32_l + data32_r) / 2);
+                                       }
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+               }
+       } else {
+               return false;
+       }
+       *buffer = left_buffer;
+       *rate = sample_rate;
+       *got_samples = (int)samples;
+       return true;
+}
index 07abfb3..2a4063c 100644 (file)
@@ -782,14 +782,24 @@ typedef union {
 
 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
+uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x);
+uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x);
 
+uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x);
+uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x);
+uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x);
+uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x);
 // max/min
 #ifndef _MSC_VER
        #undef max
        #undef min
        int DLL_PREFIX max(int a, int b);
+       unsigned int DLL_PREFIX max(int a, unsigned int b);
+       unsigned int DLL_PREFIX max(unsigned int a, int b);
        unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
        int DLL_PREFIX min(int a, int b);
+       int DLL_PREFIX min(unsigned int a, int b);
+       int DLL_PREFIX min(int a, unsigned int b);
        unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
 #endif
 
@@ -1065,6 +1075,31 @@ typedef struct {
 } wav_header_t;
 #pragma pack()
 
+//  See http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html.
+#pragma pack(1)
+typedef struct {
+       wav_chunk_t riff_chunk;
+       char wave[4];
+       wav_chunk_t fmt_chunk;
+       uint16_t format_id;
+       uint16_t channels;
+       uint32_t sample_rate;
+       uint32_t data_speed;
+       uint16_t block_size;
+       uint16_t sample_bits;
+       uint16_t cbsize; // Extension size.Normaly set to 0.
+       wav_chunk_t fact_chunk; // "fact", 4.
+} wav_header_float_t;
+#pragma pack()
+
+// Use this before writing wav_data.
+bool DLL_PREFIX write_dummy_wav_header(void *__fio);
+// Use this after writng wav_data.
+bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
+                                                          uint16_t bits, size_t file_length);
+bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples);
+bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples);
+
 // file path
 const _TCHAR *DLL_PREFIX get_application_path();
 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
index f0bad2c..54ae65b 100644 (file)
@@ -352,12 +352,7 @@ void OSD_BASE::start_record_sound()
                rec_sound_fio = new FILEIO();
                if(rec_sound_fio->Fopen(bios_path(sound_file_name), FILEIO_WRITE_BINARY)) {
                        // write dummy wave header
-                       wav_header_t wav_header;
-                       wav_chunk_t wav_chunk;
-                       memset(&wav_header, 0, sizeof(wav_header));
-                       memset(&wav_chunk, 0, sizeof(wav_chunk));
-                       rec_sound_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
-                       rec_sound_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+                       write_dummy_wav_header((void *)rec_sound_fio);
                        
                        rec_sound_bytes = 0;
                        rec_sound_buffer_ptr = 0;
@@ -381,6 +376,7 @@ void OSD_BASE::stop_record_sound()
                        // update wave header
                        wav_header_t wav_header;
                        wav_chunk_t wav_chunk;
+#if 0
                        pair16_t tmpval16;
                        pair_t tmpval32;
                        
@@ -417,7 +413,14 @@ void OSD_BASE::stop_record_sound()
 
                        tmpval32.d = rec_sound_bytes;
                        wav_chunk.size = tmpval32.get_4bytes_le_to();
-
+#else
+                       if(!set_wav_header(&wav_header, &wav_chunk, 2, snd_spec_presented.freq, 16,
+                                                        (size_t)(rec_sound_bytes + sizeof(wav_header) + sizeof(wav_chunk)))) {
+                               delete rec_sound_fio;
+                               now_record_sound = false;
+                               return;
+                       }
+#endif
                        rec_sound_fio->Fseek(0, FILEIO_SEEK_SET);
                        rec_sound_fio->Fwrite(&wav_header, sizeof(wav_header_t), 1);
                        rec_sound_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
index e8c9017..7f128da 100644 (file)
@@ -596,9 +596,7 @@ bool DATAREC::rec_tape(const _TCHAR* file_path)
                
                if(check_file_extension(file_path, _T(".wav"))) {
                        // write wave header
-                       uint8_t dummy[sizeof(wav_header_t) + sizeof(wav_chunk_t)];
-                       memset(dummy, 0, sizeof(dummy));
-                       rec_fio->Fwrite(dummy, sizeof(dummy), 1);
+                       write_dummy_wav_header((void *)rec_fio);
                        is_wav = true;
                } else if(check_file_extension(file_path, _T(".tap"))) {
                        // write frequency
@@ -1035,6 +1033,7 @@ void DATAREC::save_wav_image()
        
        wav_header_t wav_header;
        wav_chunk_t wav_chunk;
+#if 0
        pair_t __riff_chunk_size;
        pair_t __fmt_chunk_size;
        pair_t __wav_chunk_size;
@@ -1051,7 +1050,6 @@ void DATAREC::save_wav_image()
        __sample_rate.d = sample_rate;
        __block_size.w = 1;
        __sample_bits.w = 8;
-       
        memcpy(wav_header.riff_chunk.id, "RIFF", 4);
        wav_header.riff_chunk.size = __riff_chunk_size.get_4bytes_le_to();
        
@@ -1068,10 +1066,17 @@ void DATAREC::save_wav_image()
        memcpy(wav_chunk.id, "data", 4);
        __wav_chunk_size.d = length - sizeof(wav_header) - sizeof(wav_chunk);
        wav_chunk.size = __wav_chunk_size.get_4bytes_le_to();
-       
+
        rec_fio->Fseek(0, FILEIO_SEEK_SET);
        rec_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
        rec_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+#else
+       if(set_wav_header(&wav_header, &wav_chunk, 1, sample_rate, 8, length)) {
+               rec_fio->Fseek(0, FILEIO_SEEK_SET);
+               rec_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
+               rec_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+       }
+#endif
 }
 
 // FUJITSU FM-7 series tape image
index 99162a6..a32beae 100644 (file)
@@ -1441,7 +1441,7 @@ void DISK::trim_buffer()
        file_size.write_4bytes_le_to(tmp_buffer + 0x1c);
        
        memset(buffer, 0, sizeof(buffer));
-       memcpy(buffer, tmp_buffer, min(sizeof(buffer), file_size.d));
+       memcpy(buffer, tmp_buffer, min((unsigned int)sizeof(buffer), file_size.d));
 }
 
 int DISK::get_max_tracks()
index d6335ba..0112ea0 100644 (file)
@@ -1593,7 +1593,7 @@ bool OPNA::LoadRhythmSample(const _TCHAR* path)
                
                uint8 subchunkname[4];
                bool is_eof = false;
-               file.Fseek(EndianToLittle_DWORD(whdr.fmt_chunk.size) - 16, FILEIO_SEEK_CUR);
+               file.Fseek(EndianFromLittle_DWORD(whdr.fmt_chunk.size) - 16, FILEIO_SEEK_CUR);
                while(1) 
                {
                        if(file.Fread(&chunk, sizeof(chunk), 1) != 1) {
@@ -1603,7 +1603,7 @@ bool OPNA::LoadRhythmSample(const _TCHAR* path)
                        if(strncmp(chunk.id, "data", 4) == 0) {
                                        break;
                        }
-                       file.Fseek(EndianToLittle_DWORD(chunk.size), FILEIO_SEEK_CUR);
+                       file.Fseek(EndianFromLittle_DWORD(chunk.size), FILEIO_SEEK_CUR);
                }
                if(is_eof) {
 //                     fsize = 8192;
@@ -1616,10 +1616,10 @@ bool OPNA::LoadRhythmSample(const _TCHAR* path)
                        file.Fclose();
                        break;
                }
-               fsize = EndianToLittle_DWORD(chunk.size);
+               fsize = EndianFromLittle_DWORD(chunk.size);
                
                fsize /= 2;
-               if ((fsize >= 0x100000) || (EndianToLittle_WORD(whdr.format_id) != 1) || (EndianToLittle_WORD(whdr.channels) != 1))
+               if ((fsize >= 0x100000) || (EndianFromLittle_WORD(whdr.format_id) != 1) || (EndianFromLittle_WORD(whdr.channels) != 1))
                        break;
                fsize = Max(fsize, (1<<31)/1024);
                
@@ -1640,7 +1640,7 @@ bool OPNA::LoadRhythmSample(const _TCHAR* path)
                }
                //file.Fread(rhythm[i].sample, fsize * 2, 1);
                
-               rhythm[i].rate = EndianToLittle_DWORD(whdr.sample_rate);
+               rhythm[i].rate = EndianFromLittle_DWORD(whdr.sample_rate);
                rhythm[i].step = rhythm[i].rate * 1024 / rate;
                rhythm[i].pos = rhythm[i].size = fsize * 1024;
                file.Fclose();
index c3183a9..a3a57b4 100644 (file)
@@ -484,9 +484,7 @@ void IO::rec_tape(const _TCHAR* file_path)
        if(cmt_fio->Fopen(file_path, FILEIO_READ_WRITE_NEW_BINARY)) {
                my_tcscpy_s(cmt_rec_file_path, _MAX_PATH, file_path);
                if(check_file_extension(file_path, _T(".wav"))) {
-                       uint8_t dummy[sizeof(wav_header_t) + sizeof(wav_chunk_t)];
-                       memset(dummy, 0, sizeof(dummy));
-                       cmt_fio->Fwrite(dummy, sizeof(dummy), 1);
+                       write_dummy_wav_header((void *)cmt_fio);
                        cmt_is_wav = true;
                }
                cmt_bufcnt = 0;
@@ -507,7 +505,7 @@ void IO::close_tape()
                                
                                wav_header_t wav_header;
                                wav_chunk_t wav_chunk;
-                               
+#if 0                          
                                pair_t __riff_chunk_size;
                                pair_t __fmt_chunk_size;
                                pair_t __wav_chunk_size;
@@ -545,6 +543,13 @@ void IO::close_tape()
                                cmt_fio->Fseek(0, FILEIO_SEEK_SET);
                                cmt_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
                                cmt_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+#else
+                               if(set_wav_header(&wav_header, &wav_chunk, 1, CMT_SAMPLE_RATE, 8, length)) {
+                                       cmt_fio->Fseek(0, FILEIO_SEEK_SET);
+                                       cmt_fio->Fwrite(&wav_header, sizeof(wav_header), 1);
+                                       cmt_fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+                               }
+#endif
                        }
                }
                cmt_fio->Fclose();
index d7f9671..dafe41b 100644 (file)
@@ -77,6 +77,7 @@ bool NOISE::load_wav_file(const _TCHAR *file_name)
        bool result = false;
        
        if(fio->Fopen(create_local_path(file_name), FILEIO_READ_BINARY)) {
+#if 0          
                wav_header_t header;
                wav_chunk_t chunk;
                pair16_t __fmt_id;
@@ -148,6 +149,9 @@ bool NOISE::load_wav_file(const _TCHAR *file_name)
                                result = true;
                        }
                }
+#else
+               result = load_wav_to_stereo((void *)fio, &buffer_l, &buffer_r, &sample_rate, &samples);
+#endif
                fio->Fclose();
        }
        delete fio;
index f58314d..3f07e40 100644 (file)
@@ -815,7 +815,7 @@ void PSUB::close_tape()
                                }
 #endif
                                uint32_t length = fio->Ftell();
-                               
+#if 0                          
                                pair_t __riff_chunk_size;
                                pair_t __fmt_chunk_size;
                                pair_t __wav_chunk_size;
@@ -853,6 +853,13 @@ void PSUB::close_tape()
                                fio->Fseek(0, FILEIO_SEEK_SET);
                                fio->Fwrite(&wav_header, sizeof(wav_header), 1);
                                fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+#else
+                               if(set_wav_header(&wav_header, &wav_chunk, 1, sample_rate, 8, length)) {
+                                       fio->Fseek(0, FILEIO_SEEK_SET);
+                                       fio->Fwrite(&wav_header, sizeof(wav_header), 1);
+                                       fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+                               }
+#endif
                        } else {
                                fio->Fwrite(CasData, CasIndex, 1);
                                if(is_p6t) {
index f6a37da..35f664c 100644 (file)
@@ -291,7 +291,7 @@ void SUB::close_tape()
                                }
 #endif
                                uint32_t length = fio->Ftell();
-                               
+#if 0                          
                                pair_t __riff_chunk_size;
                                pair_t __fmt_chunk_size;
                                pair_t __wav_chunk_size;
@@ -329,6 +329,13 @@ void SUB::close_tape()
                                fio->Fseek(0, FILEIO_SEEK_SET);
                                fio->Fwrite(&wav_header, sizeof(wav_header), 1);
                                fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+#else
+                               if(set_wav_header(&wav_header, &wav_chunk, 1, sample_rate, 8, length)) {
+                                       fio->Fseek(0, FILEIO_SEEK_SET);
+                                       fio->Fwrite(&wav_header, sizeof(wav_header), 1);
+                                       fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+                               }
+#endif
                        } else {
                                fio->Fwrite(buffer, index, 1);
                                if(is_p6t) {
index 1fcf514..c6f91fb 100644 (file)
@@ -108,9 +108,7 @@ void CMT::rec_tape(const _TCHAR* file_path)
        if(fio->Fopen(file_path, FILEIO_WRITE_BINARY)) {
                my_tcscpy_s(rec_file_path, _MAX_PATH, file_path);
                if(check_file_extension(file_path, _T(".wav"))) {
-                       uint8_t dummy[sizeof(wav_header_t) + sizeof(wav_chunk_t)];
-                       memset(dummy, 0, sizeof(dummy));
-                       fio->Fwrite(dummy, sizeof(dummy), 1);
+                       write_dummy_wav_header((void *)fio);
                        is_wav = true;
                }
                bufcnt = 0;
@@ -127,9 +125,9 @@ void CMT::close_tape()
                }
                if(is_wav) {
                        uint32_t length = fio->Ftell();
-                       
                        wav_header_t wav_header;
                        wav_chunk_t wav_chunk;
+#if 0                  
                        
                        pair_t __riff_chunk_size;
                        pair_t __fmt_chunk_size;
@@ -169,6 +167,13 @@ void CMT::close_tape()
                        fio->Fseek(0, FILEIO_SEEK_SET);
                        fio->Fwrite(&wav_header, sizeof(wav_header), 1);
                        fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+#else
+                       if(set_wav_header(&wav_header, &wav_chunk, 1, SAMPLE_RATE, 8, length)) {
+                               fio->Fseek(0, FILEIO_SEEK_SET);
+                               fio->Fwrite(&wav_header, sizeof(wav_header), 1);
+                               fio->Fwrite(&wav_chunk, sizeof(wav_chunk), 1);
+                       }
+#endif
                }
                fio->Fclose();
        }