void Node::flush() { auto registeredNum = getRegisteredObjNum(); if (registeredNum == 0) { return; } if (!m_fp) { std::string path(BasePath); #if 0 auto depth = getDepth(); auto mortonNumber = getMortonNumber(); path += "r"; uint32_t mask = 0x07 << (3 * (depth - 1)); for (uint32_t i = 1; i < depth; i++) { auto n = mortonNumber & mask; path += n; mask >>= 3; } path += ".spcd"; fopen_s(&m_fp, path.c_str(), "wb"); IZ_ASSERT(m_fp); // ヘッダー分空ける. fseek(m_fp, sizeof(m_header), SEEK_SET); m_header.magic_number = FOUR_CC('S', 'P', 'C', 'D'); m_header.version = 0; m_header.depth = depth; m_header.mortonNumber = mortonNumber; #else auto id = getId(); char tmp[10]; sprintf(tmp, "%d\0", id); path += tmp; path += ".spcd"; fopen_s(&m_fp, path.c_str(), "wb"); IZ_ASSERT(m_fp); // ヘッダー分空ける. fseek(m_fp, sizeof(m_header), SEEK_SET); m_header.magic_number = FOUR_CC('S', 'P', 'C', 'D'); m_header.version = 0; #endif }
static int read_wave_chunk(struct wave_reader *wr, wave_reader_error *error) { int result; int sub1_id, sub1_len, sub2_id, sub2_len, byte_rate, block_align; if ((result=read_int32_b(wr->fp, &sub1_id)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if (sub1_id != FOUR_CC('f','m','t',' ')) { *error = WR_BAD_CONTENT; return 0; } if ((result=read_int32_l(wr->fp, &sub1_len)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int16_l(wr->fp, &wr->format)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int16_l(wr->fp, &wr->num_channels)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int32_l(wr->fp, &wr->sample_rate)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int32_l(wr->fp, &byte_rate)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int16_l(wr->fp, &block_align)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int16_l(wr->fp, &wr->sample_bits)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if ((result=read_int32_b(wr->fp, &sub2_id)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } if (sub2_id != FOUR_CC('d','a','t','a')) { *error = WR_BAD_CONTENT; return 0; } if ((result=read_int32_l(wr->fp, &sub2_len)) != 1) { *error = result == 0 ? WR_BAD_CONTENT : WR_IO_ERROR; return 0; } wr->num_samples = sub2_len / (wr->num_channels * wr->sample_bits / 8); return 1; }
struct wave_reader * wave_reader_open(const char *filename, wave_reader_error *error) { int root_id, root_len, format_id; int continue_reading = 1; struct wave_reader *wr = NULL; assert(filename != NULL); assert(error != NULL); wr = (struct wave_reader *)calloc(1, sizeof(struct wave_reader)); if (!wr) { *error = WR_ALLOC_ERROR; goto alloc_error; } wr->fp = fopen(filename, "rb"); if (!wr->fp) { *error = WR_OPEN_ERROR; goto open_error; } if (!read_int32_b(wr->fp, &root_id)) { *error = WR_IO_ERROR; goto reading_error; } if (root_id != FOUR_CC('R','I','F','F')) { *error = WR_BAD_CONTENT; goto reading_error; } if (!read_int32_l(wr->fp, &root_len)) { *error = WR_IO_ERROR; goto reading_error; } while (continue_reading) { if (!read_int32_b(wr->fp, &format_id)) { *error = WR_IO_ERROR; goto reading_error; } switch (format_id) { case FOUR_CC('W','A','V','E'): if (!read_wave_chunk(wr, error)) { goto reading_error; } continue_reading = 0; break; default: if (!skip(wr, error)) { goto reading_error; } } } return wr; reading_error: fclose(wr->fp); open_error: free(wr); alloc_error: return NULL; }