+
+
+/** Calculate and return the sync packets required for this edit unit (aka "frame") */
+vector<bool>
+SoundAssetWriter::create_sync_packets ()
+{
+ /* Parts of this code assumes 48kHz */
+ DCP_ASSERT (_asset->sampling_rate() == 48000);
+
+ /* Encoding of edit rate */
+ int edit_rate_code = 0;
+ /* How many 0 bits are used to pad the end of the packet */
+ int remaining_bits = 0;
+ /* How many packets in this edit unit (i.e. "frame") */
+ int packets = 0;
+ Fraction const edit_rate = _asset->edit_rate ();
+ if (edit_rate == Fraction(24, 1)) {
+ edit_rate_code = 0;
+ remaining_bits = 25;
+ packets = 4;
+ } else if (edit_rate == Fraction(25, 1)) {
+ edit_rate_code = 1;
+ remaining_bits = 20;
+ packets = 4;
+ } else if (edit_rate == Fraction(30, 1)) {
+ edit_rate_code = 2;
+ remaining_bits = 0;
+ packets = 4;
+ } else if (edit_rate == Fraction(48, 1)) {
+ edit_rate_code = 3;
+ remaining_bits = 25;
+ packets = 2;
+ } else if (edit_rate == Fraction(50, 1)) {
+ edit_rate_code = 4;
+ remaining_bits = 20;
+ packets = 2;
+ } else if (edit_rate == Fraction(60, 1)) {
+ edit_rate_code = 5;
+ remaining_bits = 0;
+ packets = 2;
+ } else if (edit_rate == Fraction(96, 1)) {
+ edit_rate_code = 6;
+ remaining_bits = 25;
+ packets = 1;
+ } else if (edit_rate == Fraction(100, 1)) {
+ edit_rate_code = 7;
+ remaining_bits = 20;
+ packets = 1;
+ } else if (edit_rate == Fraction(120, 1)) {
+ edit_rate_code = 8;
+ remaining_bits = 0;
+ packets = 1;
+ }
+
+ Bitstream bs;
+
+ Kumu::UUID id;
+ DCP_ASSERT (id.DecodeHex(_asset->id().c_str()));
+
+ for (int i = 0; i < packets; ++i) {
+ bs.write_from_byte (0x4d);
+ bs.write_from_byte (0x56);
+ bs.start_crc (0x1021);
+ bs.write_from_byte (edit_rate_code, 4);
+ bs.write_from_byte (0, 2);
+ bs.write_from_byte (_sync_packet, 2);
+ bs.write_from_byte (id.Value()[i * 4 + 0]);
+ bs.write_from_byte (id.Value()[i * 4 + 1]);
+ bs.write_from_byte (id.Value()[i * 4 + 2]);
+ bs.write_from_byte (id.Value()[i * 4 + 3]);
+ bs.write_from_word (_frames_written, 24);
+ bs.write_crc ();
+ bs.write_from_byte (0, 4);
+ bs.write_from_word (0, remaining_bits);
+
+ ++_sync_packet;
+ if (_sync_packet == 4) {
+ _sync_packet = 0;
+ }
+ }
+
+ return bs.get();
+}
+