Advertisement
e4ch

Program.cs

May 11th, 2019
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.28 KB | None | 0 0
  1. ?using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         const int offsetstart = 0x2c;
  8.         const int expecteddatasize = 4032000; //4032000; // original task:4032000, Assignment1=2293200
  9.         const int numsamples = expecteddatasize >> 2; // 4 bytes per sample (2 left, 2 right)
  10.  
  11.         struct sampleLR
  12.         {
  13.             public Int16 left;
  14.             public Int16 right;
  15.         };
  16.  
  17.         static void FindText(int type, sampleLR[] s, int startoffset, int distance)
  18.         {
  19.             Console.WriteLine();
  20.             Console.WriteLine("FindText-type=" + type.ToString() + ", offset=" + startoffset.ToString() + ", distance=" + distance.ToString());
  21.             byte assembledbyte = 0;
  22.             int numbitsgot = 0;
  23.             for(int i = startoffset; i < numsamples; i += distance)
  24.             {
  25.                 if (i >= numsamples)
  26.                     break;
  27.                 byte l = (byte)(UInt16)((UInt16)s[i].left & (UInt16)0x01);
  28.                 byte r = (byte)(UInt16)((UInt16)s[i].right & (UInt16)0x01);
  29.                 if (type == 1)
  30.                 {
  31.                     assembledbyte = (byte)((byte)(assembledbyte << 1) | l);
  32.                     assembledbyte = (byte)((byte)(assembledbyte << 1) | r);
  33.                     numbitsgot += 2;
  34.                 }
  35.                 if (type == 2)
  36.                 {
  37.                     assembledbyte = (byte)((byte)(assembledbyte << 1) | r);
  38.                     assembledbyte = (byte)((byte)(assembledbyte << 1) | l);
  39.                     numbitsgot += 2;
  40.                 }
  41.                 if (type == 3)
  42.                 {
  43.                     assembledbyte = (byte)((byte)(assembledbyte << 1) | l);
  44.                     numbitsgot++;
  45.                 }
  46.                 if (type == 4)
  47.                 {
  48.                     assembledbyte = (byte)((byte)(assembledbyte << 1) | r);
  49.                     numbitsgot++;
  50.                 }
  51.                 if (type == 5)
  52.                 {
  53.                     assembledbyte = (byte)((byte)(assembledbyte >> 1) | (byte)(l<<7));
  54.                     assembledbyte = (byte)((byte)(assembledbyte >> 1) | (byte)(r<<7));
  55.                     numbitsgot += 2;
  56.                 }
  57.                 if (type == 6)
  58.                 {
  59.                     assembledbyte = (byte)((byte)(assembledbyte >> 1) | (byte)(r << 7));
  60.                     assembledbyte = (byte)((byte)(assembledbyte >> 1) | (byte)(l << 7));
  61.                     numbitsgot += 2;
  62.                 }
  63.                 if (type == 7)
  64.                 {
  65.                     assembledbyte = (byte)((byte)(assembledbyte >> 1) | (byte)(l << 7));
  66.                     numbitsgot++;
  67.                 }
  68.                 if (type == 8)
  69.                 {
  70.                     assembledbyte = (byte)((byte)(assembledbyte >> 1) | (byte)(r << 7));
  71.                     numbitsgot++;
  72.                 }
  73.                 if (numbitsgot > 8)
  74.                     throw new Exception("more than 8 bits!");
  75.                 if (numbitsgot >= 8)
  76.                 {
  77.                     //if(assembledbyte>=32&&assembledbyte<=126)
  78.                         Console.Write((char)assembledbyte);
  79.                     assembledbyte = 0;
  80.                     numbitsgot = 0;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         static void Main(string[] args)
  86.         {
  87.             System.IO.FileStream fs = new System.IO.FileStream("C:\\TEMP\\CTF_moocfi_2019\\SomethingSoundsOff\\woods_a.wav", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
  88.             byte[] b=new byte[expecteddatasize];
  89.             long sr = fs.Seek(offsetstart, System.IO.SeekOrigin.Begin);
  90.             int readresult=fs.Read(b, 0, expecteddatasize);
  91.             fs.Close();
  92.             if(readresult== expecteddatasize)
  93.             {
  94.                 Console.WriteLine("File read successfully.");
  95.                 sampleLR[] s = new sampleLR[numsamples];
  96.                 for(int i = 0; i < numsamples; i++)
  97.                 {
  98.                     byte b0 = b[4 * i];
  99.                     byte b1 = b[4 * i + 1];
  100.                     byte b2 = b[4 * i + 2];
  101.                     byte b3 = b[4 * i + 3];
  102.                     Int16 left = (Int16)((UInt16)b1 << 8 | (UInt16)b0);
  103.                     Int16 right= (Int16)((UInt16)b3 << 8 | (UInt16)b2);
  104.                     s[i].left = left;
  105.                     s[i].right = right;
  106.                 }
  107.                 Console.WriteLine("Copied samples successfully.");
  108.                 int startoffset = 0;
  109.                 int distance = 1;
  110.                 FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  111.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  112.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  113.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  114.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  115.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  116.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  117.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  118.                 //startoffset = 1;
  119.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  120.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  121.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  122.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  123.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  124.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  125.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  126.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  127.                 //startoffset = 2;
  128.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  129.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  130.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  131.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  132.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  133.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  134.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  135.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  136.                 //startoffset = 3;
  137.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  138.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  139.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  140.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  141.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  142.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  143.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  144.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  145.                 //startoffset = 4;
  146.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  147.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  148.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  149.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  150.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  151.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  152.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  153.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  154.                 //startoffset = 5;
  155.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  156.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  157.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  158.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  159.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  160.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  161.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  162.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  163.                 //startoffset = 6;
  164.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  165.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  166.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  167.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  168.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  169.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  170.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  171.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  172.                 //startoffset = 7;
  173.                 //FindText(1, s, startoffset, distance); // 1=left, right, bits 0...7
  174.                 //FindText(2, s, startoffset, distance); // 2=right, left, bits 0...7
  175.                 //FindText(3, s, startoffset, distance); // 3=left only, bits 0...7
  176.                 //FindText(4, s, startoffset, distance); // 3=right only, bits 0...7
  177.                 //FindText(5, s, startoffset, distance); // 1=left, right, bits 7...0
  178.                 //FindText(6, s, startoffset, distance); // 2=right, left, bits 7...0
  179.                 //FindText(7, s, startoffset, distance); // 3=left only, bits 7...0
  180.                 //FindText(8, s, startoffset, distance); // 3=right only, bits 7...0
  181.             }
  182.             else
  183.             {
  184.                 Console.WriteLine("Error reading file.");
  185.             }
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement