예제 #1
0
        private ArrayHandleFloat LoadSampleHelper(
            ChannelType channel,
            string type,
            string name)
        {
            if (!String.Equals(type, String.Empty) &&
                !String.Equals(type, "WAV") &&
                !String.Equals(type, "AIFF"))
            {
                throw new EvalErrorException(EvalErrors.eEvalUnableToImportFile);
            }

            // SECURITY: ensure navigation to user-provided path does not escape from the permitted root directories

            string path = null;

            if (mainWindow.SavePath != null)
            {
                string path2;
                if (SecurePathCombine(Path.GetDirectoryName(mainWindow.SavePath), name, out path2) &&
                    File.Exists(path2))
                {
                    path = path2;
                }
            }
            if (path == null)
            {
                string path2;
                if (SecurePathCombine(Program.GetSettingsDirectory(false /*create*/, true /*roaming*/), name, out path2) &&
                    File.Exists(path2))
                {
                    path = path2;
                }
            }
            if (path == null)
            {
                throw new EvalErrorException(EvalErrors.eEvalUnableToImportFile);
            }

            ArrayHandleFloat array = null;

            try
            {
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.BufferSize))
                {
                    using (AudioFileReader reader = Program.TryGetAudioReader(stream))
                    {
                        if (reader == null)
                        {
                            throw new NullReferenceException(); // neither AIFF nor WAV
                        }

                        if ((reader.NumChannels == NumChannelsType.eSampleMono)
                            != (channel == ChannelType.eMonoChannel))
                        {
                            throw new ArgumentException(); // file does not have specified channel
                        }

                        array = new ArrayHandleFloat(new float[reader.NumFrames]);
                        float[] buffer = new float[4096];
                        int     c;
                        int     p = 0;
                        while ((c = reader.ReadPoints(buffer, 0, buffer.Length)) != 0)
                        {
                            switch (channel)
                            {
                            default:
                                Debug.Assert(false);
                                throw new ArgumentException();

                            case ChannelType.eLeftChannel:
                                for (int i = 0; i < c; i += 2)
                                {
                                    array.floats[p++] = buffer[i + 0];
                                }
                                break;

                            case ChannelType.eRightChannel:
                                for (int i = 0; i < c; i += 2)
                                {
                                    array.floats[p++] = buffer[i + 1];
                                }
                                break;

                            case ChannelType.eMonoChannel:
                                for (int i = 0; i < c; i++)
                                {
                                    array.floats[p++] = buffer[i];
                                }
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw new EvalErrorException(EvalErrors.eEvalUnableToImportFile);
            }
            return(array);
        }
예제 #2
0
 public AudioFilePlayGeneratorParams(
     AudioFileReader reader)
 {
     this.reader = reader;
 }