-- $Date: 2004/02/14 09:46:15 $
-- $Revision: 1.12 $
-- $Author: jcrocholl $

with Integer_Strings; use Integer_Strings;
with Readers; use Readers;
with Token_Readers; use Token_Readers;

package body Pitches.MusicXML is

   -- Read pitch data from an XML reader.
   function Read_Pitch
     (XML : access XML_Reader-- Use this XML reader.
     return Pitch_Access       -- The newly created pitch.
   is
      Step   : Step_Enum := C;
      Alter  : Integer := 0;
      Octave : Integer := 4;
   begin
      while Descend(XML) loop
         Exit_Tag(XML);

         if Found(XML, "step") then
            Step := To_Step_Enum(Read_Data(XML));
         elsif Found(XML, "alter") then
            Alter := To_Number(Read_Data(XML));
         elsif Found(XML, "octave") then
            Octave := To_Number(Read_Data(XML));
         else
            XML_Expect_Error(XML, "/pitch",
              "step" / "octave");
         end if;

         Exit_Element(XML);
      end loop;
      Exit_Tag(XML);

      return Create(Step, Alter, Octave);
   end Read_Pitch;

   -- Write pitch data to an XML writer.
   procedure Write_Pitch
     (XML  : access XML_Writer-- Use this XML writer.
      This : access Pitch)      -- Write this pitch.
   is
   begin
      Start_Element(XML, "pitch");

      Write_Element(XML, "step", Get_Step(This)'Img);

      if Get_Alter(This) /= 0 then
         Write_Element(XML, "alter", To_String(Get_Alter(This)));
      end if;

      Write_Element(XML, "octave", To_String(Get_Octave(This)));

      Close_Element(XML, "pitch");
   end Write_Pitch;

end Pitches.MusicXML;