# Sound data Import Export

Can't regain the data after Export.

Create an array of sampled amplitude values:
2. Visualize as sound
3. Save it
4. Get it back
5. ???

sound = RandomReal[1, {500}];
ListPlay[sound]
Export["test.txt", sound];
in=Import["test.txt"]
ListPlay[in]


A second example.
Take data from a WAV sound,
Export it,
reImport it,
play it.

tubadat = ExampleData[{"Sound", "Tuba"}, "Data"];
ListPlay[in]


It is not the original sound.

-
 related – acl Dec 25 '12 at 15:29

Try Export["~/Desktop/test.txt", Compress@sound] and in = Uncompress@Import["~/Desktop/test.txt"]. The way you did it, it gets imported as a string. There's other ways, but the one I present is what I always use to export/import arbitrary data structures: it's cross-platform and robust (as far as I can tell).

Regarding the second example and the comments:

tubadat = ExampleData[{"Sound", "Tuba"}, "Data"];
ListPlay[in, SampleRate -> 22050, PlayRange->{-1,1}]


works; the sounds seem identical and tubadat==in evaluates to True. Note that setting the sample rate is necessary (it defauls to 8k).

To summarise, you need to

• make sure you're importing in the right format (your original code ended up importing as a string)

• fix the sample rate as it defaults to the wrong values for this case.

• possibly fix the max amplitude (otherwise the sample is amplified or clipped)

-
 Unfortionatly it does not work. Please take the second example in my question. The sound when reimported is distorted. – Hp Radojewski Schäfer Von Dec 25 '12 at 15:51 @HpRadojewskiSchäferVon If I do tubadat = ExampleData[{"Sound", "Tuba"}, "Data"]; ListPlay[tubadat, SampleRate -> 22050] Export["~/Desktop/TubaDat.txt", Compress@tubadat]; in = Uncompress@Import["~/Desktop/TubaDat.txt"]; ListPlay[in, SampleRate -> 22050] tubadat == in then the sounds seem identical and tubadat is literally the same list as in. did you a) use txt instead of dat (I guess yes)? b) set the sample rate correctly in ListPlay – acl Dec 25 '12 at 16:19 OK. . . . I now see where the real culprit is located. . . . It is already distorted when importing the Data in tubadat = ExampleData[{"Sound", "Tuba"}, "Data"]; Compare it with ExampleData[{"Sound", "Tuba"}]. . . Here it goes wrong. – Hp Radojewski Schäfer Von Dec 25 '12 at 16:54 are you sure it's not just a question of setting the sample rate? it defaults to 8192Hz – acl Dec 25 '12 at 16:55 OK. . .see . . ExampleData[{"Sound", "Tuba"}] . . take. . tubadat = ExampleData[{"Sound", "Tuba", SampleRate -> 22050]; . . see . . ListPlay[tubadat] – Hp Radojewski Schäfer Von Dec 25 '12 at 17:12

The 4th line needs correction:

in = Import["test.txt", "Data"] // Flatten


Now it will work - you need to read up on import/export of data on Documentation. This is fixes your specific code. But there are many ways to do this. For example to use "test.dat" format is better (no "Data" specification needed in Import). Or exporting sound itself (not the data) will work too - with the advantage that you can play it in other software:

data = RandomReal[1, 500];
sound = ListPlay[data];
Export["test.wav", sound];
Import["test.wav"]


-
 The solution by .dat works fine here. – Hp Radojewski Schäfer Von Dec 25 '12 at 15:03 The solution by .dat works fine here. But I encounterd the problem in a task, where not the sound itself has to be exported but only the data. And later on the data has to be imported back. Here is the example. First we got a wav-sound: tuba = ExampleData[{"Sound", "Tuba"}]; We extract the data: tubadat = ExampleData[{"Sound", "Tuba"}, "Data"]; Export["tubaDat.dat", tubadat]; in = Import[tubaDat.dat]; ListPlay[in] As you see, the replay of the imported file is distorted. – Hp Radojewski Schäfer Von Dec 25 '12 at 15:18