static void write_sampled_instrument_to_bytearray(SampledInstrument sampled_instrument, List <byte> arr) { arr.AddRange(BitConverter.GetBytes(sampled_instrument.data_1)); arr.AddRange(BitConverter.GetBytes(sampled_instrument.data_2)); arr.AddRange(BitConverter.GetBytes(sampled_instrument.data_3)); arr.AddRange(BitConverter.GetBytes(sampled_instrument.sample_size)); arr.AddRange(sampled_instrument.sample_pcm_data); }
static SampledInstrument extract_sampled_instrument(byte[] file_contents, int sampled_instrument_address) { SampledInstrument sampled_instrument = new SampledInstrument(); sampled_instrument.data_1 = read_32_bit_from_file_at_offset(file_contents, sampled_instrument_address); sampled_instrument.invalid = (read_8_bit_from_file_at_offset(file_contents, sampled_instrument_address) + read_8_bit_from_file_at_offset(file_contents, sampled_instrument_address + 1) + read_8_bit_from_file_at_offset(file_contents, sampled_instrument_address + 2)) != 0; sampled_instrument_address += 4; sampled_instrument.data_2 = read_32_bit_from_file_at_offset(file_contents, sampled_instrument_address); sampled_instrument_address += 4; sampled_instrument.data_3 = read_32_bit_from_file_at_offset(file_contents, sampled_instrument_address); sampled_instrument_address += 4; sampled_instrument.sample_size = read_32_bit_from_file_at_offset(file_contents, sampled_instrument_address); sampled_instrument_address += 4; sampled_instrument.sample_pcm_data = new List <byte>(); if (sampled_instrument.invalid) { System.Console.WriteLine("Invalid Sampled Instrument!"); sampled_instrument.sample_size = 0; return(sampled_instrument); } System.Console.WriteLine("Sample size: " + sampled_instrument.sample_size.ToString("X")); if (sampled_instrument.sample_size > SAMPLE_SIZE_LIMIT) { System.Console.WriteLine("-------------------------------------------------------------------------------------------------------"); System.Console.WriteLine("Sample size too large, capping it to " + SAMPLE_SIZE_LIMIT.ToString("X")); System.Console.WriteLine("-------------------------------------------------------------------------------------------------------"); sampled_instrument.sample_size = SAMPLE_SIZE_LIMIT; } for (int idx = 0; idx < sampled_instrument.sample_size; ++idx) { sampled_instrument.sample_pcm_data.Add(read_8_bit_from_file_at_offset(file_contents, sampled_instrument_address)); sampled_instrument_address += 1; } return(sampled_instrument); }