Example #1
0
        public int __sceSasCore(uint SasCorePointer, StereoShortSoundSample* SasOut)
        {
            var SasCore = GetSasCore(SasCorePointer);

            if (SasCore.OutputMode != OutputMode.PSP_SAS_OUTPUTMODE_STEREO)
            {
                Logger.Unimplemented("SasCore.OutputMode != OutputMode.PSP_SAS_OUTPUTMODE_STEREO");
            }

            int NumberOfChannels = SasCore.OutputMode == OutputMode.PSP_SAS_OUTPUTMODE_STEREO ? 2 : 1;
            int NumberOfSamples = SasCore.GrainSamples;

            for (int n = 0; n < NumberOfSamples; n++)
            {
                BufferTemp[n] = default(StereoIntSoundSample);
                VoiceOnCount[n] = 0;
            }

            // Read and mix voices.
            foreach (var Voice in SasCore.Voices)
            {
                if (Voice.OnAndPlaying)
                {
                    for (int n = 0; n < NumberOfSamples; n++)
                    {
                        if (Voice.SampleOffset < Voice.Vag.SamplesCount)
                        {
                            VoiceOnCount[n]++;
                            BufferTemp[n] += Voice.Vag.GetSampleAt(Voice.SampleOffset++);
                        }
                        else
                        {
                            Voice.SetPlaying(false);
                            break;
                        }
                    }
                }
            }

            // Normalize output
            for (int n = 0; n < NumberOfSamples; n++)
            {
                if (VoiceOnCount[n] > 0)
                {
                    BufferShort[n] = (BufferTemp[n] / VoiceOnCount[n]);
                }
                else
                {
                    BufferShort[n] = default(StereoShortSoundSample);
                }
            }

            // Output converted 44100 data
            for (int n = 0; n < NumberOfSamples; n++)
            {
                SasOut[n] = BufferShort[n];
            }
            //throw(new NotImplementedException());
            return 0;
        }
Example #2
0
        public void WriteWave(Stream Stream, StereoShortSoundSample[] Samples)
        {
            this.Stream = Stream;
            this.BinaryWriter = new BinaryWriter(Stream);

            WriteChunk("RIFF", () =>
            {
                Stream.WriteStringz("WAVE", 4, Encoding.ASCII);
                WriteChunk("fmt ", () =>
                {
                    Stream.WriteStruct(new WaveFormat()
                    {
                        CompressionCode = 1,
                        SampleRate = 44100,
                        NumberOfChannels = 2,
                        BytesPerSecond = 44100 * sizeof(short) * 2,
                        BlockAlignment = sizeof(short) * 2,
                        BitsPerSample = 16,
                        Padding = 0,
                    });
                });
                WriteChunk("data", () =>
                {
                    BinaryWriter.Write(PointerUtils.ArrayToByteArray(Samples));
                    /*
                    foreach (var Sample in Samples)
                    {
                        BinaryWriter.Write(Sample.Left);
                        BinaryWriter.Write(Sample.Right);
                    }
                    */
                });
            });
        }
Example #3
0
 public void WriteWave(String FileName, StereoShortSoundSample[] Samples)
 {
     using (var Stream = File.Open(FileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
     {
         WriteWave(Stream, Samples);
     }
 }
Example #4
0
 /*
 static public short[] Convert(short[] Input, int InputRate, int InputChannels, int OutputRate, int OutputChannels)
 {
     var Output = new short[(Input.Length * InputRate * InputChannels) / (OutputRate * OutputChannels)];
     if (InputRate != OutputRate)
 }
 */
 public static StereoShortSoundSample[] Convert_Mono22050_Stereo44100(short[] Input)
 {
     var Output = new StereoShortSoundSample[Input.Length * 2];
     int m = 0;
     for (int n = 0; n < Input.Length; n++)
     {
         //Input[n]
         short Sample1 = Input[n];
         Output[m++].MonoLeftRight = Sample1;
         //Output[m++] = Sample1;
         if (n < Input.Length - 1)
         {
             short Sample2 = Input[n + 1];
             Output[m++].MonoLeftRight = (short)((Sample1 + Sample2) / 2);
             //Output[m++] = (short)((Sample1 + Sample2) / 2);
         }
         else
         {
         }
     }
     return Output;
 }
Example #5
0
		//[HlePspNotImplemented]
		public int __sceSasCoreWithMix(uint SasCorePointer, StereoShortSoundSample* SasInOut, int LeftVolume, int RightVolume)
		{
#if false
			var SasCore = GetSasCore(SasCorePointer);
			int NumberOfChannels = SasCore.OutputMode == OutputMode.PSP_SAS_OUTPUTMODE_STEREO ? 2 : 1;
			int NumberOfSamples = SasCore.GrainSamples * NumberOfChannels;

			fixed (short* FixedMixBufferShort = MixBufferShort)
			{
				__sceSasCore(SasCorePointer, FixedMixBufferShort);
			}

			int MaxVolume = 0x1000;

			int LeftVolumeComp = MaxVolume - LeftVolume;
			int RightVolumeComp = MaxVolume - RightVolume;

			for (int n = 0; n < NumberOfSamples; n += 2)
			{
				SasInOut[n + 0] = (short)(((int)SasInOut[n + 0] * LeftVolumeComp + (int)MixBufferShort[n + 0] * LeftVolume) * short.MaxValue / MaxVolume);
				SasInOut[n + 1] = (short)(((int)SasInOut[n + 1] * RightVolumeComp + (int)MixBufferShort[n + 1] * RightVolume) * short.MaxValue / MaxVolume);
			}

			//throw (new NotImplementedException());
			return 0;
#else
			return __sceSasCore(SasCorePointer, SasInOut);
#endif
		}
Example #6
0
		public int sceAtracDecodeData(int AtracId, StereoShortSoundSample* SamplesOut, out int DecodedSamples, out int ReachedEnd, out int RemainingFramesToDecode)
		{
			var Atrac = AtracList.Get(AtracId);
			
			// Decode
			DecodedSamples = Atrac.Decode(SamplesOut);

			//Console.WriteLine("{0}/{1} -> {2} : {3}", Atrac.DecodingOffsetInSamples, Atrac.TotalSamples, DecodedSamples, Atrac.DecodingReachedEnd);
			
			RemainingFramesToDecode = -1;

			if (Atrac.DecodingReachedEnd)
			{
				if (Atrac.NumberOfLoops == 0)
				{
					ReachedEnd = -1;
					throw (new SceKernelException(SceKernelErrors.ERROR_ATRAC_ALL_DATA_DECODED));
				}
				if (Atrac.NumberOfLoops > 0) Atrac.NumberOfLoops--;
				Atrac.DecodingOffset = 0;
			}

			ReachedEnd = 0;
			return 0;
		}
Example #7
0
			//List<short> Temp = new List<short>();
			public int Decode(StereoShortSoundSample* SamplesOut)
			{
				//int Channels = 2;

				//ToReadSamples /= 2;

				int StartDecodingOffset = DecodingOffset;

				for (int n = 0; n < MaximumSamples; n++)
				{
					if (DecodingReachedEnd)
					{
						break;
					}
					SamplesOut[n] = DecodedSamples[DecodingOffset];
					DecodingOffset++;
				}
				/*
				if (Temp.Count > 90000)
				{
					new WaveStream().WriteWave(@"c:\temp\4.wav", Temp.ToArray());
					Console.ReadKey();
				}
				*/
				return DecodingOffset - StartDecodingOffset;
			}
Example #8
0
 public static StereoShortSoundSample Mix(StereoShortSoundSample A, StereoShortSoundSample B)
 {
     return new StereoShortSoundSample(Clamp((A.Left + B.Left) / 2), Clamp((A.Right + B.Right) / 2));
 }
Example #9
0
 public static IEnumerable<StereoShortSoundSample> DecodeBlocksStream(Block[] Blocks)
 {
     var DecodedBlock = new short[28];
     var Decoder = new Decoder();
     int SamplesOffset = 0;
     bool Ended = false;
     var LastSample = default(StereoShortSoundSample);
     var CurrentSample = default(StereoShortSoundSample);
     for (int n = 0; !Ended && (n < Blocks.Length); n++)
     {
         var CurrentBlock = Blocks[n];
         SamplesOffset = 0;
         Decoder.DecodeBlock(CurrentBlock, DecodedBlock, ref SamplesOffset);
         switch (CurrentBlock.Type)
         {
             case Block.TypeEnum.None:
                 // 16 bytes = 56 stereo 44100 samples
                 foreach (var DecodedMonoSample in DecodedBlock)
                 {
                     CurrentSample = new StereoShortSoundSample(DecodedMonoSample, DecodedMonoSample);
                     yield return StereoShortSoundSample.Mix(LastSample, CurrentSample);
                     yield return CurrentSample;
                     LastSample = CurrentSample;
                 }
                 break;
             case Block.TypeEnum.End:
                 Ended = true;
                 break;
         }
     }
 }
Example #10
0
 public int sceAtracDecodeData(Atrac Atrac, StereoShortSoundSample* SamplesOut, [HleInvalidAsInvalidPointer] out int DecodedSamples, [HleInvalidAsInvalidPointer] out int ReachedEnd, [HleInvalidAsInvalidPointer] out int RemainingFramesToDecode)
 {
     return _sceAtracDecodeData(Atrac, SamplesOut, out DecodedSamples, out ReachedEnd, out RemainingFramesToDecode);
 }
Example #11
0
            /// <summary>
            /// 
            /// </summary>
            /// <param name="SamplesOut"></param>
            /// <returns></returns>
            public int Decode(StereoShortSoundSample* SamplesOut)
            {
                if (SamplesOut == null) return 0;

                //Console.Error.WriteLine("Decode");
                try
                {
                    //int Channels = 2;

                    //ToReadSamples /= 2;

                    var BlockSize = this.Format.BlockSize;
                    //this.Data
                    int channels;
                    short[] buf;

                    int rc;

                    if (BlockSize <= 0)
                    {
                        Console.WriteLine("BlockSize <= 0");
                        return -1;
                    }

                    if (this.DataStream.Available() < BlockSize)
                    {
                        Console.WriteLine("EndOfData {0} < {1} : {2}, {3}", this.DataStream.Available(), BlockSize, DecodingOffset, EndSample);
                        return 0;
                    }

                    var Data = new byte[BlockSize];
                    this.DataStream.Read(Data, 0, Data.Length);

                    fixed (byte* DataPtr = Data)
                    {
                        if ((rc = this.MaiAT3PlusFrameDecoder.decodeFrame(DataPtr, BlockSize, out channels, out buf)) != 0)
                        {
                            Console.WriteLine("MaiAT3PlusFrameDecoder.decodeFrame: {0}", rc);
                            return 0;
                        }

                        int DecodedSamples = this.MaximumSamples;
                        int DecodedSamplesChannels = DecodedSamples * channels;
                        _DecodingOffset += DecodedSamples;

                        fixed (short* buf_ptr = buf)
                        {
                            for (int n = 0; n < DecodedSamplesChannels; n += channels)
                            {
                                SamplesOut->Left = buf_ptr[n + 0];
                                SamplesOut->Right = buf_ptr[n + 1];
                                SamplesOut++;
                            }
                        }

                        return DecodedSamples;
                    }
                }
                catch
                {
                    Console.Error.WriteLine("Error Atrac3.Decode");
                    return 0;
                }
            }
Example #12
0
        private int _sceAtracDecodeData(Atrac Atrac, StereoShortSoundSample* SamplesOut, out int DecodedSamples, out int ReachedEnd, out int RemainingFramesToDecode)
        {
            // Decode
            DecodedSamples = Atrac.Decode(SamplesOut);
            ReachedEnd = 0;
            RemainingFramesToDecode = Atrac.RemainingFrames;

            //Console.WriteLine("{0}/{1} -> {2} : {3}", Atrac.DecodingOffsetInSamples, Atrac.TotalSamples, DecodedSamples, Atrac.DecodingReachedEnd);

            if (Atrac.DecodingReachedEnd)
            {
                if (Atrac.NumberOfLoops == 0)
                {
                    DecodedSamples = 0;
                    ReachedEnd = 1;
                    RemainingFramesToDecode = 0;
                    Console.WriteLine("SceKernelErrors.ERROR_ATRAC_ALL_DATA_DECODED)");
                    throw (new SceKernelException(SceKernelErrors.ERROR_ATRAC_ALL_DATA_DECODED));
                }
                if (Atrac.NumberOfLoops > 0) Atrac.NumberOfLoops--;

                Atrac.DecodingOffset = (Atrac.LoopInfoList.Length > 0) ? Atrac.LoopInfoList[0].StartSample : 0;
            }

            //return Atrac.GetUidIndex(InjectContext);
            return 0;
        }
Example #13
0
 public StereoShortSoundSample[] GetAllDecodedSamples()
 {
     var Samples = new StereoShortSoundSample[SamplesCount];
     SamplesDecoder.Reset();
     for (int n = 0; n < SamplesCount; n++)
     {
         Samples[n] = SamplesDecoder.GetNextSample();
     }
     return Samples;
 }