Esempio n. 1
0
        public override int MusicDelivery(SpotifySession session, AudioFormat format, IntPtr frames, int num_frames)
        {
            if (num_frames == 0)
            {
                return(0);
            }

            var size = num_frames * format.channels * 2;
            var data = new byte[size];

            Marshal.Copy(frames, data, 0, size);

            wr.Write(data);

            if (OnDownloadProgress != null)
            {
                counter++;
                var duration = downloadingTrack.Duration();
                var process  = (int)Math.Round((double)100 / duration * (46.4 * counter), 0);
                OnDownloadProgress(process);
            }

            return(num_frames);
            // return base.MusicDelivery(session, format, frames, num_frames);
        }
Esempio n. 2
0
        public override int MusicDelivery(SpotifySession session, AudioFormat format, IntPtr frames, int num_frames)
        {
            if (num_frames == 0)
            {
                return(0);
            }

            var size = num_frames * format.channels * 2;
            var data = new byte[size];

            Marshal.Copy(frames, data, 0, size);

            wr.Write(data);

            if (OnDownloadProgress != null)
            {
                counter++;
                var duration = downloadingTrack.Duration();
                // Todo: Find out how to calculate this correctly,
                // so far 46.4 is used to calculate the process
                // but there should be a way to calculate this
                // with the given variables
                var process = (int)Math.Round((double)100 / duration * (46.4 * counter), 0);
                OnDownloadProgress(process);
            }

            return(num_frames);
            // return base.MusicDelivery(session, format, frames, num_frames);
        }
Esempio n. 3
0
        /// <summary>
        /// Converts Wav to MP3.
        /// </summary>
        /// <param name="inputFile">The input file.</param>
        /// <param name="outputPath">The output path.</param>
        /// <returns>
        /// Path of changed file
        /// </returns>
        public static string ConvertWavToMp3(string inputFile, string outputPath)
        {
            var reader = new WaveStream(inputFile);

            try
            {
                var config = new BE_CONFIG(reader.Format, (uint)(Math.Abs(BitRate - 0.0) < 0.1 ? DefaultBitRate : BitRate));
                var writer = new Mp3Writer(new FileStream(outputPath, FileMode.Create), reader.Format, config);

                try
                {
                    var buffer = new byte[writer.OptimalBufferSize];
                    int read;
                    while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        writer.Write(buffer, 0, read);
                    }
                }
                finally
                {
                    writer.Close();
                }
            }
            finally
            {
                reader.Close();
            }

            return(outputPath);
        }
Esempio n. 4
0
 public static void WriteWaveData(object sender, DataReadEventArgs ea)
 {
     if (m_Writer != null)
     {
         m_Writer.Write(ea.Data, 0, (int)ea.DataSize);
     }
 }
Esempio n. 5
0
        internal byte[] ConvertWavToMp3(Stream stream)
        {
            var inStr = new WaveStream(stream);

            try {
                var convertedStream = new MemoryStream();
                var writer          = new Mp3Writer(convertedStream, inStr.Format);
                try {
                    var buff = new byte[writer.OptimalBufferSize];
                    int read;
                    while ((read = inStr.Read(buff, 0, buff.Length)) > 0)
                    {
                        writer.Write(buff, 0, read);
                    }

                    convertedStream.Seek(0, SeekOrigin.Begin);
                    return(convertedStream.ToArray());
                } catch (Exception e) {
                    LoggerWrapper.LogTo(LoggerName.Errors).ErrorException(
                        "AudioConverter.ConvertWavToMp3 inner вылетело исключение {0}", e);
                } finally {
                    writer.Close();
                }
            } catch (Exception e) {
                LoggerWrapper.LogTo(LoggerName.Errors).ErrorException(
                    "AudioConverter.ConvertWavToMp3 outer вылетело исключение {0}", e);
            } finally {
                inStr.Close();
            }
            return(null);
        }
Esempio n. 6
0
        private static void mp3izza(string nomefile, string nomefilemp3)
        {
            WaveStream filewav = new WaveStream(nomefile);
            //      try
            //      {
            Mp3Writer salvamp3 = new Mp3Writer(new FileStream(nomefilemp3,
                                                              FileMode.Create), filewav.Format);

            //          try
            //          {
            byte[] buff = new byte[salvamp3.OptimalBufferSize];
            int    read = 0;

            while ((read = filewav.Read(buff, 0, buff.Length)) > 0)
            {
                salvamp3.Write(buff, 0, read);
            }
            //         }
            //         finally
            //         {
            salvamp3.Close();
            //         }
            //     }
            //     finally
            //     {
            filewav.Close();
            //     }
            File.Delete(nomefile);
        }
Esempio n. 7
0
        public static void ConvertWavToMp3(string wav, string mp3)
        {
            Mp3WriterConfig m_Config = null;
            WaveStream      InStr    = new WaveStream(wav);

            m_Config = new Mp3WriterConfig(InStr.Format);
            m_Config.Mp3Config.format.mp3.bOriginal = 80;

            try
            {
                Mp3Writer writer = new Mp3Writer(new FileStream(mp3, FileMode.Create), m_Config);

                try
                {
                    byte[] buff   = new byte[writer.OptimalBufferSize];
                    int    read   = 0;
                    int    actual = 0;
                    long   total  = InStr.Length;

                    try
                    {
                        while ((read = InStr.Read(buff, 0, buff.Length)) > 0)
                        {
                            writer.Write(buff, 0, read);
                            actual += read;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                InStr.Close();
            }
        }
Esempio n. 8
0
        private static void WriteFile(Mp3Writer wmaOutput, WmaStreamReader wmaInput, byte[] buffer, long stopPosition)
        {
            if (stopPosition == -1)
            {
                stopPosition = wmaInput.Length + 1;
            }
            int read;

            //Read the file until we hit our stop position, or the end of the file.
            while (wmaInput.Position < stopPosition && (read = wmaInput.Read(buffer, 0, buffer.Length)) > 0)
            {
                wmaOutput.Write(buffer, 0, read);
            }
        }
Esempio n. 9
0
        public void convert_mp3(string wavPath)
        {
            string[] wavFiles = Directory.GetFiles(wavPath, "*.wav");
            foreach (string w in wavFiles)
            {
                string sPath = Path.GetDirectoryName(w);
                string fName = Path.GetFileNameWithoutExtension(w);
                #region write mp3
                WaveStream InStr = new WaveStream(w);
                try
                {
                    Mp3Writer writer = new Mp3Writer(new FileStream(sPath + "\\" + fName + ".mp3",
                                                                    FileMode.Create), InStr.Format);
                    try
                    {
                        byte[] buff = new byte[writer.OptimalBufferSize];
                        int    read = 0;
                        while ((read = InStr.Read(buff, 0, buff.Length)) > 0)
                        {
                            writer.Write(buff, 0, read);
                        }
                    }
                    finally
                    {
                        writer.Close();
                    }
                }
                finally
                {
                    InStr.Close();
                }

                #endregion
                File.Delete(w);
            }
        }
Esempio n. 10
0
File: Form1.cs Progetto: 4dvn/yeti
 private void buttonCompress_Click(object sender, System.EventArgs e)
 {
     if (File.Exists(textBoxOutFile.Text) && (MessageBox.Show(this, "Override the existing file?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes))
     {
         return;
     }
     try
     {
         progressBar.Value = 0;
         toolTip1.SetToolTip(progressBar, "");
         this.Text   = "Audio Compress";
         Compressing = true;
         try
         {
             RefreshControls();
             WaveStream InStr = new WaveStream(textBoxInFile.Text);
             try
             {
                 Mp3Writer writer = new Mp3Writer(new FileStream(textBoxOutFile.Text, FileMode.Create), m_Config);
                 try
                 {
                     byte[] buff   = new byte[writer.OptimalBufferSize];
                     int    read   = 0;
                     int    actual = 0;
                     long   total  = InStr.Length;
                     Cursor.Current = Cursors.WaitCursor;
                     try
                     {
                         while ((read = InStr.Read(buff, 0, buff.Length)) > 0)
                         {
                             Application.DoEvents();
                             writer.Write(buff, 0, read);
                             actual           += read;
                             progressBar.Value = (int)(((long)actual * 100) / total);
                             toolTip1.SetToolTip(progressBar, string.Format("{0}% compresssed", progressBar.Value));
                             this.Text = string.Format("Audio Compress - {0}% compresssed", progressBar.Value);
                             Application.DoEvents();
                         }
                         toolTip1.SetToolTip(progressBar, "Done");
                         this.Text = "Audio Compress - Done";
                     }
                     finally
                     {
                         Cursor.Current = Cursors.Default;
                     }
                 }
                 finally
                 {
                     writer.Close();
                 }
             }
             finally
             {
                 InStr.Close();
             }
         }
         finally
         {
             Compressing = false;
             RefreshControls();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "An exception has ocurred with the following message", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Esempio n. 11
0
        public void Process(byte[] data)
        {
            if (Mp3Writer == null)
            {
                return;
            }

            Mp3Writer.Write(data);

            if (Mp3Writer.DataAvailable == 0)
            {
                return;
            }

            LinkedList <ClientInfo> remove = new LinkedList <ClientInfo>();

            /* only send metadata if it has changed */
            string metaData        = "StreamTitle='" + Description + "';";
            bool   updatedMetaData = (metaData != LastMetaData);

            LastMetaData = metaData;

            lock (ClientStreams)
            {
                if (ClientStreams.Count > 0)
                {
                    foreach (ClientInfo info in ClientStreams)
                    {
                        try
                        {
                            info.UpdateMeta |= updatedMetaData;

                            int nextMetaData = MetaInt - (int)(info.DataSent % (ulong)MetaInt);

                            /* split this buffer? */
                            if (SendMetadata && nextMetaData < Mp3Writer.DataAvailable)
                            {
                                int firstCount  = nextMetaData;
                                int secondCount = (Mp3Writer.DataAvailable - firstCount);

                                /* first write the first part */
                                info.Stream.Write(Mp3Writer.m_OutBuffer, 0, firstCount);
                                info.DataSent += (ulong)firstCount;

                                /* only send metadata if it has changed */
                                SendMetaData(info, metaData);

                                /* finally the second part */
                                info.Stream.Write(Mp3Writer.m_OutBuffer, firstCount, secondCount);
                                info.DataSent += (ulong)secondCount;
                            }
                            else
                            {
                                info.Stream.Write(Mp3Writer.m_OutBuffer, 0, Mp3Writer.DataAvailable);
                                info.DataSent += (ulong)Mp3Writer.DataAvailable;
                            }
                        }
                        catch (Exception e)
                        {
                            remove.AddLast(info);
                        }
                    }
                }

                foreach (ClientInfo info in remove)
                {
                    ClientStreams.Remove(info);
                    info.Stream.Close();
                    UpdateStatus();
                }
            }
            Mp3Writer.DataAvailable = 0;
        }
Esempio n. 12
0
        public void Process(byte[] data)
        {
            lock (this)
            {
                if (FileStream == null)
                {
                    return;
                }

                if (SquelchState == DemodulationState.eSquelchState.Closed)
                {
                    if (SquelchClosedCounter >= SquelchClosedDelay * SamplingRate)
                    {
                        return;
                    }
                    else
                    {
                        SquelchClosedCounter += data.Length / 2;
                        Array.Clear(data, 0, data.Length);
                    }
                }
                else
                {
                    SquelchClosedCounter = 0;
                }

                switch (FileType)
                {
                case eFileType.MP3:
                    if (Mp3Writer == null)
                    {
                        return;
                    }

                    /* feed samples into mp3 converter */
                    Mp3Writer.Write(data);

                    if (Mp3Writer.DataAvailable == 0)
                    {
                        return;
                    }

                    /* write mp3 data */
                    FileStream.Write(Mp3Writer.m_OutBuffer, 0, Mp3Writer.DataAvailable);
                    Mp3Writer.DataAvailable = 0;

                    break;

                case eFileType.WAV:
                    FileStream.Write(data, 0, data.Length);
                    break;
                }

                /* flush every block */
                if (FileStream.Position > NextFlushPosition)
                {
                    DisplayedStatus   = "Writing, Position: " + FileStream.Position;
                    NextFlushPosition = FileStream.Position + FlushDelta;
                    FileStream.Flush();
                }
            }
        }